公眾號(hào)網(wǎng)站制作淘寶seo優(yōu)化怎么做
1. UNIQUE約束介紹
- 也叫:唯一鍵約束,用于限定數(shù)據(jù)表中字段值的唯一性。
1.1 UNIQUE和primary key區(qū)別:
- 主鍵/聯(lián)合主鍵每張表中只有一個(gè)。
- UNIQUE約束可以在一張表中,多個(gè)字段中存在。例如:學(xué)生的電話、身份證號(hào)都是唯一的。
2. 添加唯一約束
2.1 建表時(shí)添加
2.1.1 案例
- 建立個(gè)學(xué)生信息表,將電話號(hào)碼設(shè)置為唯一約束:
create table tb_students(stu_num char(5) not null,stu_name varchar(10) not null,stu_sex char(1) not null,stu_age number(2) not null,stu_tel char(11) not null,constraint uq_student_tel UNIQUE(stu_tel)
);
實(shí)際開(kāi)發(fā)中,常用的是下面的,直接在stu_tel后面添加個(gè)unique就行:
create table tb_students(stu_num char(5) not null,stu_name varchar(10) not null,stu_sex char(1) not null,stu_age number(2) not null,stu_tel char(11) not null unique
);
2.2 建表后,再添加
2.2.1 案例
- 建立一張學(xué)生信息表,無(wú)唯一鍵:
create table tb_students(stu_num char(5) not null,stu_name varchar(10) not null,stu_sex char(1) not null,stu_age number(2) not null,stu_tel char(11) not null
);
- 接著為表添加唯一鍵:stu_tel:
alter table tb_students
add constraint uq_student_tel
unique(stu_tel);
3. 刪除唯一約束
3.1 案例
- 刪除唯一約束uq_student_tel:
alter table tb_students
drop constraint uq_student_tel;