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 外键名;