SQL Server创建数据库和数据表的相关约束实现方法
本文分析了SQL Server创建数据库和数据表的相关约束实现方法。分享给大家供大家参考,具体如下:
创建约束语法如下:
CREATE DATABASE [test]
ON
(NAME=N test ,FILENAME=N d:\SQL2kt_Data\test.mdf ,SIZE=3mb,MAXSIZE=UNLIMITED,FILEGROWTH=1MB)
LOG ON
(NAME=N test_log ,FILENAME=N d:\SQL2kt_Data\test_log.ldf ,SIZE=1MB,MAXSIZE=2048MB,FILEGROWTH=10%)
GO
名词解释(翻译):
constraint
1. 约束;限制[C][(+on)]
legal constraints on the company s activities
对该公司活动法律的限制
2. 强迫;强制[U]
He acted under constraint.
他被迫采取行动。
3. 抑制;拘束;态度不自然[U]
She showed constraint in the presence of the strangers.
她在陌生人面前显得很拘束。
4. 拘禁[U]
5. 拘束(或限制)的事物[C]
clustered
聚集成群的
主外键:选中设置外键的列,右键 关系 表和列规范 点击带有“ ”的按钮
创建带有主键的表,其中,[tid]desc,看上去是倒叙添加数字,其实不是,添加数据是正常的,但是当数据添加完成后,最后添加的数据将第一个被查询出来。
create table dbo.test3(
[tid] [int] identity(100,1) not null,
[name] [varchar](100),
constraint [pk_tid] primary key clustered(
[tid] desc
)
)on [primary]
设置外键
alter table dbo.test4 add fkt
foreign key (tid)
references(来自) dbo.test3([tid]) ON UPDATE CASCADE ON DELETE CASCADE
给没有设置主键的表设置主键,主键字段必须为非空。
alter table dbo.test5 with check add constraint pk_id primary key (id)删除主键()
alter table test5
drop constraint(限制) pk_id(别名)
删除外键
alter table test4
drop constraint fkt(别名)
约束
非空约束
alter table test5
alter column name int not null
唯一约束
直接在表中建立唯一约束、
constraint 约束别名 unique 列表名
create table dbo.test6(
id int not null,
vname varchar(20)
constraint test6_unique unique nonclustered(
vname asc
)
)
check约束
建立check约束
constraint 约束别名 check 约束条件
(修改)
alter table test6
with nocheck add constraint test6_check
check(vname != shit )
卸载约束
alter table test6
drop constraint test6_check
创建修改视图
create view dbo.view2
as
select * from dbo.test6 where dbo.test6.id
看结果select * from dbo.view2
删除试图
drop view dbo.view2
主外键:选中设置外键的列,右键 关系 表和列规范 点击带有“ ”的按钮
创建带有主键的表,其中,[tid]desc,看上去是倒叙添加数字,其实不是,添加数据是正常的,但是当数据添加完成后,最后添加的数据将第一个被查询出来。
create table dbo.test3(
[tid] [int] identity(100,1) not null,
[name] [varchar](100),
constraint [pk_tid] primary key clustered(
[tid] desc
)
)on [primary]
设置外键
alter table dbo.test4 add constraint fkt
foreign key (tid)
references dbo.test3([tid]) ON UPDATE CASCADE ON DELETE CASCADE
给没有设置主键的表设置主键,主键字段必须为非空。
alter table dbo.test5 with check add constraint pk_id primary key (id) 删除主键alter table test5
drop constraint pk_id
删除外键
alter table test4
drop constraint fkt
约束
//javascript :判空
//逻辑层验证 :通过java或者c#进行验证 :登录名是否正确,唯一性通常在此作,尽可能降低数据库服务器的负载
//数据库验证 :唯一约束,check约束
非空约束
alter table test5
alter column name int not null
唯一约束
create table dbo.test6(
id int not null,
vname varchar(20)
constraint test6_unique unique nonclustered(
vname asc
)
)
给已有的字段创建唯一约束
CREATE UNIQUE iNDEX 索引名 ON 表名称(字段名)
注意:字段中已有值不能重复
check约束
alter table test6
with nocheck add constraint test6_check
check(vname != shit )
alter table test3
with nocheck add constraint test3_check2
check(tname != shit and tname != fuck and tname != ohyeah )
卸载约束
alter table test6
drop constraint test6_check
默认约束
create table test4(
tid int,
pwd varchar(20) default 000000 not null
)
给已有的字段增加默认约束
alter table test3 add default 0 for tname1 添加绑定值 exec sp_bindefault td, test2.vname 卸载绑定值 exec sp_unbindefault test2.vname补充:数据库中约束
约束的目的:确保表中数据的完整性
1. 常见的约束类型:
a) 主键约束(Primary Key Constraint):要求主键列数据唯一,并且不允许为空
b) 唯一约束(Unique Constraint):要求该列唯一,允许为空,但只能出现一个空值。
c) 检查约束(Check Constraint):某列取值范围限制、格式限制等,如有关年龄的约束
d) 默认约束(Default Constraint):某列的默认值,如果男生较多,性别默认为“男”
e) 外键约束(Foreign Key Constraint):用于两表间建立关系,需要指定引用主表的哪列
2. 约束的格式:
alter table 表名
add constraint 约束名(取名规则:约束类型_约束字段) 约束类型 具体的约束说明
3. 例子:
alter table stu
add constraint pk_stuno primary key(sno) sno学号为主键
alter table stu
add constraint uq_stuid unique(sid) sid为身份证号,每个身份证号是唯一的
alter table stu
add constraint df_sadess default( 地址不详 ) for saddress saddress为地址,默认值为地址不详
alter table stu
add constraint ck_sage check(sage between 15 and 40) sage学生年龄,要求其值在到之间
alter table scores
add constraint fk_st foreign key(sno) references stu(sno)
外键约束,主表stu连接从表scores,关键字段sno
创建表间约束并不困难,但是专业的名词需要记住
希望本文所述对大家SQL Server数据库设计有所帮助。
我想要获取技术服务或软件
服务范围:MySQL、ORACLE、SQLSERVER、MongoDB、PostgreSQL 、程序问题
服务方式:远程服务、电话支持、现场服务,沟通指定方式服务
技术标签:数据恢复、安装配置、数据迁移、集群容灾、异常处理、其它问题
本站部分文章参考或来源于网络,如有侵权请联系站长。
数据库远程运维 SQL Server创建数据库和数据表的相关约束实现方法
相关文章
- SQL Server 2005“备份集中的数据库备份与现有的数据库不同”解决方法
- SQL SERVER备份数据库存储过程的方法
- sql server实现在多个数据库间快速查询某个表信息的方法
- SQL server 定时自动备份数据库的图文方法
- mysql导入大批量数据出现MySQL server has gone away的解决方法详解数据库
- swoole结合php的pdo mysql模式出现MySQL server has gone away详解编程语言
- 优化SQL Server:让数据库发挥更大潜力(优化sqlserver)
- SQL Server数据库中实现去重的方法(sqlserver除重)
- 重置SQL Server表:一招制敌万招之首(sqlserver重置表)
- SQL Server超级卡:提升数据库性能的完美解决方案(sqlserver超级卡)
- 数据库使用SQL Server建立本地数据库的简易指南(sqlserver用本地)
- 分析SQL Server 数据库的环比分析研究(sqlserver 环比)
- SQL Server数据库中添加值的简单方法(sqlserver添加值)
- 纵表SQL Server数据库横表转换到纵表的技术实现(sqlserver横表转)
- SQL Server带来的新生——新技术替代传统数据库(sqlserver 替代)
- 数据库管理系统深入探索SQL Server:领跑关系数据库管理系统(sqlserver是关系)
- SQL Server改变数据库表结构的方法(sqlserver改基表)
- 什么SQL Server分页技术:利用它实现数据精准搜索(sqlserver分页是)
- SQL Server:数据库管理的一种方案(sqlserver关键字)