MySQL create语句


1. create database:

mysql> create database course2;
Query OK, 1 row affected (0.03 sec)

mysql> create database course2;
ERROR 1007 (HY000): Can't create database 'course2'; database exists
mysql> create database if not exists course2;
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> use course2
Database changed

mysql> drop database course2;
Query OK, 0 rows affected (0.01 sec)

2. create table:

mysql> use course;
Database changed
mysql> create table students(id int,name varchar(20),gender int);
Query OK, 0 rows affected (0.14 sec)

mysql> create table students2(id int);
Query OK, 0 rows affected (0.08 sec)

mysql> use course;
Database changed
mysql> create table monitor.students2(id int);
Query OK, 0 rows affected (0.09 sec)

mysql> use monitor;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_monitor |
+-------------------+
| students2         |
+-------------------+
1 row in set (0.01 sec)

• Temporary关键词表示创建的是临时表,临时表仅对本链接可见,另外的数据库链接不可见,当本链接断开时,临时表也自动被drop掉

mysql> create temporary table students_tmp(id int,name varchar(10));
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+-------------------+
| Tables_in_monitor |
+-------------------+
| students2         |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from students_tmp;   #==>本地终端连接:
Empty set (0.00 sec)

mysql> select * from students_tmp;   #==>另一个终端连接:
ERROR 1146 (42S02): Table 'course.students_tmp' doesn't exist

mysql> insert into students_tmp values(1,'a');
Query OK, 1 row affected (0.00 sec)

mysql> select * from students_tmp;
+------+------+
| id   | name |
+------+------+
|    1 | a    |
+------+------+
1 row in set (0.00 sec)

mysql> exit
Bye

[root@mysql
-master ~]# mysql -u root -p mysql> use course; Database changed mysql> select * from students_tmp; ERROR 1146 (42S02): Table 'course.students_tmp' doesn't exist

• Like关键词表示基于另外一个表的定义复制一个新的空表,空表上的字段属性和索引都和原表相同

mysql> use course;
Database changed
mysql> select * from students;
Empty set (0.00 sec)

mysql> show tables;
+------------------+
| Tables_in_course |
+------------------+
| students         |
| students2        |
+------------------+
2 rows in set (0.01 sec)

mysql> insert into students values(1,'a'),(2,'b');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> desc students;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | YES  |     | NULL    |       |
| name   | varchar(20) | YES  |     | NULL    |       |
| gender | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> insert into students values(1,'a',1),(2,'b',1);
Query OK, 2 rows affected (0.06 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from students;
+------+------+--------+
| id   | name | gender |
+------+------+--------+
|    1 | a    |      1 |
|    2 | b    |      1 |
+------+------+--------+
2 rows in set (0.00 sec)

mysql> create table students_copy like students;
Query OK, 0 rows affected (0.10 sec)

mysql> desc students_copy;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | YES  |     | NULL    |       |
| name   | varchar(20) | YES  |     | NULL    |       |
| gender | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> desc students;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | YES  |     | NULL    |       |
| name   | varchar(20) | YES  |     | NULL    |       |
| gender | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from students;
+------+------+--------+
| id   | name | gender |
+------+------+--------+
|    1 | a    |      1 |
|    2 | b    |      1 |
+------+------+--------+
2 rows in set (0.00 sec)

mysql> select * from students_copy;
Empty set (0.00 sec)

mysql> alter table students add primary key(id);
Query OK, 0 rows affected (0.33 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> create index idx_1 on students(name);
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table students;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                           |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `gender` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_1` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> create table students_copy like students;
Query OK, 0 rows affected (0.14 sec)

mysql> show create table students_copy;
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table         | Create Table                                                                                                                                                                                                                                |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students_copy | CREATE TABLE `students_copy` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `gender` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_1` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

mysql> CREATE TABLE `students_copy2` (
    ->   `id` int(11) NOT NULL,
    ->   `name` varchar(20) DEFAULT NULL,
    ->   `gender` int(11) DEFAULT NULL,
    ->   PRIMARY KEY (`id`),
    ->   KEY `idx_1` (`name`)
    -> );
Query OK, 0 rows affected (0.16 sec)

• Create table … as select语句表示创建表的同时将select的查询结果数据插入到表中,但索引和主外键信息都不会同步过来

mysql> drop table students_copy2;
Query OK, 0 rows affected (0.08 sec)

mysql> select * from students where id=1;
+----+------+--------+
| id | name | gender |
+----+------+--------+
|  1 | a    |      1 |
+----+------+--------+
1 row in set (0.00 sec)

mysql> create table students_copy2 as select * from students where id=1;
Query OK, 1 row affected (0.10 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from students_copy2;
+----+------+--------+
| id | name | gender |
+----+------+--------+
|  1 | a    |      1 |
+----+------+--------+
1 row in set (0.00 sec)

mysql> show create table students_copy2;
+----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table          | Create Table                                                                                                                                                                                   |
+----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students_copy2 | CREATE TABLE `students_copy2` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `gender` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

• Data_type表示定义的字段类型,后续会有详细介绍
• Not null/null表示字段是否允许为空,默认为null表示允许为空,not null表示需要对此字段明确数值,或者要有默认值,否则报错
• Default表示设置字段的默认值

mysql> desc students;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(20) | YES  | MUL | NULL    |       |
| gender | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> show create table students;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                           |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `gender` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_1` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> desc students;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(20) | YES  | MUL | NULL    |       |
| gender | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into students(name,gender) values('d',1);
ERROR 1364 (HY000): Field 'id' doesn't have a default value
mysql> insert into students(id) values(5);
Query OK, 1 row affected (0.04 sec)

mysql> select * from students;
+----+------+--------+
| id | name | gender |
+----+------+--------+
|  1 | a    |      1 |
|  2 | b    |      1 |
|  5 | NULL |   NULL |
+----+------+--------+
3 rows in set (0.00 sec)

mysql> alter table students modify name varchar(20) not null;
ERROR 1138 (22004): Invalid use of NULL value
mysql> delete from students where id=5;
Query OK, 1 row affected (0.04 sec)

mysql> alter table students modify name varchar(20) not null;
Query OK, 0 rows affected (0.23 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc students;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(20) | NO   | MUL | NULL    |       |
| gender | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> insert into students(id,name) values(5,'c');
Query OK, 1 row affected (0.06 sec)

mysql> insert into students(id) values(6);
ERROR 1364 (HY000): Field 'name' doesn't have a default value

mysql> show create table students;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                       |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
  `id` int(11) NOT NULL,
  `name` varchar(20) NOT NULL,
  `gender` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_1` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> drop table students;
Query OK, 0 rows affected (0.15 sec)

mysql> CREATE TABLE `students` (
    ->   `id` int(11) default 1,
    ->   `name` varchar(20) default 'abc',
    ->   `gender` int(11),
    ->   PRIMARY KEY (`id`),
    ->   KEY `idx_1` (`name`)
    -> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.10 sec)

mysql> desc students;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | 1       |       |
| name   | varchar(20) | YES  | MUL | abc     |       |
| gender | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into students(gender) values(1);
Query OK, 1 row affected (0.05 sec)

mysql> select * from students;
+----+------+--------+
| id | name | gender |
+----+------+--------+
|  1 | abc  |      1 |
+----+------+--------+
1 row in set (0.00 sec)

mysql> insert into students(gender) values(1);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> insert into students(id,gender) values(2,1);
Query OK, 1 row affected (0.10 sec)

mysql> select * from students;
+----+------+--------+
| id | name | gender |
+----+------+--------+
|  1 | abc  |      1 |
|  2 | abc  |      1 |
+----+------+--------+
2 rows in set (0.00 sec)

• Auto_increment表示字段为整数或者浮点数类型的value+1递增数值,value为当前表中该字段最大的值,默认是从1开始递增;一个表中只容许有一个自增字段,且该字段必须有key属性,不能含有default属性,且插入负值会被当成很大的正数

mysql> drop table students;
Query OK, 0 rows affected (0.14 sec)

mysql> create table students(id int auto_increment,name varchar(20));
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
mysql> create table students(id int primary key auto_increment,name varchar(20));
Query OK, 0 rows affected (0.13 sec)

mysql> insert into students(name) values('a');
Query OK, 1 row affected (0.06 sec)

mysql> insert into students(name) values('b');
Query OK, 1 row affected (0.08 sec)

mysql> insert into students(name) values('c');
Query OK, 1 row affected (0.05 sec)

mysql> insert into students(name) values('d');
Query OK, 1 row affected (0.05 sec)

mysql> select * from students;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
+----+------+
4 rows in set (0.00 sec)

mysql> insert into students(id,name) values(10,'d');
Query OK, 1 row affected (0.08 sec)

mysql> select * from students;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
| 10 | d    |
+----+------+
5 rows in set (0.00 sec)

mysql> insert into students(name) values('e');
Query OK, 1 row affected (0.06 sec)

mysql> select * from students;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
| 10 | d    |
| 11 | e    |
+----+------+
6 rows in set (0.00 sec)

• Column_format目前仅在ndb存储引擎的表上有用,表示该字段的存储类型是fixed, dynamic或者是default
• Storage目前也仅在ndb存储引擎的表上有用
• Constraint表示为主键、唯一键、外键等约束条件命名,如果没有命名则MySQL会默认给一个
• Primary key表示该字段为主键,主键字段必须唯一,必须非空,一个表中只能有一个主键,主键可以包含一个或多个字段
• Key/index表示索引字段
• Unique表示该字段为唯一属性字段,且允许包含多个null值
• Foreign key表示该字段为外键字段

mysql> desc students;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> show create table students;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                   |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> create index idx_1 on students(name);
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table students;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                           |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_1` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

mysql> delete from students;
Query OK, 6 rows affected (0.06 sec)

mysql> create unique index idx_2 on students(name);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table students;
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                          |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_2` (`name`),
  KEY `idx_1` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> insert into students(name) values('a');
Query OK, 1 row affected (0.05 sec)

mysql> insert into students(name) values('b');
Query OK, 1 row affected (0.06 sec)

mysql> insert into students(name) values('a');
ERROR 1062 (23000): Duplicate entry 'a' for key 'idx_2'

mysql> insert into students(id) values(20);
Query OK, 1 row affected (0.10 sec)

mysql> select * from students;
+----+------+
| id | name |
+----+------+
| 20 | NULL |
| 12 | a    |
| 13 | b    |
+----+------+
3 rows in set (0.01 sec)

mysql> insert into students(id) values(21);
Query OK, 1 row affected (0.09 sec)

mysql> select * from students;
+----+------+
| id | name |
+----+------+
| 20 | NULL |
| 21 | NULL |
| 12 | a    |
| 13 | b    |
+----+------+
4 rows in set (0.00 sec)

mysql> insert into students values(22,'');
Query OK, 1 row affected (0.03 sec)

mysql> insert into students values(23,'');
ERROR 1062 (23000): Duplicate entry '' for key 'idx_2'

mysql> alter table students add gender int;
Query OK, 0 rows affected (0.18 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc students;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| name   | varchar(20) | YES  | UNI | NULL    |                |
| gender | int(11)     | YES  |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> create unique index idx_3 on students(gender);
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table students;
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                                                                            |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `gender` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_2` (`name`),
  UNIQUE KEY `idx_3` (`gender`),
  KEY `idx_1` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> drop index idx_2 on students;
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> drop index idx_3 on students;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> create unique index idx_3 on students(name,gender);
Query OK, 0 rows affected (0.17 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table students;
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                                                    |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `gender` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_3` (`name`,`gender`),
  KEY `idx_1` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from students;
+----+------+--------+
| id | name | gender |
+----+------+--------+
| 20 | NULL |   NULL |
| 21 | NULL |   NULL |
| 22 |      |   NULL |
| 12 | a    |   NULL |
| 13 | b    |   NULL |
+----+------+--------+
5 rows in set (0.00 sec)

mysql> insert into students values(23,'a',1);
Query OK, 1 row affected (0.08 sec)

mysql> update students set gender=1 where id=12;
ERROR 1062 (23000): Duplicate entry 'a-1' for key 'idx_3'
mysql> create table course(id int primary key,course_name varchar(10));
Query OK, 0 rows affected (0.18 sec)

mysql> insert into course values(1,'AA'),(2,'BB');
Query OK, 2 rows affected (0.10 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> desc course;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id          | int(11)     | NO   | PRI | NULL    |       |
| course_name | varchar(10) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> create table std_cour_relation(sid int,course_id int);
Query OK, 0 rows affected (0.17 sec)

mysql> select * from students;
+----+------+--------+
| id | name | gender |
+----+------+--------+
| 20 | NULL |   NULL |
| 21 | NULL |   NULL |
| 22 |      |   NULL |
| 12 | a    |   NULL |
| 23 | a    |      1 |
| 13 | b    |   NULL |
+----+------+--------+
6 rows in set (0.00 sec)

mysql> insert into std_cour_relation values(12,1);
Query OK, 1 row affected (0.06 sec)

mysql> insert into std_cour_relation values(13,2);
Query OK, 1 row affected (0.09 sec)

mysql> insert into std_cour_relation values(100,2);
Query OK, 1 row affected (0.05 sec)

mysql> drop table std_cour_relation;
Query OK, 0 rows affected (0.13 sec)

mysql> create table std_cour_relation(sid int,course_id int,constraint for_1 foreign key (sid) references students(id));
Query OK, 0 rows affected (0.10 sec)

mysql> drop table std_cour_relation;
Query OK, 0 rows affected (0.05 sec)

mysql> create table std_cour_relation(sid int,course_id int,
-> constraint for_1 foreign key (sid) references students(id),
-> constraint for_2 foreign key (course_id) references course(id)); Query OK, 0 rows affected (0.11 sec) mysql> show create table std_cour_relation; +-------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | std_cour_relation | CREATE TABLE `std_cour_relation` ( `sid` int(11) DEFAULT NULL, `course_id` int(11) DEFAULT NULL, KEY `for_1` (`sid`), KEY `for_2` (`course_id`), CONSTRAINT `for_1` FOREIGN KEY (`sid`) REFERENCES `students` (`id`), CONSTRAINT `for_2` FOREIGN KEY (`course_id`) REFERENCES `course` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +-------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) mysql> insert into std_cour_relation values(12,1); Query OK, 1 row affected (0.11 sec) mysql> insert into std_cour_relation values(100,1); ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`course`.`std_cour_relation`, CONSTRAINT `for_1` FOREIGN KEY (`sid`) REFERENCES `students` (`id`)) mysql> select * from std_cour_relation; +------+-----------+ | sid | course_id | +------+-----------+ | 12 | 1 | +------+-----------+ 1 row in set (0.01 sec) mysql> delete from students where id=12; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`course`.`std_cour_relation`, CONSTRAINT `for_1` FOREIGN KEY (`sid`) REFERENCES `students` (`id`)) mysql> delete from std_cour_relation where sid=12; Query OK, 1 row affected (0.13 sec) mysql> delete from students where id=12; Query OK, 1 row affected (0.02 sec)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM