Home > Database > Mysql Tutorial > MySQL中数据的约束

MySQL中数据的约束

WBOY
Release: 2016-06-07 15:25:50
Original
1258 people have browsed it

今天说说数据库中的数据表数据约束问题,约束大概分为主键约束,外键约束,默认约束,唯一约束,非空约束这几种,关于讲解配置,我后面慢慢说 1、主键约束 其实是用来唯一标示表中的一个列或多个列,但是一个表中必须唯一,配置方法 (1)、初始化列级主键约

今天说说数据库中的数据表数据约束问题,约束大概分为主键约束,外键约束,默认值约束,唯一约束,非空约束这几种,关于讲解配置,我后面慢慢说

1、主键约束

其实是用来唯一标示表中的一个列或多个列,但是一个表中必须唯一,配置方法

(1)、初始化列级主键约束

create table custominfo
(
id int primary key,
name varchar(12),
age int
);
Copy after login

(2)、初始化表级主键约束
create table custominfo
(
id int,
name varchar(12),
age int,
primary key(id)
);
Copy after login

(3)、修改主键约束
alter table customerinfo
add constraint yueshu primary key(id);
Copy after login

(4)、修改添加联合主键约束
alter table customerinfo
add constraint yueshu primary key(id,name);
Copy after login

(5)、删除主键约束
alter table customerinfo
drop primary key;
Copy after login

2、外键约束

比如某个表B关联于表A,也就是表A中所有项目表B中都有,就可以设置表A主键关联,表B外键关联


(1)初始化添加

create table typeinfo
(
typeid int primary key,
type varchar(20)
);
create table roominfo
(
roomid int primary key,
type varchar(20),
price int
constraint yueshu foreign key(roomid) references typeinfo(typeid)
);
Copy after login

(2)、修改添加
alter table roominfo
add constraint yueshu foreign key(roomid) references typeinfo(typeid);
Copy after login

(3)、删除外键约束
alter table roominfo
drop foreign key yueshu;
Copy after login

3、默认值约束

将某一数据初始化为固定值

(1)、初始化设置

create table roominfo
(
id int,
name varchar(20),
price int default 0
);
Copy after login

(2)、修改添加设置
alter table roominfo
alter price set default 0;
Copy after login

(3)、删除默认约束
alter table roominfo
alter price drop default;
Copy after login

4、非空约束

某项数据不得为空的约束

(1)、初始化设置

create table roominfo
(
id int not null,
name varchar(20)
);
Copy after login

(2)、修改设置
alter table roominfo
alter id int not null;
Copy after login

5、检查约束

就是固定行规定数据的范围

(1)、初始化列级设置

<pre name="code" class="sql"><pre name="code" class="sql" style="background-color: rgb(255, 255, 255); "><pre name="code" class="sql">create table roominfo
(
id int,
name varchar(20),
price int check(price>=0)
);
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

(2)、初始化表级设置
<pre name="code" class="sql">create table roominfo
(
id int,
name varchar(20),
price int,
check(price>=0)
);
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

(3)、修改设置
alter table roominfo
add constraint yueshu check(price>=0);
Copy after login

6、唯一约束

(1)、初始化列级唯一约束

create table roominfo
(
id int unique,
name varchar(20)
);
Copy after login

(2)、初始化表级唯一约束
create table roominfo
(
id int,
name varchar(20),
unique(id)
);
Copy after login

可以设置多个独立唯一约束

(3)、初始化创建共同唯一约束

create table roominfo
(
id int,
name varchar(20),
price int,
unique(id,name)
);
Copy after login

(4)、修改设置独立唯一约束
alter table roominfo
add constraint yueshu unique(id);
Copy after login

(5)、修改设置共同唯一约束
alter table roominfo
add constraint yueshu unqiue(id,name);
Copy after login

(6)、删除唯一约束
drop index yueshu on roominfo;
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template