-
添加外键约束
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name](index_col_name,...) reference_definition
查看全部 -
添加唯一约束
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...)
查看全部 -
添加主键约束
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
查看全部 -
删除列
ALTER TABLE tbl_name DROP [COLUMN] col_name
查看全部 -
添加单列
ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]
查看全部 -
表级约束与列级约束
对一个数据列建立的约束,称为列级约束。
对多个数据列建立的约束,称为表级约束。
列级约束既可以在列定义时声明,也可以在列定义后声明。
表级约束只能在列定义后声明。
查看全部 -
mysql> CREATE TABLE users1( -> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> username VARCHAR(10) NOT NULL, -> pid SMALLINT UNSIGNED, -> FOREIGN KEY (pid) REFERENCES provinces (id) ON DELETE CASCADE -> ); Query OK, 0 rows affected (0.12 sec) mysql> SHOW CREATE TABLE users1; +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | users1 | CREATE TABLE `users1` ( `id` smallint unsigned NOT NULL AUTO_INCREMENT, `username` varchar(10) NOT NULL, `pid` smallint unsigned DEFAULT NULL, PRIMARY KEY (`id`), KEY `pid` (`pid`), CONSTRAINT `users1_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `provinces` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> INSERT provinces(PNAME) VALUES('A'); Query OK, 1 row affected (0.00 sec) mysql> INSERT provinces(PNAME) VALUES('B'); Query OK, 1 row affected (0.01 sec) mysql> INSERT provinces(PNAME) VALUES('C'); Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM provinces; +----+-------+ | id | pname | +----+-------+ | 1 | A | | 2 | B | | 3 | C | +----+-------+ 3 rows in set (0.00 sec)
mysql> INSERT users1(username, pid) VALUES('Tom', 3); Query OK, 1 row affected (0.01 sec) mysql> INSERT users1(username, pid) VALUES('John', 7); ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`users1`, CONSTRAINT `users1_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `provinces` (`id`) ON DELETE CASCADE
mysql> INSERT users1(username, pid) VALUES('John', 1); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM users1; +----+----------+------+ | id | username | pid | +----+----------+------+ | 1 | Tom | 3 | | 3 | Rose | 3 | | 4 | John | 1 | +----+----------+------+ 3 rows in set (0.00 sec)
mysql> DELETE FROM provinces WHERE id = 3; Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM provinces; +----+-------+ | id | pname | +----+-------+ | 1 | A | | 2 | B | +----+-------+ 2 rows in set (0.00 sec) mysql> SELECT * FROM users1; +----+----------+------+ | id | username | pid | +----+----------+------+ | 4 | John | 1 | +----+----------+------+ 1 row in set (0.00 sec)
查看全部 -
use test:打开系统的test数据库
查看全部 -
外键约束的参照操作
CASCADE:从父表删除或更新且自动删除或更新子表中匹配的行
SET NULL:从父表删除或更新行,并设置子表中的外键列为 NULL。如果使用该选项,必须保证子表列没有指定 NOT NULL
RESTRICT:拒绝对父表的删除或更新操作。
NO ACTION:标准 SQL 的关键字,在 MySQL 中与 RESTRICT 相同
查看全部 -
哈哈哈查看全部
-
mysql> USE test; Database changed mysql> CREATE TABLE provinces( -> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> pname VARCHAR(20) NOT NULL -> ); Query OK, 0 rows affected (0.14 sec)
mysql> SHOW CREATE TABLE provinces; +-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | provinces | CREATE TABLE `provinces` ( `id` smallint unsigned NOT NULL AUTO_INCREMENT, `pname` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> CREATE TABLE users( -> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> username VARCHAR(10) NOT NULL, -> pid BIGINT, -> FOREIGN KEY (pid) REFERENCES provinces (id) -> ); ERROR 1005 (HY000): Can't create table 'test.users' (errno:150) mysql> CREATE TABLE users( -> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> username VARCHAR(10) NOT NULL, -> pid SMALLINT, -> FOREIGN KEY (pid) REFERENCES provinces (id) -> ); ERROR 1005 (HY000):Can't create table 'test.users' (errno:150) mysql> CREATE TABLE users( -> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> username VARCHAR(10) NOT NULL, -> pid SMALLINT UNSIGNED, -> FOREIGN KEY (pid) REFERENCES provinces (id) -> ); Query OK, O rows affected (0.10 sec)
mysql> SHOW INDEXES FROM provinces; +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ | provinces | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | YES | NULL | +-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ 1 row in set (0.11 sec)
mysql> SHOW INDEXES FROM provinces\G; *************************** 1. row *************************** Table: provinces Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: Visible: YES Expression: NULL 1 row in set (0.00 sec) ERROR: No query specified
mysql> SHOW INDEXES FROM users\G; *************************** 1. row *************************** Table: users Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: Visible: YES Expression: NULL *************************** 2. row *************************** Table: users Non_unique: 1 Key_name: pid Seq_in_index: 1 Column_name: pid Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: Visible: YES Expression: NULL 2 rows in set (0.01 sec) ERROR: No query specified
mysql> SHOW CREATE TABLE users; +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | users | CREATE TABLE `users` ( `id` smallint unsigned NOT NULL AUTO_INCREMENT, `username` varchar(10) NOT NULL, `pid` smallint unsigned DEFAULT NULL, PRIMARY KEY (`id`), KEY `pid` (`pid`), CONSTRAINT `users_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `provinces` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
查看全部 -
编辑数据表的默认存储引擎
MySQL配置文件
default-storage-engine=INNODB
查看全部 -
外键约束的要求
父表和子表必须使用相同的存储引擎,而且禁止使用临时表。
数据表的存储引擎只能为 InnoDB。
外键列和参照列必须具有相似的数据类型。其中数字的长或否有符号位必须相同;而字符的长度则可以不同。
外键列和参照列必须创建索引。如果外键列不存在索引的话,MySQL将自动创建索引。
查看全部 -
FOREIGN KEYp
保持数据一致性,完整性。
实现一对一或一对多关系。
查看全部 -
约束
约束保证数据的完整性和一致性。
约束分为表级约束和列级约束。
约束类型包括:
NOT NULL(非空约束)
PRIMARY KEY(主键约束)
UNIQUE KEY(唯一约束)
DEFAULT(默认约束)
FOREIGN KEY(外键约束)
查看全部
举报