建立表语法mysql
建立一个表,多个字段: create table 表名( 字段名 列类型 [可选的参数], # 记住要加逗号 字段名 列类型 [可选的参数] # 最后一行不加逗号 ... )charset=utf8; # 后面加;号
说明:列约束用来保证数据的完整性和一致性,约束条件都是建立表语法的时候可选参数sql
auto_increment:
表示自增长1数据库
not null:
标识该字段不能为空default:为该字段设置默认值
foreign key:
外键索引unique key:
标识字段的值是惟一的,字段值不能重复,能够有多个primary key:
主键索引,能够加快查询速度,字段值不能重复,只能有一个unsigned:
无符号,默认是有符号的zerofill:
使用0填充安全
例子服务器
# 建立表 mysql> create table t3( -> id int unsigned auto_increment primary key, -> name char(10) not null default "xxx", -> age int not null default 0 -> )charset=utf8; Query OK, 0 rows affected (0.03 sec) # 向表中插入数据 mysql> insert into t3 (age) values (18); Query OK, 1 row affected (0.00 sec) # 查询表中数据 mysql> select * from t3; +----+------+-----+ | id | name | age | +----+------+-----+ | 1 | xxx | 18 | +----+------+-----+ 1 row in set (0.00 sec) # 查看表结构 desc 表名 mysql> desc t3; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | char(10) | NO | | xxx | | | age | int(11) | NO | | 0 | | +-------+------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
列类型 | 说明 |
---|---|
数字(整数) | 存储年龄、等级、id等 |
数字(浮点数) | 存储薪资、身高、体重等 |
字符串 | 存储姓名、性别等 |
时间日期类型 | 存储注册时间、入职时间等 |
枚举 | 字段的值只能在给定范围中选择 |
数字(整形)ui
unsigned
加在列类型后面,表明无符号,不能取负数,默认是有符号,能够是负数加密
应用场景:根据公司业务场景选择合适类型code
例子:blog
# 咱们建立一个t4表,限制使用无符号的 mysql> create table t4(x int unsigned); Query OK, 0 rows affected (0.02 sec) # 查看表结构,默认int数值长度已经设置为10位 mysql> desc t4; +-------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+-------+ | x | int(10) unsigned | YES | | NULL | | +-------+------------------+------+-----+---------+-------+ 1 row in set (0.00 sec) # 只能插入无符号:[0 ~ 4294967295]范围值内的数字 # 超过数字的长度也报错 mysql> insert into t4 values (42949672955); ERROR 1264 (22003): Out of range value for column 'x' at row 1 # 只能插入无符号:[0 ~ 4294967295]范围值内的数字 # 超过范围即报错 mysql> insert into t4 values (4294967296); ERROR 1264 (22003): Out of range value for column 'x' at row 1 # 在这个范围内则成功 mysql> insert into t4 values (4294967295); Query OK, 1 row affected (0.00 sec) # 查询插进去的数据,若是数据库配置是非安全模式的话,插进去的数据是这个数据类型限制的最大数值 mysql> select * from t4; +------------+ | x | +------------+ | 4294967295 | | 4294967295 | +------------+ 2 rows in set (0.00 sec)
注意:那么有的同窗可能不会报错,能插进去,那是由于你的mysql5.6 没有开启安全模式,mysql5.7 之后的版本默认都是安全模式
# 查看当前数据库模式: mysql> show variables like "%sql_mode%"; +---------------+--------------------------------------------+ | Variable_name | Value | +---------------+--------------------------------------------+ | sql_mode | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | +---------------+--------------------------------------------+ 1 row in set (0.00 sec) sql_model=no_engine_substitution # 非安全性,默认 sql_model=strict_trans_tables # 安全性 # 临时设置为安全模式,服务重启后会被重置 mysql>: set global sql_mode="strict_trans_tables"; # 在root用户登陆状态下 # 在设置后,quit断开数据库链接后(服务器不重启)就会进入安全模式, # 那么如今在插入超过范围内的数据就会报错
数字(浮点型)
例子:
# 建立表t5 限制salary字段为decimal数据类型,num为float数据类型 mysql> create table t5( -> id int auto_increment primary key, -> salary decimal(16,10), -> num float -> )charset=utf8; Query OK, 0 rows affected (0.02 sec) # 查看表结构 mysql> desc t5; +--------+----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+----------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | salary | decimal(16,10) | YES | | NULL | | | num | float | YES | | NULL | | +--------+----------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) # 插入数据,salary总数字长度为16,小数点后面正好为10位,插入数据 mysql> insert into t5 (salary,num) values (500000.2312345678,5000.232423523534634); Query OK, 1 row affected (0.01 sec) # 查询没有问题,精确存,可是num float类型的不精确,存两位小数点,还四舍五入了 mysql> select * from t5; +----+-------------------+---------+ | id | salary | num | +----+-------------------+---------+ | 1 | 500000.2312345678 | 5000.23 | +----+-------------------+---------+ 1 row in set (0.00 sec) # 插入数据,salary总数字长度为15,小数点后面小于10位,插入数据mysql> insert into t5 (salary,num) values (500000.231234567,5000.232423523534634); Query OK, 1 row affected (0.01 sec) # 查询,不精确,缺省的一位用0补齐,可是num float类型的不精确,存两位小数点,还四舍五入了 mysql> select * from t5; +----+-------------------+---------+ | id | salary | num | +----+-------------------+---------+ | 1 | 500000.2312345678 | 5000.23 | | 2 | 500000.2312345670 | 5000.23 | +----+-------------------+---------+ 2 rows in set (0.00 sec) # 插入数据,salary总数字长度为17,小数点后面大于10位,插入数据 mysql> insert into t5 (salary,num) values (500000.23123456789,5000.232423523534634); Query OK, 1 row affected, 1 warning (0.01 sec) # 查询,不精确,只能存指定的长度,多出来的四舍五入了,可是num float类型的不精确,存两位小数点,还四舍五入了 mysql> select * from t5; +----+-------------------+---------+ | id | salary | num | +----+-------------------+---------+ | 1 | 500000.2312345678 | 5000.23 | | 2 | 500000.2312345670 | 5000.23 | | 3 | 500000.2312345679 | 5000.23 | +----+-------------------+---------+ 3 rows in set (0.00 sec)
字符串
二者区别:
char 定长,不管插入字符是多少个,永远固定占规定的长度,使用场景:身份证、手机号、md5加密事后的密码char(32)
varchar 变长,根据插入的字符串长度计算所占的字节数,可是总有一个字节是用来保存字符串大小的,
若是不能肯定插入的数据的大小,通常建议使用varchar(255)。
Value | CHAR(4) |
Storage Required | VARCHAR(4) |
Storage Required |
---|---|---|---|---|
'' |
' ' |
4 bytes | '' |
1 byte |
'ab' |
'ab ' |
4 bytes | 'ab' |
3 bytes |
'abcd' |
'abcd' |
4 bytes | 'abcd' |
5 bytes |
'abcdefgh' |
'abcd' |
4 bytes | 'abcd' |
5 bytes |
例子:
# 建立t6表 mysql> create table t6( -> id int unsigned auto_increment primary key, -> name char(10) not null default 'xxx' -> )charset=utf8; Query OK, 0 rows affected (0.02 sec) mysql> desc t6; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | char(10) | NO | | xxx | | +-------+------------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) # 建立t7表 mysql> create table t7( -> id int unsigned auto_increment primary key, -> name varchar(10) not null default 'xxx' -> )charset=utf8; Query OK, 0 rows affected (0.03 sec) mysql> desc t7; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(10) | NO | | xxx | | +-------+------------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) mysql> insert into t6 (name) values ("hello"); Query OK, 1 row affected (0.01 sec) mysql> insert into t7 (name) values ("hello"); Query OK, 1 row affected (0.01 sec) mysql> select * from t6; +----+-------+ | id | name | +----+-------+ | 1 | hello | +----+-------+ 1 row in set (0.00 sec) mysql> select * from t7; +----+-------+ | id | name | +----+-------+ | 1 | hello | +----+-------+ 1 row in set (0.00 sec) mysql> insert into t6 (name) values ("hello32dwdsaffgfrthtrhtr"); ERROR 1406 (22001): Data too long for column 'name' at row 1 mysql> insert into t7 (name) values ("hello32dwdsaffgfrthtrhtr"); ERROR 1406 (22001): Data too long for column 'name' at row 1
时间日期类型
例子:
mysql> create table t8( -> d date, -> t time, -> dt datetime -> ); Query OK, 0 rows affected (0.04 sec) mysql> desc t8; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | d | date | YES | | NULL | | | t | time | YES | | NULL | | | dt | datetime | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec) # now()表示当前时间 mysql> insert into t8 values(now(),now(),now()); Query OK, 1 row affected, 1 warning (0.00 sec) # 按照指定时间类型存时间 mysql> select * from t8; +------------+----------+---------------------+ | d | t | dt | +------------+----------+---------------------+ | 2019-10-29 | 01:23:08 | 2019-10-29 01:23:08 | +------------+----------+---------------------+ 1 row in set (0.00 sec)
枚举
列出全部选项
mysql> create table t9( -> id int auto_increment primary key, -> gender enum("male","female") -> )charset=utf8; Query OK, 0 rows affected (0.03 sec) mysql> insert into t9 (gender) values ("male"); Query OK, 1 row affected (0.00 sec) mysql> insert into t9 (gender) values ("female"); Query OK, 1 row affected (0.00 sec) # 只能插入枚举类型的数据 mysql> insert into t9 (gender) values ("femal"); ERROR 1265 (01000): Data truncated for column 'gender' at row 1 mysql> mysql> select * from t9; +----+--------+ | id | gender | +----+--------+ | 1 | male | | 2 | female | +----+--------+ 2 rows in set (0.00 sec)
drop table 表名
语法:
alter table 旧表名 rename 新表名
语法:
alter table 表名 add 字段名 列类型 [可选的参数];
上面添加的列永远是添加在最后一列以后,若是须要在指定位置添加字段的话,使用下面的语法
alter table 表名 add 字段名 列类型 [可选的参数] first;
alter table 表名 add 字段名 列类型 [可选的参数] after 字段名
alter table 表名 modify 字段名 列类型 [可选的参数]
alter table 表名 change 旧字段名 新字段名 新列类型 [可选的参数]
alter table 表名 drop 字段名
show tables:
查看表名
一、 查看被复制表的建立语句:show create table 表名
,而后拷贝sql语句更换表名执行
二、 create table 旧表名 like 新表名
增长数据,语法:
insert into 表名 (列1,列2) values (值1,值2)
delete from 表名 where 条件
;若是不加条件删除表中全部数据
truncate 表名
;没有where条件,删除表中所有数据,速度比delete快
二者区别:
一、 delete删除以后再插入数据,自增id从上一次主键自增,truncate是从1开始
二、 delete删除是一行一行删除,truncate是全选删除,速度比delete快
update 表名 set 列名1=新值1 where 条件
语法:
select 列1,列2 from 表名 [where 条件]
select * from 表名 where id between ... and ...
;between...and...取值范围是闭区间
查询去重
select distinct 列名 from 表名
四则运算
select 列表*10 from 表名
in
select * from 表名 where id in (取值范围)
like模糊查询
select * from 表名 where 列名 like 'x%';
以x开头,%表示通配符
select * from 表名 where 列名 like '%x';
以x结尾,%表示通配符
select * from 表名 where 列名 like '%x%'
包含x的,%表示通配符