使用sql语句创建和删除约束示例代码
作者:whsnow 时间:2024-01-15 21:55:51
使用sql语句创建和删除约束
约束类型
主键约束(Primary Key constraint) --:要求主键列数据唯一,并且不允许为空。
唯一约束(Unique constraint) --:要求该列唯一,允许为空,但只能出现一个空值。
检查约束(Check constraint) --:某列取值范围限制,格式限制等,如有关年龄、邮箱(必须有@)的约束。
默认约束(Default constraint) --:某列的默认值,如在数据库里有一项数据很多重复,可以设为默认值。
外键约束(Foreign Key constraint) --:用于在两个表之间建立关系,需要指定引用主表的哪一列。
**********************************************************************
添加约束:
alter table tablename
add constraint pk_colname primary key(colname)主建约束
alter table tablename
add constraint uq_colname unique (colname)唯一约束
alter table tablename
add constraint df_colname default('地址不详')for colname 默认约束
alter table tablename
add constraint ck_colname check(colname between 12 and 15)检查约束
alter table tablename
add constraint fk_colname foreign key(colname)references tablename(colname)外建约束
删除约束:
alter table tablename
drop constraint 约束名
创建登陆帐户/数据库用户
创建登录帐户:
exec sp_grantlogin 'windows 域名/域帐户'
创建数据库用户:
exec sp_grantdbaccess '登陆帐户','数据库用户'
向数据库授权:
grant 权限[on 表名]to 数据库用户
以上语句可直接在企业管理器中操作
企业管理器/安全性/登陆/新建登陆
填写名称和密码
选择数据库访问,再底下"数据库角色中允许" db_owner也打上勾
默认约束使用户能够定义一个值,每当用户没有在某一列中输入值时,则将所定义的值提供给这一列。如果用户对此列没有特定的要求,可以使用默认约束来为此列输入默认值。
标签:sql语句,创建约束,删除约束
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
通过python模糊匹配算法对两个excel表格内容归类
2023-02-14 20:57:49
![](https://img.aspxhome.com/file/2023/5/127595_0s.jpg)
使用numpy.ndarray添加元素
2022-10-31 06:12:15
TensorFlow卷积神经网络MNIST数据集实现示例
2023-04-20 18:33:12
常用的数据库访问方式是什么?
2009-11-01 15:08:00
图文详解python安装Scrapy框架步骤
2022-02-11 15:02:47
![](https://img.aspxhome.com/file/2023/5/86165_0s.png)
常用原生JS兼容性写法汇总
2024-04-22 22:38:15
Python常见错误:IndexError: list index out of range解决
2021-02-17 21:53:10
Python (Win)readline和tab补全的安装方法
2021-07-30 16:04:46
![](https://img.aspxhome.com/file/2023/5/76965_0s.jpg)
一种有创意的CSS命名规则
2008-06-15 15:18:00
Mysql主键和唯一键的区别点总结
2024-01-15 10:36:36
![](https://img.aspxhome.com/file/2023/5/128385_0s.jpg)
我用Python给班主任写了一个自动阅卷脚本(附源码)
2023-11-16 07:43:25
![](https://img.aspxhome.com/file/2023/3/64303_0s.jpg)
Python字符串三种格式化输出
2022-12-15 09:47:59
python实现微信远程控制电脑
2023-07-07 07:43:43
![](https://img.aspxhome.com/file/2023/7/112147_0s.png)
利用Python编写一个记忆翻牌游戏
2022-09-24 02:20:54
python palywright库基本使用
2021-03-30 17:34:42
argparse 模块简介
2023-05-27 10:19:36
![](https://img.aspxhome.com/file/2023/2/117382_0s.png)
浅析JS原始值和引用值问题
2024-04-28 09:33:17
简述Python中的面向对象编程的概念
2021-11-08 07:53:11
Go语言题解LeetCode1260二维网格迁移示例详解
2024-04-29 13:04:53
![](https://img.aspxhome.com/file/2023/9/133929_0s.jpg)
Vue插槽原理与用法详解
2024-05-09 10:43:09
![](https://img.aspxhome.com/file/2023/3/126623_0s.png)