腾讯云如何做网站,广告设计与制作前景,上海市,营销策略包括哪些方面1. UNIQUE约束介绍
也叫#xff1a;唯一键约束#xff0c;用于限定数据表中字段值的唯一性。
1.1 UNIQUE和primary key区别#xff1a;
主键/联合主键每张表中只有一个。UNIQUE约束可以在一张表中#xff0c;多个字段中存在。例如#xff1a;学生的电话、身份证号都是…1. UNIQUE约束介绍
也叫唯一键约束用于限定数据表中字段值的唯一性。
1.1 UNIQUE和primary key区别
主键/联合主键每张表中只有一个。UNIQUE约束可以在一张表中多个字段中存在。例如学生的电话、身份证号都是唯一的。
2. 添加唯一约束
2.1 建表时添加 2.1.1 案例
建立个学生信息表将电话号码设置为唯一约束
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)
);实际开发中常用的是下面的直接在stu_tel后面添加个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 案例
建立一张学生信息表无唯一键
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;