目录
1. 表的约束问题:
主键约束:
案例:
非空约束
唯一约束:
默认值约束:
案例:
2.表的结构
前提:接上一张博客内容经行操作
用来保证数据插入的安全性、完整性、正确性
主健,外键,唯一,默认值,非空,检查
一旦某个字段被主键约束修饰,该字段就是主键字段 # primary key
1、该字段的值不允许重复
2、该字段不能为空
该字段就是用来保证唯一的
auto_increment # 自增 默认每次一
mysql> drop table if exists t_user; # 删除表
mysql> delete from t_user; # 清空表数据
Query OK, 5 rows affected (0.00 sec)
mysql> create table t_user (id int primary key, name varchar(225), age int); #增加为主键mysql> show tables; +-----------------+ | Tables_in_db_hj | +-----------------+ | t_user | +-----------------+ 1 row in set (0.00 sec)
mysql> create table t_user (id int primary key auto_increment, name varchar(225), age int); # auto_increment 自增长的整数类型
Query OK, 0 rows affected (0.02 sec)mysql> create table t_user (id int primary key(id) auto_increment , name varchar(225), age int, );
mysql> create table t_user (id int auto_increment, name varchar(225), age int, primary key(id)); # 还可以写为:
mysql> desc t_user; # 查看表 t_user 的结构信息
+-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(255) | YES | | NULL | | | age | int | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec)
mysql> insert into t_user values(null , "SJ", 16);
Query OK, 1 row affected (0.01 sec) # 对表t_user添加元素(默认, "SJ", 16)mysql> select * from t_user; # 查看表t_user
+----+------+------+ | id | name | age | +----+------+------+ | 2 | SJT | 16 | | 3 | SJ | 16 | +----+------+------+ 2 rows in set (0.00 sec)
mysql> insert into t_user values(null , "SJT", 16); # 给表中添加元素
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_user; # 查看此时表中内容
+----+------+------+ | id | name | age | +----+------+------+ | 2 | SJT | 16 | | 3 | SJ | 16 | | 4 | SJT | 16 | +----+------+------+ 3 rows in set (0.00 sec)
当主键完成自增后,即使报错,下一次执行命令成功时,主机依然会在报错后发生自增,具体解释,如图:
是否允许某些字段为空!!! # not null
表示字段不能出现重复值! ! ! # unique (8,0以下的版本也不会报错,但也不会使用)
默认给定一个值 # check (gender in("男",“女))
mysql> insert into t_user values(null, null, 16);
Query OK, 1 row affected (0.01 sec)mysql> select * from t_user;
+----+------+------+ | id | name | age | +----+------+------+ | 2 | SJT | 16 | | 3 | SJ | 16 | | 4 | SJT | 16 | | 5 | NULL | 16 | +----+------+------+ 4 rows in set (0.00 sec)
mysql> insert into t_user values(null, "ldh", 16);
Query OK, 1 row affected (0.00 sec)mysql> select * from t_user;
+----+------+------+ | id | name | age | +----+------+------+ | 2 | SJT | 16 | | 3 | SJ | 16 | | 4 | SJT | 16 | | 5 | NULL | 16 | | 6 | ldh | 16 | +----+------+------+ 5 rows in set (0.01 sec)
mysql> drop table if exists t_user;
Query OK, 0 rows affected (0.01 sec)
创建表:
CREATE TABLE IF NOT EXISTS t_user(
id int primary key auto_increment,
name varchar(50) not null unique,
nickname varchar(255) unique,
age int default 18,
gender varchar(10) check (gender in("男",“女))
);
插入数据-insert:
insert into table name[(id,name, .....)] values ( )insert into table 表名字[(表的字段,。。。)] values (对应制表时的,规则顺序)
mysql> insert into t_user values(null, "liudehua" , "天王", 58, "男"); # 正确
insert into t_user values(default,"liming""小白脸",56,"男") ; # 正确
insert into t_user value(null, "quofucheng","舞王", default, "女"); # 错误
insert into t_user (name, nickname, gender) values("zxy", "歌神","错的");
具体执行时代码:
mysql> insert into t_user values(null, "liudehua" , "天王", 58, "男");
mysql> insert into t_user values(default, "liming", "小白脸", 56, "男");
Query OK, 1 row affected (0.00 sec)mysql> insert into t_user values(null, "guofucheng", "舞王", default, "女");
Query OK, 1 row affected (0.00 sec)mysql> select * from t_user;
+----+------------+-----------+------+--------+ | id | name | nickname | age | gender | +----+------------+-----------+------+--------+ | 1 | liudehua | 天王 | 58 | 男 | | 2 | liming | 小白脸 | 56 | 男 | | 3 | guofucheng | 舞王 | 18 | 女 | +----+------------+-----------+------+--------+ 3 rows in set (0.01 sec)
表的结构:
第一次使用表结构,是在创建表的时候,制定了对应表的结构
alter table xxxx
新增字段:
ALTER TABLE 表名 ADD 列名列类型[约束条件];
alter table t user add test text default "呵呵呵“
修改字段的类型:
ALTER TABLE 名 modify 列名列类型[约束条件];
alter table t user add test varchar();
修改字段名称:
ALTER TABLE 表名 change 日列名 新的列表名 列类型[约束条件];
删除字段:
alter table 表名称 drop 字段名称;
修改表名称:
ALTER TABLE 表名 RENAME 新表名
rename table 表名称 to 新表名;
alter table emp rename employee