列属性
是否为空
是否为空
null 表示字段值可以为null
not null字段值不能为空
默认值
默认值
如果一个字段没有插入值,可以默认插入一个指定的值
default 关键字用来插入默认值
mysql> create table stu19(
-> name varchar(20) not null default '姓名不详',
-> addr varchar(50) not null default '地址不详'
-> );
# `Query OK, 0 rows affected (0.05 sec)`
mysql> insert into stu19(name) values ('tom');
# `Query OK, 1 row affected (0.00 sec)`
mysql> insert into stu19 values (default,default);
# `Query OK, 1 row affected (0.00 sec)`
mysql> select * from stu19;
+----------+----------+
| name | addr |
+----------+----------+
| tom | 地址不详 |
| 姓名不详 | 地址不详 |
+----------+----------+
# `2 rows in set (0.00 sec)`
自动增长
自动增长
字段值从1开始,每次递增1,自动增长的值就不会有重复,适合用来生成唯一的id
在MySQL中只要是自动增长列必须是主键
auto_increment 关键字用来设置自动增长
主键
概念
唯一标识表中的记录的一个或一组列称为主键
primary key 关键字用来设置主键
特点
不能重复、不能为空
一个表只能有一个主键
作用
保证数据完整性
加快查询速度
原则
最少性:尽量选择单个键作为主键
稳定性:尽量选择数值更新少的列作为主键
比如:
学号 姓名 地址 这三个字段都不重复
选哪个做主键?
选学号,因为学号最稳定
应用
只要是auto_increment必须是主键,但是主键不一定是auto_increment
主键特点是不能重复不能为空
一个表只能有一个主键,但是一个主键可以有多个字段组成
自动增长列通过插入null值让其递增
自动增长列的数据被删除,默认不再重复使用。
truncate table删除数据后(清空表格),再次插入从1开始
练习
在主键列输入的数值,允许为空吗? false
一个表可以有多个主键吗?false
在一个学校数据库中,如果一个学校内允许重名的学员,但是一个班级内不允许学员重名,可以组合班级和姓名两个字段一起来作为主键吗? true
标识列(自动增长列)允许为字符数据类型吗? false
一个自动增长列中,插入3行,删除2行,插入3行,删除2行,插入3行,删除2行,再次插入是多少? 10
创建主键
mysql> create table stu20(
-> id int auto_increment primary key,
-> name varchar(20)
-> );
# `Query OK, 0 rows affected (0.04 sec)`
mysql> create table stu21(
-> id int auto_increment,
-> name varchar(20),
-> primary key(id)
-> );
# `Query OK, 0 rows affected (0.02 sec)`
创建组合主键
mysql> create table stu22(
-> classname varchar(20),
-> stuname varchar(20),
-> primary key(classname,stuname)
-> );
# `Query OK, 0 rows affected (0.00 sec)`
mysql> desc stu22;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| classname | varchar(20) | NO | PRI | | |
| stuname | varchar(20) | NO | PRI | | |
+-----------+-------------+------+-----+---------+-------+
# `2 rows in set (0.00 sec)`
更改表添加主键
-- 创建表
mysql> create table stu23(
-> id int,
-> name varchar(20)
-> );
# `Query OK, 0 rows affected (0.05 sec)`
-- 添加主键
mysql> alter table stu23 add primary key(id);
# `Query OK, 0 rows affected (0.09 sec)`
# `Records: 0 Duplicates: 0 Warnings: 0`
删除主键
mysql> alter table stu23 drop primary key;
# `Query OK, 0 rows affected (0.03 sec)`
# `Records: 0 Duplicates: 0 Warnings: 0`
插入数据
mysql> create table stu25(
-> id tinyint unsigned auto_increment primary key,
-> name varchar(20)
-> );
# `Query OK, 0 rows affected (0.05 sec)`
-- 插入数据
mysql> insert into stu25 values (3,'tom'); -- 可以直接插入数字
# `Query OK, 1 row affected (0.06 sec)`
-- 自动增长列可以插入null,让列的值自动递增
mysql> insert into stu25 values (null,'berry');
# `Query OK, 1 row affected (0.00 sec)`
唯一键
唯一键与主键的区别
主键
不能重复,不能为空
一个表只能有一个主键
唯一键
不能重刻,可以为空
一个表可以有多个唯一键
关键字
unique
unique key
创建唯一键
mysql> create table stu26(
-> id int auto_increment primary key,
-> name varchar(20) unique
-> );
# `Query OK, 0 rows affected (0.05 sec)`
mysql> create table stu27(
-> id int primary key,
-> name varchar(20),
-> unique(name)
-> );
# `Query OK, 0 rows affected (0.05 sec)`
修改表添加唯一键
-- 将name设为唯一键
mysql> alter table stu28 add unique(name);
-- 将name,addr设为唯一键
mysql> alter table stu28 add unique(name),add unique(addr);
# `Query OK, 0 rows affected (0.00 sec)`
# `Records: 0 Duplicates: 0 Warnings: 0`
mysql> desc stu28;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
| addr | varchar(20) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
# `3 rows in set (0.02 sec)`
查看唯一键的名
mysql> show create table stu28\G;
通过唯一键的名字删除唯一键
mysql> alter table stu28 drop index name;
# `Query OK, 0 rows affected (0.00 sec)`
# `Records: 0 Duplicates: 0 Warnings: 0`
备注
备注
说明性文本
备注属于SQL代码的一部分
通过关键字comment设置
mysql> create table stu29(
-> id int primary key comment '学号',
-> name varchar(20) not null comment '姓名'
-> );
# `Query OK, 0 rows affected (0.03 sec)`