Study Notes (04): mySQL database development tutorial - implement entity integrity - the only constraint

Learning immediately: https://edu.csdn.net/course/play/4364/77141?utm_source=blogtoedu

The only constraint

# 创建表时指定列添加唯一性约束
create table w
(
sname char(10) unique,
smobile char(11)
)

 

# 给现有表中指定列添加唯一性约束
alter table w add constraint uc_mobile unique(smobile)

# 如果表中有重复值,不允许添加唯一性约束
# 通过聚合函数查找有重复值的记录,删除记录后,再创建唯一性约束
select smobile, COUNT(*) from w group by smobile having COUNT(*)>1
delete from w where smobile='12345678901'

 

# 创建复合唯一性约束
create table sc
(
sid int,
kid int,
mark int,
constraint uc_sk unique(sid, kid)
)

 

# 删除唯一性约束
alter table sc drop index uc_sk

**创建唯一性约束时,就会为该列创建索引,快速判断是否有重复值
index type:unique

 

Released four original articles · won praise 0 · Views 25

Guess you like

Origin blog.csdn.net/weiying_zh/article/details/105271808