SQLServe_使用T-SQL语句创建数据库、创建表以及表的约束

2024-05-27 04:32

本文主要是介绍SQLServe_使用T-SQL语句创建数据库、创建表以及表的约束,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

if exists(select * from sysdatabases where name='school')
begindrop database school
end
gocreate database school
on primary
(name='school_data',							--主数据文件的逻辑名称filename='F:\project\school_data.mdf',  				--主数据文件的物理名称及地址size=5mb,								--主数据文件的初始大小maxsize=100mb,								--主数据文件的最大值filegrowth=15%								--主数据文件的增长率
)
log on
(name='school_log.ldf',					--数据日志文件的逻辑名称filename='F:\project\school_log.mdf',			--数据日志文件的物理名称及地址size=2mb,						--数据日志文件的初始大小filegrowth=1mb						--数据日志文件的增长速度
)
go--建表
use school	
go
--1.年级表
if exists(select * from sysobjects where name='Grade')
begindrop table Grade
end
gocreate table Grade
(GradeId int Identity(1,1) not null,GradeName nvarchar(50) not null
)
go--2.学生表
--如果要创建的表已存在,那么就删除
if exists(select * from sysobjects where name='Student')
begindrop table Student
end
go
create table Student
(StudentNo nchar(8) not null,StudentName nvarchar(20) not null,LoginPwd nvarchar(20) not null,Sex nchar(1) not null,GradeId int not null,Phone nvarchar(20) not null,Address nvarchar(50) not null,BornDate nvarchar(50) not null,EMail nvarchar(50) not null 
)--3.科目表
if exists(select * from sysobjects where name='Subject')
begindrop table Subject
end
gocreate table Subject
(SubjectId int identity(1,1) not null,SubjectName nvarchar(20) not null,ClassHour int not null,GradeId int not null
)--4.成绩表
if exists(select * from sysobjects where name='Result')
begindrop table Result
end
gocreate table Result
(Id int identity(1,1) not null,StudentNo nchar(8) not null,SubjectId int not null,StudentResult int not null, --0-100之间ExamDate datetime not null  --默认为当前日期
)--约束
--1.年级表约束
alter table Grade
add constraint PK_GradeId primary key(GradeId)  --主键约束
go
alter table Grade
add constraint UQ_GradeName unique (GradeName)	--唯一约束
go
--2.学生表约束
alter table Student
add constraint PK_StudentNo primary key(StudentNo)
go
alter table Student
add constraint CK_StudentNocheck check(StudentNo like 'S20118[0-9][0-9]')--检查约束
go
alter table Student
add constraint CK_LoginPwd check (len(LoginPwd)>=6 and len(LoginPwd)<=12)
go
alter table Student
add constraint FK_Student_GradeId foreign key(GradeId) references Grade(GradeId)
go
alter table Student
add constraint CK_Sex check(sex in ('男','女'))
go
alter table Student
add constraint CK_Phone check(Phone like '1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
go
alter table Student
add constraint DF_Address default('学生宿舍') for Address
go
alter table Student
add constraint CK_BornDate check(BornDate>1990-1-1)
go
alter table Student
add constraint CK_Email check(Email like '%@%')
go--科目表约束
alter table Subject
add constraint PK_SubjectId primary key(SubjectId)
go
alter table Subject
add constraint CK_ClassHour check(ClassHour>0)
go
alter table Subject
add constraint FK_Subject_GradeId foreign key(GradeId) references Grade(GradeId)
go--成绩表约束
alter table Result
add constraint PK_Id primary key(Id)
go
alter table Result
add constraint FK_Result_StudentNo foreign key(StudentNo) references Student(StudentNo)
go
alter table Result
add constraint FK_Result_SujectId foreign key(SubjectId) references Subject(SubjectId)
go

这篇关于SQLServe_使用T-SQL语句创建数据库、创建表以及表的约束的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1006513

相关文章

C语言中联合体union的使用

本文编辑整理自: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=179471 一、前言 “联合体”(union)与“结构体”(struct)有一些相似之处。但两者有本质上的不同。在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和。而在“联合”中,各成员共享一段内存空间, 一个联合变量

ESP32 esp-idf esp-adf环境安装及.a库创建与编译

简介 ESP32 功能丰富的 Wi-Fi & 蓝牙 MCU, 适用于多样的物联网应用。使用freertos操作系统。 ESP-IDF 官方物联网开发框架。 ESP-ADF 官方音频开发框架。 文档参照 https://espressif-docs.readthedocs-hosted.com/projects/esp-adf/zh-cn/latest/get-started/index

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

mysql索引四(组合索引)

单列索引,即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引;组合索引,即一个索引包含多个列。 因为有事,下面内容全部转自:https://www.cnblogs.com/farmer-cabbage/p/5793589.html 为了形象地对比单列索引和组合索引,为表添加多个字段:    CREATE TABLE mytable( ID INT NOT NULL, use

mysql索引三(全文索引)

前面分别介绍了mysql索引一(普通索引)、mysql索引二(唯一索引)。 本文学习mysql全文索引。 全文索引(也称全文检索)是目前搜索引擎使用的一种关键技术。它能够利用【分词技术】等多种算法智能分析出文本文字中关键词的频率和重要性,然后按照一定的算法规则智能地筛选出我们想要的搜索结果。 在MySql中,创建全文索引相对比较简单。例如:我们有一个文章表(article),其中有主键ID(

mysql索引二(唯一索引)

前文中介绍了MySQL中普通索引用法,和没有索引的区别。mysql索引一(普通索引) 下面学习一下唯一索引。 创建唯一索引的目的不是为了提高访问速度,而只是为了避免数据出现重复。唯一索引可以有多个但索引列的值必须唯一,索引列的值允许有空值。如果能确定某个数据列将只包含彼此各不相同的值,在为这个数据列创建索引的时候就应该使用关键字UNIQUE,把它定义为一个唯一索引。 添加数据库唯一索引的几种

mysql索引一(普通索引)

mysql的索引分为两大类,聚簇索引、非聚簇索引。聚簇索引是按照数据存放的物理位置为顺序的,而非聚簇索引则不同。聚簇索引能够提高多行检索的速度、非聚簇索引则对单行检索的速度很快。         在这两大类的索引类型下,还可以降索引分为4个小类型:         1,普通索引:最基本的索引,没有任何限制,是我们经常使用到的索引。         2,唯一索引:与普通索引

Lipowerline5.0 雷达电力应用软件下载使用

1.配网数据处理分析 针对配网线路点云数据,优化了分类算法,支持杆塔、导线、交跨线、建筑物、地面点和其他线路的自动分类;一键生成危险点报告和交跨报告;还能生成点云数据采集航线和自主巡检航线。 获取软件安装包联系邮箱:2895356150@qq.com,资源源于网络,本介绍用于学习使用,如有侵权请您联系删除! 2.新增快速版,简洁易上手 支持快速版和专业版切换使用,快速版界面简洁,保留主

如何免费的去使用connectedpapers?

免费使用connectedpapers 1. 打开谷歌浏览器2. 按住ctrl+shift+N,进入无痕模式3. 不需要登录(也就是访客模式)4. 两次用完,关闭无痕模式(继续重复步骤 2 - 4) 1. 打开谷歌浏览器 2. 按住ctrl+shift+N,进入无痕模式 输入网址:https://www.connectedpapers.com/ 3. 不需要登录(也就是