Mysql约束

λ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.30 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use company;
Database changed
mysql> desc emp;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| EMPNO    | int         | YES  |     | NULL    |       |
| ENAME    | varchar(10) | YES  |     | NULL    |       |
| JOB      | varchar(9)  | YES  |     | NULL    |       |
| MGR      | int         | YES  |     | NULL    |       |
| HIREDATE | date        | YES  |     | NULL    |       |
| salary   | double      | YES  |     | NULL    |       |
| COMM     | double      | YES  |     | NULL    |       |
| DEPTNO   | int         | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> select * from emp;
+-------+--------+-----------+------+------------+--------+------+--------+
| EMPNO | ENAME  | JOB       | MGR  | HIREDATE   | salary | COMM | DEPTNO |
+-------+--------+-----------+------+------------+--------+------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-01-17 |   1000 | NULL |     20 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 |   1800 |  300 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 |   1450 |  500 |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 |   2975 | NULL |     20 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 |   1450 | 1400 |     30 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 |   2850 | NULL |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 |   2450 | NULL |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-07-13 |   3000 | NULL |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-07 |   5000 | NULL |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 |   1700 |    0 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-07-13 |   1300 | NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |   1150 | NULL |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 |   3000 | NULL |     20 |
|  7934 | MILLER | CLEERK    | 7782 | 1982-02-23 |   1500 | NULL |     10 |
+-------+--------+-----------+------+------------+--------+------+--------+
14 rows in set (0.01 sec)
mysql>  ALTER TABLE emp ADD CONSTRAINT pk_emp_EMPNO PRIMARY KEY (EMPNO);--设置empno为主键
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc emp;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| EMPNO    | int         | NO   | PRI | NULL    |       |
| ENAME    | varchar(10) | YES  |     | NULL    |       |
| JOB      | varchar(9)  | YES  |     | NULL    |       |
| MGR      | int         | YES  |     | NULL    |       |
| HIREDATE | date        | YES  |     | NULL    |       |
| salary   | double      | YES  |     | NULL    |       |
| COMM     | double      | YES  |     | NULL    |       |
| DEPTNO   | int         | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> select ename,salary from emp where salary = 1000 or ename = 'ALLEN' and deptno = 20;--先执行or,再执行and
+-------+--------+
| ename | salary |
+-------+--------+
| SMITH |   1000 |
+-------+--------+
1 row in set (0.00 sec)
mysql>  select ename, salary FROM emp WHERE salary = 1000 OR ename = 'JONES' AND deptno = 20;
+-------+--------+
| ename | salary |
+-------+--------+
| SMITH |   1000 |
| JONES |   2975 |
+-------+--------+
2 rows in set (0.00 sec)
mysql> select ename, salary FROM emp WHERE salary = 800 AND ename = 'JONES' OR deptno = 20;
+-------+--------+
| ename | salary |
+-------+--------+
| SMITH |   1000 |
| JONES |   2975 |
| SCOTT |   3000 |
| ADAMS |   1300 |
| FORD  |   3000 |
+-------+--------+
5 rows in set (0.00 sec)
select ename, sal, deptno FROM emp WHERE sal = 2975 AND ename = 'JONES' OR deptno = 30;
mysql> select ename, salary, deptno FROM emp WHERE salary = 2975 AND ename = 'JONES' OR deptno = 30;

mysql> select ename, salary, deptno FROM emp WHERE (salary = 2975 AND ename = 'JONES') OR deptno = 30;
+--------+--------+--------+
| ename  | salary | deptno |
+--------+--------+--------+
| ALLEN  |   1800 |     30 |
| WARD   |   1450 |     30 |
| JONES  |   2975 |     20 |
| MARTIN |   1450 |     30 |
| BLAKE  |   2850 |     30 |
| TURNER |   1700 |     30 |
| JAMES  |   1150 |     30 |
+--------+--------+--------+
7 rows in set (0.00 sec)
mysql> show create table emp;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table




         |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| emp   | CREATE TABLE `emp` (
  `EMPNO` int NOT NULL COMMENT '员工编号',
  `ENAME` varchar(10) DEFAULT NULL COMMENT '员工姓名',
  `JOB` varchar(9) DEFAULT NULL COMMENT '员工职位',
  `MGR` int DEFAULT NULL COMMENT '领导编号',
  `HIREDATE` date DEFAULT NULL COMMENT '入职日期',
  `salary` double DEFAULT NULL,
  `COMM` double DEFAULT NULL COMMENT '奖金',
  `DEPTNO` int DEFAULT NULL COMMENT '部门编号',
  PRIMARY KEY (`EMPNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci                           |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> --创建表时添加主键约束
mysql> create table student(
    -> id int primary key,
    -> name varchar(14),
    -> age int);
Query OK, 0 rows affected (0.06 sec)

mysql> show create table student;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table

                                   |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
  `id` int NOT NULL,
  `name` varchar(14) DEFAULT NULL,
  `age` int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into student values(1,'王雪',20);
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO student(id) VALUES(1);
ERROR 1062 (23000): Duplicate entry '1' for key 'student.PRIMARY'
mysql> INSERT INTO student(name, age) VALUES('张三', 18);
ERROR 1364 (HY000): Field 'id' doesn't have a default value
mysql> INSERT INTO student(id, name, age) VALUES('张三', 18);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql>  INSERT INTO student(id, name, age) VALUES(2,'张三', 18);
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 王雪   |   20 |
|  2 | 张三   |   18 |
+----+--------+------+
2 rows in set (0.00 sec)
mysql> insert into student(id) values (3);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  1 | 王雪   |   20 |
|  2 | 张三   |   18 |
|  3 | NULL   | NULL |
+----+--------+------+
3 rows in set (0.00 sec)
mysql>  DROP TABLE student;
Query OK, 0 rows affected (0.01 sec)

mysql> create table student(
    -> id int,
    -> name varchar(20),
    -> CONSTRAINT pk_id PRIMARY KEY (id)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into student values (1,'李四');
Query OK, 1 row affected (0.00 sec)

mysql> --删除主键
mysql> alter table student drop primary key;
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0
mysql> create table student(
    -> id int primary key AUTO_INCREMENT,
    -> age int UNIQUE AUTO_INCREMENT
    -> );
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
mysql> create table student(
    -> id int primary key auto_increment,
    -> name varchar(20)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> insert into student(name) values ('张三');
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
+----+--------+
1 row in set (0.00 sec)

--再次插入名字为张三
mysql> insert into student(name) values ('张三');
Query OK, 1 row affected (0.01 sec)

--随着ID的自增第二个张三也会插入表中
mysql> select * from student;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  2 | 张三   |
+----+--------+
2 rows in set (0.00 sec)
--查询的几种方式
mysql> select id,name from student;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  2 | 张三   |
+----+--------+
2 rows in set (0.00 sec)

mysql> select id as '编号',name as '姓名' from student;
+--------+--------+
| 编号   | 姓名   |
+--------+--------+
|      1 | 张三   |
|      2 | 张三   |
+--------+--------+
2 rows in set (0.00 sec)

mysql> select id '编号', name '姓名' from student;
+--------+--------+
| 编号   | 姓名   |
+--------+--------+
|      1 | 张三   |
|      2 | 张三   |
+--------+--------+
2 rows in set (0.00 sec)

mysql> select id '编号', name '姓名' from student AS s;
+--------+--------+
| 编号   | 姓名   |
+--------+--------+
|      1 | 张三   |
|      2 | 张三   |
+--------+--------+
2 rows in set (0.00 sec)

mysql> select id '编号', name '姓名' from student s;
+--------+--------+
| 编号   | 姓名   |
+--------+--------+
|      1 | 张三   |
|      2 | 张三   |
+--------+--------+
2 rows in set (0.00 sec)

mysql>  select id '编号', name '姓名' from student ;
+--------+--------+
| 编号   | 姓名   |
+--------+--------+
|      1 | 张三   |
|      2 | 张三   |
+--------+--------+
2 rows in set (0.00 sec)

外键

mysql> -- 将 dept 表的 deptno 设置为主键
mysql> ALTER TABLE dept ADD PRIMARY KEY(deptno);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> CREATE TABLE employee(
    -> id INT AUTO_INCREMENT PRIMARY KEY,
    -> name VARCHAR(20),
    -> dept_no INT, -- 外键列
    -> CONSTRAINT FK_DEPT_NO FOREIGN KEY(dept_no) REFERENCES dept(deptno)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> -- 有外键约束后,约束列的值只能是 NULL 或者 主表中被引用的列的值
mysql> SELECT * FROM dept;
+--------+------------+----------+
| DEPTNO | DNAME      | LOC      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.00 sec)

mysql> INSERT INTO employee(name, dept_no) VALUES('JONES', 1);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`scott`.`employee`, CONSTRAINT `FK_DEPT_NO` FOREIGN KEY (`dept_no`) REFERENCES `dept` (`DEPTNO`))
mysql> INSERT INTO employee(name, dept_no) VALUES('JONES', 10);
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO employee(name) VALUES('JERRY');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM employee;
+----+-------+---------+
| id | name  | dept_no |
+----+-------+---------+
|  2 | JONES |      10 |
|  3 | JERRY |    NULL |
+----+-------+---------+
2 rows in set (0.00 sec)

mysql> DELETE FROM employee WHERE id = 3;
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM employee;
+----+-------+---------+
| id | name  | dept_no |
+----+-------+---------+
|  2 | JONES |      10 |
+----+-------+---------+
1 row in set (0.00 sec)


mysql> INSERT INTO dept VALUES(50, '研发部', '西安');
Query OK, 1 row affected (0.01 sec)

mysql> -- 删除主表中未被引用的数
mysql> DELETE FROM dept WHERE deptno = 50;
Query OK, 1 row affected (0.01 sec)

mysql> -- 删除主表中被引用的数据
mysql> DELETE FROM dept WHERE deptno = 10;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`scott`.`employee`, CONSTRAINT `FK_DEPT_NO` FOREIGN KEY (`dept_no`) REFERENCES `dept` (`DEPTNO`))
mysql> -- 更新主表数据时也一样
mysql> UPDATE dept SET deptno = 11 WHERE deptno = 10;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`scott`.`employee`, CONSTRAINT `FK_DEPT_NO` FOREIGN KEY (`dept_no`) REFERENCES `dept` (`DEPTNO`))
mysql> DROP TABLE employee;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE employee(
    -> id INT AUTO_INCREMENT PRIMARY KEY,
    -> name VARCHAR(20),
    ->  dept_no INT
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> -- 添加外键约束
mysql> ALTER TABLE employee ADD CONSTRAINT FK_DEPT_NO FOREIGN KEY (dept_no) REFERENCES dept(deptno);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> SHOW CREATE TABLE employee;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table    | Create Table

                                                                                                                | +----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | employee | CREATE TABLE `employee` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `dept_no` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_DEPT_NO` (`dept_no`),
  CONSTRAINT `FK_DEPT_NO` FOREIGN KEY (`dept_no`) REFERENCES `dept` (`DEPTNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)

mysql> -- 删除外键约束
mysql> ALTER TABLE employee DROP CONSTRAINT FK_DEPT_NO;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

唯一 非空 默认

mysql> CREATE TABLE temp(
    -> id FLOAT AUTO_INCREMENT PRIMARY KEY,
    -> name VARCHAR(20)
    -> );
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> -- 修改自增长列的起始值
mysql> ALTER TABLE temp AUTO_INCREMENT = 10;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> -- 插入值
mysql> INSERT INTO temp(name) VALUES('A');
Query OK, 1 row affected (0.01 sec)

mysql> -- 查询值
mysql> SELECT * FROM temp;
+----+------+
| id | name |
+----+------+
| 10 | A    |
+----+------+
1 row in set (0.00 sec)

mysql> -- 删除表
mysql> DROP TABLE temp;
Query OK, 0 rows affected (0.01 sec)

mysql> -- 创建表时设置自增长初始值
mysql> CREATE TABLE temp(
    -> id INT PRIMARY KEY AUTO_INCREMENT,
    -> name VARCHAR(2)
    -> ) AUTO_INCREMENT=10;
Query OK, 0 rows affected (0.01 sec)

mysql> -- 测试
mysql> INSERT INTO temp (name) VALUES('B');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM temp;
+----+------+
| id | name |
+----+------+
| 10 | B    |
+----+------+
1 row in set (0.00 sec)

mysql> show tables;
+-----------------+
| Tables_in_scott |
+-----------------+
| bonus           |
| dept            |
| emp             |
| salgrade        |
| student         |
| teacher         |
| temp            |
+-----------------+
7 rows in set (0.00 sec)

mysql> SHOW CREATE TABLE student;
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table

       |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
  `id` int NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `age` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `age` (`age`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> -- 唯一约束,表示列的值不同
mysql> -- UNIQUE KEY
mysql> DROP TABLE student;
Query OK, 0 rows affected (0.01 sec)

mysql> -- 创建学生表,id 为主键,name 唯一
mysql> CREATE TABLE student(
    -> id INT PRIMARY KEY,
    -> name VARCHAR(20) UNIQUE, -- 唯一列
    -> stu_no VARCHAR(10) COMMENT '学号'
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> -- student 表中有两个约束,一个 id 的主键约束, 另外一个是 name 的唯一约束
mysql> INSERT INTO student(id, name, stu_no) VALUES(1, 'TOM', '10001');
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO student(id, name, stu_no) VALUES(2, 'TOM', '10002');
ERROR 1062 (23000): Duplicate entry 'TOM' for key 'student.name'
mysql> INSERT INTO student(id, name, stu_no) VALUES(2, 'TOM1', '10002');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM student;
+----+------+--------+
| id | name | stu_no |
+----+------+--------+
|  1 | TOM  | 10001  |
|  2 | TOM1 | 10002  |
+----+------+--------+
2 rows in set (0.00 sec)

mysql> -- 修改数据时,数据也会受约束的影响
mysql> UPDATE student SET name = 'TOM' WHERE id = 2;
ERROR 1062 (23000): Duplicate entry 'TOM' for key 'student.name'
mysql> -- 唯一约束表示值不能重复,那么可以有多个NULL
mysql> INSERT INTO student(id, stu_no) VALUES(3, '10003');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM student;
+----+------+--------+
| id | name | stu_no |
+----+------+--------+
|  1 | TOM  | 10001  |
|  2 | TOM1 | 10002  |
|  3 | NULL | 10003  |
+----+------+--------+
3 rows in set (0.00 sec)

mysql> INSERT INTO student(id, stu_no) VALUES(4, '10004');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM student;
+----+------+--------+
| id | name | stu_no |
+----+------+--------+
|  1 | TOM  | 10001  |
|  2 | TOM1 | 10002  |
|  3 | NULL | 10003  |
|  4 | NULL | 10004  |
+----+------+--------+
4 rows in set (0.00 sec)

mysql> -- 第二种创建唯一约束
mysql> DROP TABLE student;
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE student(
    -> id INT PRIMARY KEY AUTO_INCREMENT,
    -> name VARCHAR(20),
    -> stu_no CHAR(10),
    -> CONSTRAINT uq_name UNIQUE(name)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> -- 测试
mysql> INSERT INTO student (name, stu_no) VALUES('Danny', '10001');
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO student (name, stu_no) VALUES('Danny', '10002');
ERROR 1062 (23000): Duplicate entry 'Danny' for key 'student.uq_name'
mysql> -- 将 stu_no 也设置为唯一约束的列
mysql> ALTER TABLE student ADD CONSTRAINT uq_no UNIQUE(stu_no);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> -- 删除唯一约束
mysql> ALTER TABLE student DROP CONSTRAINT uq_no;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE student ADD UNIQUE(stu_no);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> -- 没有约束名的约束如何删
mysql> ALTER TABLE student DROP CONSTRAINT stu_no;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> SHOW CREATE TABLE student;
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table

                                                    |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `stu_no` char(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> -- 之前讲过 LIKE 创建同类型的表
mysql> CREATE TABLE student_copy LIKE student;
Query OK, 0 rows affected (0.02 sec)

mysql> SHOW CREATE TABLE student_copy;
+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table        | Create Table

                                             |
+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student_copy | CREATE TABLE `student_copy` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `stu_no` char(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> -- 那么 AS 会吗?
mysql> CREATE TABLE student_copy1 AS SELECT * FROM student;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> SHOW CREATE TABLE student_copy1;
+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table         | Create Table
                                                                                                       |
+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student_copy1 | CREATE TABLE `student_copy1` (
  `id` int NOT NULL DEFAULT '0',
  `name` varchar(20) DEFAULT NULL,
  `stu_no` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> -- 默认约束。默认值
mysql> -- 默认约束能和主键约束同时存在吗?
mysql> DROP TABLE student;
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE student(
    -> id INT DEFAULT 5 PRIMARY KEY,
    -> name VARCHAR(20)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> -- 测试
mysql> INSERT INTO student(name) VALUES('A');
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM student;
+----+------+
| id | name |
+----+------+
|  5 | A    |
+----+------+
1 row in set (0.00 sec)

mysql> INSERT INTO student(name) VALUES('A');
ERROR 1062 (23000): Duplicate entry '5' for key 'student.PRIMARY'
mysql> DROP TABLE student;
Query OK, 0 rows affected (0.01 sec)

mysql> -- 非空约束和默认约束一起
mysql> CREATE TABLE student(
    -> id INT PRIMARY KEY,
    -> name VARCHAR(20) NOT NULL DEFAULT NULL
    -> );
ERROR 1067 (42000): Invalid default value for 'name'
mysql> CREATE TABLE student(
    -> id INT PRIMARY KEY,
    -> name VARCHAR(20) NOT NULL DEFAULT 'a'
    -> );
Query OK, 0 rows affected (0.01 sec)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值