MySQL 约束与自增长
mysql约束
基本介绍
约束用于确保数据库的数据满足特定的商业规则。在mysql中,约束包括: not null、unique,primary key,foreign key,和check五种。
primary key(主键)-基本使用
- primary key不能重复而且不能为null。
- 一张表最多只能有一个主键,但可以是复合主键主键的指定方式有两种
- 直接在字段名后指定:字段名primakry key在表定义最后写primary key(列名);
- 使用desc表名,可以看到primary key的情况.
- 在实际开发中,每个表往往都会设计一个主键.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
CREATE TABLE t17( id INT PRIMARY KEY, `name` VARCHAR(32), email VARCHAR(32));
INSERT INTO t17 VALUES(1,'jack','jack@sohu.com') INSERT INTO t17 VALUES(2,'tom','tom@sohu.com') INSERT INTO t17( VALUES(1,'frx','frx@sohu.com') SELECT * FROM t17
INSERT INTO t17( VALUES(NULL,'frx','frx@sohu.com')
CREATE TABLE t18( id INT , `name` VARCHAR(32) , email VARCHAR(32), PRIMARY KEY(id,`name`));
INSERT INTO t18 VALUES(1,'tom','tom@sohu.com'); INSERT INTO t18 VALUES(1,'jack','jack@sohu.com'); SELECT * FROM t18
CREATE TABLE t19( id INT, `name` VARCHAR(32) PRIMARY KEY, email VARCHAR(32));
CREATE TABLE t20( id INT, `name` VARCHAR(32) , email VARCHAR(32), PRIMARY KEY(`name`));
DESC t20 DESC t18
|
not null和unique(唯一)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
CREATE TABLE t21( id INT UNIQUE, `name` VARCHAR(32), email VARCHAR(32) ); INSERT INTO t21 VALUES(1,'jack','jack@sohu.com'); INSERT INTO t21 VALUES(1,'tom','tom@sohu.com');
INSERT INTO t21 VALUES(NULL,'tom','tom@sohu.com');
CREATE TABLE t22( id INT UNIQUE, `name` VARCHAR(32) UNIQUE, email VARCHAR(32) ); DESC t22
|
foreign key(外键)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
CREATE TABLE my_class( id INT PRIMARY KEY, `name` VARCHAR(32) NOT NULL DEFAULT '');
CREATE TABLE my_stu( id INT PRIMARY KEY, `name` VARCHAR(32) NOT NULL DEFAULT '', class_id INT, FOREIGN KEY (class_id) REFERENCES my_class(id))
INSERT INTO my_class VALUES(100,'java'),(200,'web'); SELECT * FROM my_class
INSERT INTO my_stu VALUES(1,'tom',100); INSERT INTO my_stu VALUES(2,'jack',200); INSERT INTO my_stu VALUES(4,'marry',NULL); INSERT INTO my_stu VALUES(3,'frx',300); SELECT * FROM my_stu SELECT * FROM my_class
DELETE FROM my_class WHERE id=100
|
check
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
CREATE TABLE t23( id INT PRIMARY KEY, `name` VARCHAR(32), sex VARCHAR(6) CHECK (sex IN('man','woman')), sal DOUBLE CHECK(sal>1000 AND sal<2000));
INSERT INTO t23 VALUES(1,'jack','mid',1); SELECT * FROM t23
|
商店售货系统表设计案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| CREATE TABLE goods_( goods_id INT PRIMARY KEY, goods_name VARCHAR(64) NOT NULL DEFAULT '', unitprice DECIMAL(10,2) NOT NULL CHECK (unitprice>=1.0 AND unitprice <=9999.99), category INT NOT NULL DEFAULT 0, provider VARCHAR(64) NOT NULL); CREATE TABLE customer( customer_id CHAR(8) PRIMARY KEY, `name` VARCHAR(64) NOT NULL DEFAULT '', address VARCHAR(64) NOT NULL DEFAULT '', email VARCHAR(64) UNIQUE NOT NULL, sex ENUM('男','女')NOT NULL, card_Id CHAR(18)); CREATE TABLE purchase( order_id INT UNSIGNED PRIMARY KEY, customer_id CHAR(8) NOT NULL DEFAULT '', goods_id INT NULL DEFAULT 0, nums INT NOT NULL DEFAULT 0, FOREIGN KEY (customer_id) REFERENCES customer(customer_id), FOREIGN KEY (goods_id) REFERENCES goods_(goods_id)); DESC customer DESC goods_ DESC purchase
|
自增长
自增长基本介绍
自增长使用细节
- 一般来说自增长是和primary key配合使用的
- 自增长也可以单独使用[但是需要配合一个unique]
- 自增长修饰的字段为整数型的(虽然小数也可以但是非常非常少这样使用)
- 自增长默认从1开始,你也可以通过如下命令修改altertable表名auto increment=新的开始值;
- 如果你添加数据时,给自增长字段(列)指定的有值,则以指定的值为准,如果指定了自增长,一般来说,就按照自增长的规则来添加数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
CREATE TABLE t24( id INT PRIMARY KEY AUTO_INCREMENT, email VARCHAR(32) NOT NULL DEFAULT '', `name` VARCHAR(32) NOT NULL DEFAULT ''); DESC t24
INSERT INTO t24 VALUES(NULL,'jack@qq.com','jack') INSERT INTO t24 (email,`name`)VALUES('jack@qq.com','jack');
SELECT * FROM t24
ALTER TABLE t25 AUTO_INCREMENT =100 CREATE TABLE t25( id INT PRIMARY KEY AUTO_INCREMENT, email VARCHAR(32) NOT NULL DEFAULT '', `name` VARCHAR(32) NOT NULL DEFAULT ''); INSERT INTO t25 VALUES(NULL,'jack@qq.com','jack'); INSERT INTO t25 VALUES(666,'jack@qq.com','jack'); INSERT INTO t25 VALUES(NULL,'mary@qq.com','mary'); SELECT * FROM t25;
|