学习笔记(04):mySQL数据库开发教程-实现实体完整性--唯一性约束

立即学习:https://edu.csdn.net/course/play/4364/77141?utm_source=blogtoedu

唯一性约束

# 创建表时指定列添加唯一性约束
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
发布了4 篇原创文章 · 获赞 0 · 访问量 25

猜你喜欢

转载自blog.csdn.net/weiying_zh/article/details/105271808