sql server 如何用命令添加非空约束 sql server设置非空约束
转载
/*
数据完整性=数据的有效性和正确性(完整性=有效,正确)
完整性约束
1.唯一约束(允许一行数据为空)
2.主键约束(唯一约束 + 非空约束)
3.默认值约束
4.check约束
5.主外键约束
6.非空约束
一个表在不同的关系中既可以是主表又可以是子表
主外键是一对多关系
*/
create table student(
--字段名 数据类型 约束 (一般在此只加非空约束)
stuId int not null,
stuName varchar(20) not null,
stuAge int not null,
stuSex bit not null,
stuTel varchar(13) not null,
stuAddress varchar(30)
)
go
--添加主键约束
alter table student
add constraint PK_stuId primary key (stuId)
--添加唯一约束
alter table student
add constraint UQ_stuName unique (stuName)
--添加check约束
alter table student
add constraint CK_stuAge check (stuAge >=18 and stuAge <= 35)
--添加默认约束
alter table student
add constraint DF_stuAddress default ('地址不详') for stuAddress
go
create table exam(
examId int not null,
stuId int not null, --外键
labExam int not null,
writtenAxam int not null
)
go
--添加主键约束
alter table exam
add constraint PK_examId primary key (examId)
--添加check约束
alter table exam
add constraint CK_labExam check (labExam between 0 and 100)
--添加非空约束
alter table exam
add constraint NN_labExam check (labExam is not null)
--设置非空(alter table 表名 alter column 列名 该列的类型 NOT NULL)
alter table exam
alter column writtenAxam int not null
go
--添加check约束
alter table exam
add constraint CK_writtenAxam check (writtenAxam >= 0 and writtenAxam <= 100 )
--添加外键约束
alter table exam --修改外键表
add constraint FK_stuId
foreign key(stuId) references student(stuId)
--外键表字段 主键表字段
go
--删除约束(alter table 表名 drop constraint 约束名)
alter table exam
drop constraint FK_stuId
go
/*哪个是外键表,就给它添加外键约束
给表添加主外键约束的时候遵循3个一致
1.外键表外键与主键表主键数据类型一致(语法规范)
2.长度一致(语法规范)
3.名称一致(一般是项目规范,名称最好是一致)
*/
use testDB
/*
启动停止服务的3中方式
1.控制面板-->管理工具-->服务
2.Sql Server 配置管理器
3.使用命令启动和停止服务 net start mssqlserver (这是启动服务),net stop mssqlserver(这是停止服务)
*/
/*
打开sqlserver数据库的操作界面也可以使用命令:如下
win +R 输入cmd
然后输入ssms 打开2008及以上版本的sqlserver管理器
然后输入sqlwb 打开2005版本的sqlserver管理器
也可以win +R 后,直接输入ssms或者sqlwb (ssms是2008及以上版本,sqlwb是2005版本)
*/
/*
sqlserver中有些命令,如下:
sqlcmd 类似于sqlserver的控制台,可以删除表,查询表等等操作
osql 使用外部文件执行sql,自己可以写一个bat(批处理)文件来执行sql,以下是教你怎
么写批处理文件
新建一个批处理文件,以bat后缀结尾,文件名可以自己取,我这里就叫做create.bat,在该文
件中编写以下内容
net start mssqlserver
@echo off
echo ==============正在初始化数据库,请稍后.....==============================
osql -E -i student.sql
echo ==============数据库初始化完毕==============================
*/
go
/*
sysobjects表示对象信息表,准确来说本质上是一个视图(该视图存
放的是表,视图,索引,约束等等)每个数据库都有一个此表
*/
select * from sysobjects
go
--查看exam表在数据库中是否存在
if exists(select * from sysobjects where name = 'exam')--默认只执行后面一行,所以要用begin end
/*
begin end 类似于java中的{}花括号,begin类似于java中的开
始花括号{,end类似于java中的结束花括号}
*/
begin
print '数据库表已存在,准备删除'
--删除exam表
drop table exam
end
go
create table exam(
examId int not null,
stuId int not null, --外键
labExam int not null,
writtenAxam int not null
)
print '创建表成功'
go
--添加主键约束
alter table exam
add constraint PK_examId primary key (examId)
--添加check约束
alter table exam
add constraint CK_labExam check (labExam between 0 and 100)
--添加非空约束
alter table exam
add constraint NN_labExam check (labExam is not null)
--设置非空(alter table 表名 alter column 列名 该列的类型 NOT NULL)
alter table exam
alter column writtenAxam int not null
go
--添加check约束
alter table exam
add constraint CK_writtenAxam check (writtenAxam >= 0 and writtenAxam <= 100 )
--添加外键约束
alter table exam --修改外键表
add constraint FK_stuId
foreign key(stuId) references student(stuId)
--外键表字段 主键表字段
go
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。