研究ノート(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