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)