博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL UNIQUE Constraint
阅读量:6224 次
发布时间:2019-06-21

本文共 1878 字,大约阅读时间需要 6 分钟。

SQL UNIQUE Constraint

The UNIQUE constraint uniquely identifies each record in a database table.

The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.

Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

SQL UNIQUE Constraint on CREATE TABLE

The following SQL creates a UNIQUE constraint on the "P_Id" column when the "Persons" table is created:

SQL Server / Oracle / MS Access:

CREATE TABLE Persons(P_Id int NOT NULL UNIQUE,LastName varchar(255) NOT NULL,FirstName varchar(255),Address varchar(255),City varchar(255))

MySQL:

CREATE TABLE Persons(P_Id int NOT NULL,LastName varchar(255) NOT NULL,FirstName varchar(255),Address varchar(255),City varchar(255),UNIQUE (P_Id))

To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

CREATE TABLE Persons(P_Id int NOT NULL,LastName varchar(255) NOT NULL,FirstName varchar(255),Address varchar(255),City varchar(255),CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName))

SQL UNIQUE Constraint on ALTER TABLE

To create a UNIQUE constraint on the "P_Id" column when the table is already created, use the following SQL:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE PersonsADD UNIQUE (P_Id)

To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE PersonsADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)

To DROP a UNIQUE Constraint

To drop a UNIQUE constraint, use the following SQL:

MySQL:

ALTER TABLE PersonsDROP INDEX uc_PersonID

SQL Server / Oracle / MS Access:

ALTER TABLE PersonsDROP CONSTRAINT uc_PersonID

 

转载地址:http://pnyna.baihongyu.com/

你可能感兴趣的文章
jmeter安装
查看>>
我的友情链接
查看>>
HTTP响应代码(Response Status Code)中文详解
查看>>
bootstrap-按钮的向下向上三角形-向上弹起的下拉菜单
查看>>
SQL Server 2017 AlwaysOn AG 自动初始化(四)
查看>>
phpcms V9 get 实现前台搜索结果分页
查看>>
磁盘配额
查看>>
O-3 WOL网络唤醒详解,随时随地打开电脑
查看>>
F(x)
查看>>
shell脚本整理
查看>>
再续伯努利欧拉错装信封问题
查看>>
可变多维数组组合算法
查看>>
使用Firefox的“HttpRequester”插件测试REST风格的webservice
查看>>
视图和索引
查看>>
论文解读:基于机器学习的知道推荐—Enlister
查看>>
管道命令和xargs的区别
查看>>
百度APP爬虫
查看>>
最全GhostXP SP3系统安装方法(光盘安装|硬盘安装|U盘安装)详细图文教程
查看>>
十七、编辑头像(带参数)
查看>>
U盘数据恢复
查看>>