mysql> desc t2; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | pay | float(7,2) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 1 row in set (0.00 sec)
mysql> create table t4( -> age float(7,2), -> high float(3,2) -> ); Query OK, 0 rows affected (0.36 sec)
mysql> desc t4; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | age | float(7,2) | YES | | NULL | | | high | float(3,2) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) insert into t4 values(11211,1.82); ########################################################### 字符類型 --定長:char(字符數) 最大長度255字符 不夠指定字符數時再右邊用空格補齊 字符數超出時,無法寫入數據 --varchar(字符數) 按數據實際大小分配存儲空間 字符數超出時,無法寫入數據 --大文本類型:text/blob 字符數大與65535存儲時使用 mysql> create table t8( -> name char(10), -> class char(7), -> address char(15), -> mail varchar(30) -> ); mysql> desc t8; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | char(10) | YES | | NULL | | | class | char(7) | YES | | NULL | | | address | char(15) | YES | | NULL | | | mail | varchar(30) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> insert into t8 values("jim","nsd1709","beijing","123456@qq.com") Query OK, 1 row affected (0.04 sec)
mysql> select * from t8; +------+---------+---------+---------------+ | name | class | address | mail | +------+---------+---------+---------------+ | jim | nsd1709 | beijing | 123456@qq.com | +------+---------+---------+---------------+ 1 row in set (0.00 sec) #################################################################### 日期時間類型: 年 year YYYY 2017 日期 date YYYYMMDD 20171220 時間 time HHMMSS 155302 日期時間: datetime YYYYMMDDHHMMSS timestamp YYYYMMDDHHMMSS
mysql> create table t9( -> name char(10), -> age tinyint, -> s_year year, -> uptime time, -> birthday date, -> party datetime -> ); Query OK, 0 rows affected (0.37 sec)
mysql> desc t9; +----------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+------------+------+-----+---------+-------+ | name | char(10) | YES | | NULL | | | age | tinyint(4) | YES | | NULL | | | s_year | year(4) | YES | | NULL | | | uptime | time | YES | | NULL | | | birthday | date | YES | | NULL | | | party | datetime | YES | | NULL | | +----------+------------+------+-----+---------+-------+ 6 rows in set (0.00 sec) mysql> insert into t9 values("Tom",24,1992,073000,19920221122020,20180131122100); Query OK, 1 row affected, 1 warning (0.04 sec) mysql> select * from t9; +------+------+--------+----------+------------+---------------------+ | name | age | s_year | uptime | birthday | party | +------+------+--------+----------+------------+---------------------+ | Tom | 24 | 1992 | 07:30:00 | 1992-02-21 | 2018-01-31 12:21:00 | +------+------+--------+----------+------------+---------------------+ 1 row in set (0.00 sec) #################################################### 時間函數 now() 獲取調用次函數時的系統日期時間 sysdate() 執行時動態獲得系統日期時間 sleep(N) 休眠N秒 curdate() 獲得當前的系統日期 curtime() 獲得當前的系統時刻 month() 獲得指定時間中的月份 date() 獲得指定時間中的日期 time() 獲取指定時間中的時刻
mysql> select from t9; +-------+------+--------+----------+------------+---------------------+ | name | age | s_year | uptime | birthday | party | +-------+------+--------+----------+------------+---------------------+ | Tom | 24 | 1992 | 07:30:00 | 1992-02-21 | 2018-01-31 12:21:00 | | Jerry | 25 | 1991 | 06:50:55 | 1991-08-19 | 2018-01-31 12:21:00 | +-------+------+--------+----------+------------+---------------------+ 2 rows in set (0.00 sec) mysql> insert into t9 values("kenji",19,year(now()),time(now()),date(now()),now()); Query OK, 1 row affected (0.04 sec) mysql> select from t9; +-------+------+--------+----------+------------+---------------------+ | name | age | s_year | uptime | birthday | party | +-------+------+--------+----------+------------+---------------------+ | Tom | 24 | 1992 | 07:30:00 | 1992-02-21 | 2018-01-31 12:21:00 | | Jerry | 25 | 1991 | 06:50:55 | 1991-08-19 | 2018-01-31 12:21:00 | | kenji | 19 | 2017 | 03:55:12 | 2017-12-20 | 2017-12-20 03:55:12 | +-------+------+--------+----------+------------+---------------------+ 3 rows in set (0.00 sec) ########################################################### 枚舉類型:字段的值只能在列表的范圍內選擇 字段名 enum(值列表) 只能選擇一個值,在賦值時可用數字選擇。 字段名 set(值列表) 多選
mysql> create table t12( name char(10), sex enum("boy","girl"), yourlikes set("book","film","game","study") ); Query OK, 0 rows affected (0.43 sec)
mysql> desc t12; +-----------+-----------------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-----------------------------------+------+-----+---------+-------+ | name | char(10) | YES | | NULL | | | sex | enum('boy','girl') | YES | | NULL | | | yourlikes | set('book','film','game','study') | YES | | NULL | | +-----------+-----------------------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> insert into t12 values("ZhouMing","boy","book,film"); Query OK, 1 row affected (0.04 sec)
mysql> select * from t12; +----------+------+-----------+ | name | sex | yourlikes | +----------+------+-----------+ | ZhouMing | boy | book,film | +----------+------+-----------+ 1 row in set (0.00 sec) ############################################################## 約束條件:作用限制賦值 --Null 允許為空,默認設置 --NO NULL 不允許為空 Key 索引類型 Default 設置默認值,缺省為NULL
mysql> create table t13( name char(10) not null, sex enum('man','woman') not null default "man", age tinyint not null default 23 ); Query OK, 0 rows affected (0.37 sec)
mysql> desc t13; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | name | char(10) | NO | | NULL | | | sex | enum('man','woman') | NO | | man | | | age | tinyint(4) | NO | | 23 | | +-------+---------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
mysql> select * from t13; +---------+-----+-----+ | name | sex | age | +---------+-----+-----+ | chihiro | man | 23 | +---------+-----+-----+ 1 row in set (0.00 sec) ####################################################### 修改表結構 mysql> alter table 表名 執行動作;
-查看索引 desc 表名; show index from 表名; //查看索引信息的具體值 創建索引 默認使用的索引類型:BTREE(二叉樹) 1-10 1-5 6-10 hash B+Tree create index 索引名 on 表名(字段名); 1)建表時創建索引 mysql> create table t25( -> name char(10), -> age int, -> sex enum("boy","girl"), -> index(sex), -> index(name) -> ); mysql> desc t25; +-------+--------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------------+------+-----+---------+-------+ | name | char(10) | YES | MUL | NULL | | | age | int(11) | YES | | NULL | | | sex | enum('boy','girl') | YES | MUL | NULL | | +-------+--------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> create index name on t21(name); //一般習慣將索引名與字段名相同 Query OK, 0 rows affected (0.42 sec) 2)在已有表創建索引 mysql> desc t21; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | name | char(5) | YES | | NULL | | | age | int(2) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> create index name on t21(name); Query OK, 0 rows affected (0.42 sec) ysql> desc t21; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | name | char(5) | YES | MUL | NULL | | | age | int(2) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
mysql> show index from t21/G 1. row Table: t21 Non_unique: 1 Key_name: name Seq_in_index: 1 Column_name: name Collation: A Cardinality: 1 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: 1 row in set (0.00 sec)
刪除 drop index 索引名 on 表名; ########################################################### primary key主鍵 注意事項 -一個表中只能有一個primary key字段 -對應的字段值不允許有重復,且不允許賦NULL值 -如果有多個字段都作為PRIMARY KEY,稱為復合主鍵,必須一起創建 -主鍵字段的KEY標志是PRI 通常與AUTO INCREMENT連用 -經常把表中能夠唯一標識記錄的字段設置為主鍵字段【記錄編號字段】 1)已有表設主鍵 mysql> drop index name on t25; mysql> desc t25; +-------+--------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------------+------+-----+---------+-------+ | name | char(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | | sex | enum('boy','girl') | YES | MUL | NULL | | +-------+--------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> select * from t25; Empty set (0.00 sec) mysql> alter table t25 add primary key(name); //只能有一個主鍵 Query OK, 0 rows affected (0.47 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table t25 drop primary key; //刪除主鍵 2)新建表,設主鍵 mysql> create table t26( -> name char(10), -> age int, -> likes set("a","b","c"), -> primary key(name) -> ); mysql> desc t26; +-------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+-------+ | name | char(10) | NO | PRI | NULL | | | age | int(11) | YES | | NULL | | | likes | set('a','b','c') | YES | | NULL | | +-------+------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) 建表時上例主建加在中間也可以 eg:name char(10) primary key, 效果與t26相同 3)復合主鍵:多個字段一起做主鍵,字段不允許同時重復 mysql> create table t28( cip char(15), port smallint, status enum("allow","deny") default "deny", primary key(cip,port) ); Query OK, 0 rows affected (0.31 sec)
mysql> desc t28; +--------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+----------------------+------+-----+---------+-------+ | cip | char(15) | NO | PRI | NULL | | | port | smallint(6) | NO | PRI | NULL | | | status | enum('allow','deny') | YES | | deny | | +--------+----------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> alter table t28 drop primary key; //刪除主鍵 mysql> alter table t28 add primary key(cip,port); //添加主鍵
4)與auto_increment 連用 字段值自動增長 eg: id name age class jim 21 1709 讓id字段的值自動增長 +1 條件:主鍵并且是數字 mysql> create table t29( -> id int(2) zerofill primary key auto_increment, -> name char(10), -> class char(10), -> index(name) -> ); Query OK, 0 rows affected (0.22 sec)
mysql> desc t29; +-------+--------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------------------+------+-----+---------+----------------+ | id | int(2) unsigned zerofill | NO | PRI | NULL | auto_increment | | name | char(10) | YES | MUL | NULL | | | class | char(10) | YES | | NULL | | +-------+--------------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql> insert into t29(name,class) values("tom","1709"); mysql> insert into t29(name,class) values("jerry","1709"); mysql> insert into t29 values(9,"jack","1709"); //可自己賦值id,但id屬于主鍵,不能同名 mysql> insert into t29(name,class) values("rose","1709"); //自動增長會選擇數字最大的值進行自動增長,之前設置id=9,再開啟自動增長則為10 mysql> select * from t29; +----+-------+-------+ | id | name | class | +----+-------+-------+ | 01 | tom | 1709 | | 02 | jerry | 1709 | | 09 | jack | 1709 | | 10 | rose | 1709 | +----+-------+-------+ 4 rows in set (0.00 sec) mysql> alter table t29 drop primary key; //無法刪除主鍵,因為id設置為auto_increment自動增長,該命令必須是主鍵才可設立。 ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key mysql> alter table t29 modify id int; //修改字段類型,取消自動增長 mysql> alter table t29 drop primary key; //刪除主鍵成功 Query OK, 4 rows affected (1.00 sec) ########################################################## UNIQUE唯一索引 唯一索引不可賦相同值 ,可以為NULL 1)建表的時候指定UNIQUE字段 mysql> create table t211( stu_id char(9), name char(10), sex enum("boy","girl"), unique(stu_id) ); 指定學號為唯一索引 mysql> desc t211; +--------+--------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------------+------+-----+---------+-------+ | stu_id | char(9) | YES | UNI | NULL | | | name | char(10) | YES | | NULL | | | sex | enum('boy','girl') | YES | | NULL | | +--------+--------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) ######################################################### mysql> create table t212( stu_id char(9) not NULL, name char(10), sex enum("boy","girl"), unique(stu_id) ); Query OK, 0 rows affected (0.26 sec) //指定stu_id為唯一索引,但不允許它為空值,則描述信息顯示stu_id為RPI,但實際上主鍵不存在也無法刪除 mysql> desc t212; +--------+--------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------------+------+-----+---------+-------+ | stu_id | char(9) | NO | PRI | NULL | | | name | char(10) | YES | | NULL | | | sex | enum('boy','girl') | YES | | NULL | | +--------+--------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) ####################################### 練習 mysql> desc stuinfo +-------+-------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+-------------------+-----------------------------+ | name | varchar(15) | YES | | NULL | | | class | char(7) | YES | | NULL | | | party | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +-------+-------------+------+-----+-------------------+-----------------------------+ 3 rows in set (0.00 sec) alter table stuinfo add stu_id char(7) first create ubique index stu_id on stuinfo(name) alter table stuinfo add id int(2) zerofill primary key auto_increment; ############################################################### 外鍵 foreign key(字段名) references 表名(字段名)on uptate cascade on delete cascade 同步更新,同步刪除 作用:限制給字段賦值的,值必須在指定表中指定字段值的范圍里選擇 mysql> create table jfb( -> id int(2) primary key auto_increment, -> name char(10), -> pay float(7,2) -> )engine=innodb; 建立參照表 insert into jfb(name,pay)values("tom",20000),("lucy",20000); Query OK, 2 rows affected (0.07 sec)
mysql> create table xsb( num int(2), name char(10), class char(9), foreign key(num) references jfb(id) on update cascade on delete cascade )engine=innodb;
同步修改 update 表名 set 字段名=值 where 條件; //條件就是原有的(字段名=值) 同步刪除 delete from 表名 where 條件; 被參考的表不能隨意刪除
刪除外鍵字段 show create table 表名; alter table 表名 drop foreign key 外鍵名;