目录
1. 表的约束问题:
主键约束:
案例:
非空约束
唯一约束:
默认值约束:
案例:
2.表的结构
前提:接上一张博客内容经行操作
1. 表的约束问题:
用来保证数据插入的安全性、完整性、正确性
主健,外键,唯一,默认值,非空,检查
主键约束:
一旦某个字段被主键约束修饰,该字段就是主键字段
1、该字段的值不允许重复
2、该字段不能为空
该字段就是用来保证唯一的
案例:
mysql> drop table if exists t_user; # 删除表
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);
Query OK, 0 rows affected (0.02 sec) # auto_increment 自增长的整数类型mysql> create table t_user (id int auto_increment, name varchar(225), age int, primary key(id)); # 还可以写为:
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;
+----+------+------+ | 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 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 ()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> select *from t_user;
+----+----------+----------+------+--------+
| id | name | nickname | age | gender |
+----+----------+----------+------+--------+
| 1 | liudehua | 天王 | 58 | 男 |
+----+----------+----------+------+--------+
1 row in set (0.00 sec)
2.表的结构
表的结构:
第一次使用表结构,是在创建表的时候,制定了对应表的结构
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