SQL语句建表 约束条件
Posted 我的紫霞辣辣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语句建表 约束条件相关的知识,希望对你有一定的参考价值。
约束条件
插入数据的时候可以指定字段
create table t1(id int, name char(16));
insert into t1(name,id)values("nana",1);
select * from t1;
+------+------------------+
| id | name |
+------+------------------+
| 1 | nana |
+------+------------------+
default默认值
create table t2(
id int,
name char(16),
gender enum("male","female","other")default"other"
);
desc t2;
+--------+-------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(16) | YES | | NULL | |
| gender | enum('male','female','other') | YES | | other | |
+--------+-------------------------------+------+-----+---------+-------+
insert into t2 values(1,"nana","female");
insert into t2(id,name) values(2,"lala");
select * from t2;
+------+------------------+--------+
| id | name | gender |
+------+------------------+--------+
| 1 | nana | female |
| 2 | lala | other |
+------+------------------+--------+
unique唯一
单列唯一
create table t3(
id int unique,
name char(16)
);
# id重复,数据存储失败
insert into t3 values(1,"nana"),(1,"dada");
ERROR 1062 (23000): Duplicate entry '1' for key 'id'
insert into t3 values(1,"nana"),(2,"dada");
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
desc t3;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| name | char(16) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
select * from t3;
+------+------------------+
| id | name |
+------+------------------+
| 1 | nana |
| 2 | dada |
+------+------------------+
联合唯一
create table t4(
id int, ip char(16),
port int,
unique(ip,port)
);
insert into t4 values(1,"127.0.0.1",8080);
insert into t4 values(1,"127.0.0.1",8081);
insert into t4 values(1,"127.0.0.2",8080);
# 指定联合唯一的字段重复了,无法存储数据
insert into t4 values(1,"127.0.0.1",8080);
ERROR 1062 (23000): Duplicate entry '127.0.0.1 -8080' for key 'ip'
desc t4;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| ip | char(16) | YES | MUL | NULL | |
| port | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
select * from t4;
+------+------------------+------+
| id | ip | port |
+------+------------------+------+
| 1 | 127.0.0.1 | 8080 |
| 2 | 127.0.0.1 | 8081 |
| 2 | 127.0.0.2 | 8080 |
+------+------------------+------+
primary key主键
单单从约束效果上来看primary key 等价于not null + unique,非空且唯一!!!
create table t5(id int primary key);
# 报错,插入的数据不能为null
insert into t5 values(null);
ERROR 1048 (23000): Column 'id' cannot be null
# 报错,插入的数据不能重复
insert into t5 values(1),(1);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
# 非空且唯一
insert into t5 values(1),(2);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
desc t5;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
+-------+---------+------+-----+---------+-------+
select * from t5;
+----+
| id |
+----+
| 1 |
| 2 |
+----+
primary key的属性
primary key它除了有约束效果之外,它还是Innodb存储引擎组织数据的依据,Innodb存储引擎在创建表的是后必须要有primary key。因为它类似于书的目录,能够帮助数据库提升查询效率并且也是建表的依据。
一张表中有且只有一个主键,如果你没有设置主键,那么数据库会默认从上往下搜索,直到遇到一个非空且唯一的字段将它自动升级为主键!!!主键能够帮助数据库提升查询效率!!!同时也是建表的依据(主键必须存在)
mysql> create table t6(
id int,
name char(16),
age int not null unique,
addr char(32) not null unique
);
desc t6;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(16) | YES | | NULL | |
| age | int(11) | NO | PRI | NULL | |
| addr | char(32) | NO | UNI | NULL | |
+-------+----------+------+-----+---------+-------+
如果表中没有主键也没有其他任何的非空且唯一字段,那么Innodb会采用自己内部提供的一个隐藏字段为主键,隐藏意味着你无法使用它,就无法提供查询速度。一张表中通常都应该有一个主键字段 并且通常将id字段作为主键。
单个字段主键
mysql> create table t7( id int primary key, name char(16) );
mysql> desc t7;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(16) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
联合主键
联合主键只是多个字段联合起来作为表的主键,本质上还是一个主键。
create table t8(
id int,
port int,
primary key(id,port)
);
mysql> desc t8;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | 0 | |
| port | int(11) | NO | PRI | 0 | |
+-------+---------+------+-----+---------+-------+
我们以后在创建表的时候id字段一定要加primary key(数据的唯一标识)
auto_increment自增
auto_increment自增:自动增加数据的编号
create table t9(
id int primary key auto_increment,
name char(16)
);
insert into t9(name) values("nana"),("dada");
select * from t9;
+----+------------------+
| id | name |
+----+------------------+
| 1 | nana |
| 2 | dada |
+----+------------------+
注意auto_increment通常都是加在primary key主键上,不能给普通字段加!!!
补充说明:
# 在删除表中数据的时候,主键的自增不会停止
delete from t9;
insert into t9(name) values("lala");
# id默认是从4开始的
select * from t9;
+----+------------------+
| id | name |
+----+------------------+
| 4 | lala |
+----+------------------+
=======================================================================================================================
# 清空表数据并重置主键
truncate t9;
mysql> insert into t9(name) values("lala");
# id是重置为0开始的
mysql> select * from t9;
+----+------------------+
| id | name |
+----+------------------+
| 1 | lala |
+----+------------------+
以上是关于SQL语句建表 约束条件的主要内容,如果未能解决你的问题,请参考以下文章
SQL语句建表设置主键外键checkdefaultunique约束