----------------------------------------SQL Server建库-建表-建约束创建School数据库--------------------------------------

--创建School数据库之前:首先判断数据库是否存在,若存在则删除后再创建,若不存在则创建--
--exists关键字:括号里边能查询到数据则返回‘true’ 否则返回‘false’
if exists(select * from sysdatabases where name = 'School')
--exists返回‘true’则执行删除数据库操作--
drop database School

--exists返回‘false’则表明数据库不存在,直接创建
create database School
on primary
(
--主数据库文件--
name = 'School', --主数据文件逻辑名
fileName = 'D:\project\School.mdf', --主数据文件物理逻辑名
size = 5MB, --初始值大小
maxsize = 100MB, --最大大小
filegrowth = 15% --数据文件增长量
)
log on
(
--日志文件--
name = 'School_log',
filename = 'D:\project\School_log.ldf',
size = 2MB,
filegrowth = 1MB
)
go

----------------------------------------使用T-SQL创建employee数据库------------------------------------
create database employee
on primary
(
--主要数据文件--
name = 'employee1',
filename = 'D:\project\employee1.mdf',
size = 10MB,
filegrowth = 10%
),
(
--次要数据文件--
name = 'employee2',
filename = 'D:\project\employee2.ndf',
size = 20MB,
maxsize = 100MB,
filegrowth = 1MB
)
log on
(
--第一个日志文件--
name = 'employee_log1',
filename = 'D:\project\employee_log1.ldf',
size = 10MB,
filegrowth = 1MB
),
(
--第二个日志文件--
name = 'employee_log2',
filename = 'D:\project\employee_log2.ldf',
size = 10MB,
maxsize = 50MB,
filegrowth = 1MB
)

---------------------------------查询已存在的数据库信息---------------------------
select * from sysdatabases

---------------------------------删除数据库------------------------------------
drop database School

---------------------------------创建Student数据库表----------------------------
--1、选择操作的数据库--
use School
go

--判断表是否存在--
if exists(select * from sysobjects where name = 'Student')
drop table Student

--2、创建表---
create table Student
(
--具体的列名 数据类型 列的特征(是否为空)--
StudentNo int identity(2,1) not null,
LoginPwd nvarchar(20) not null,
StudentName nvarchar(20) not null,
Sex int not null,
GradeId int not null,
phone nvarchar(50) not null,
BornDate datetime not null,
Address nvarchar(255),
Email nvarchar(50),
IDENTITYcard varchar(18)
)
go

---查看所有数据库对象(数据库表)---
select * from sysobjects

drop table Student

----------------------创建subject课程表-------------------
-----1、判断表是否存在;若存在则删除再创建,若不存在则直接创建--------
if exists(select * from sysobjects where name = 'subject')
drop table subject

use School
go

---创建subject课程表--
create table subject
(
SubjectNo int not null identity(1,1),
SubjectName nvarchar(50),
ClassHour int,
GradeID int
)

----------------------------------------创建Result成绩表-------------------
-----1、判断表是否存在;若存在则删除再创建,若不存在则直接创建--------
if exists(select * from sysobjects where name = 'Result')
drop table Result

use School
go

---创建Result成绩表--
create table Result
(
StudentNo int not null,
SubjectNo int not null,
ExamDate Datetime not null,
StudentResult int not null
)

-----------------------------------------创建Grande年级表-------------------
-----1、判断表是否存在;若存在则删除再创建,若不存在则直接创建--------
if exists(select * from sysobjects where name = 'Grade')
drop table Grade

use School
go

---创建Grande年级表--
create table Grade
(
GradeId int not null,
GrandeName nvarchar(50)
)

-----------------------------------------T-SQL添加约束-------------------------
--给StudentNo添加主键约束---
alter table Student
add constraint pk_StuNo primary key(StudentNo)

--给身份证添加唯一约束--
alter table Student
add constraint uq_StuIdcard unique(IDENTITYcard)

---给地址address添加默认约束--
alter table Student
add constraint df_stuaddress default('地址不详') for Address

---删除地址address默认约束---
alter table Student
drop constraint df_stuaddress

----------出生日期添加检查约束--------
alter table Student
add constraint ck_stuBorndate check(Borndate > '1980-01-01')

---------与Grand(年级表)建立主外键关系--------

--1、添加Grade主键(操作Grade)---
alter table Grade
add constraint pk_graid primary key(GradeId)

--2、添加Grade外键(操作Student)--
alter table Student
add constraint fk_stuGradeID foreign key(GradeId) references Grade(GradeId)

-------------------给subject课程表添加约束-----------------------

----给subjectNo列添加主键约束------
alter table subject
add constraint pk_SubID primary key(SubjectNo)

------给课程名称subjectName添加非空约束;-----
-----with nocheck:已经存在数据不通过check约束-------
alter table subject with nocheck
add constraint ck_subName check(SubjectName is not null)

-----学时必须大于0-----
alter table subject with nocheck
add constraint ck_ClassHour check(ClassHour > 0)

-----与Grade年级表添加主外键约束----
alter table subject with nocheck
add constraint fk_GradeID foreign key(GradeID)
references Grade(GradeID)

----------给result成绩表添加约束------------

-------添加多个约束---------
alter table Result
add
constraint pk_No_subID_date primary key(StudentNo,SubjectNo,ExamDate),
constraint df_examdate default(getdate()) for ExamDate,
constraint ck_StudentResult check(StudentResult between 0 and 100),
constraint fk_StuNo foreign key(StudentNo) references Student(StudentNo),
constraint fk_subNo foreign key(SubjectNo) references Subject(SubjectNo)

--删除多个约束--
alter table Result
drop constraint pk_No_subID_date,fk_subNo,fk_StuNo,ck_StudentResult,df_examdate

--------更改列的数据类型----------
alter table Result
alter column StudentResult int

SQL Server建库-建表-建约束的更多相关文章

  1. SQL Server跨库复制表数据错误的解决办法

    SQL Server跨库复制表数据的解决办法   跨库复制表数据,有很多种方法,最常见的是写程序来批量导入数据了,但是这种方法并不是最优方法,今天就用到了一个很犀利的方法,可以完美在 Sql Serv ...

  2. SQL Server 跨库复制表方法小笔记

    insert into tableA (column1,column2.....) SELECT * FROM OPENDATASOURCE('SQLOLEDB', 'Data Source=127. ...

  3. SQL Server查看库、表占用空间大小

    转自:https://blog.csdn.net/yenange/article/details/50493580 查询数据文件与日志文件占用情况,查看数据大小,查看库大小 1. 查看数据文件占用(权 ...

  4. SQL Server 多库操作 库名.dbo.表名 出错的问题!

    SQL Server 多库操作 库名.dbo.表名 出错的问题! 数据库名不要用数字开头. 例如:343934.dbo.user 这就会出错.md a343934.dbo.user 就没问题!! 记住 ...

  5. 清空SQL Server数据库中所有表数据的方法(转)

    清空SQL Server数据库中所有表数据的方法 其实删除数据库中数据的方法并不复杂,为什么我还要多此一举呢,一是我这里介绍的是删除数据库的所有数据,因为数据之间可能形成相互约束关系,删除操作可能陷入 ...

  6. 把sql server 2000的用户表的所有者改成dbo

    怎么样把sql server 2000的用户表的所有者,改成dbo,而不是用户名. 推荐使用下面介绍的第二种方法,执行以下查询便可以了.sp_configure 'allow updates','1' ...

  7. 清空SQL Server数据库中所有表数据的方法

    原文:清空SQL Server数据库中所有表数据的方法 其实删除数据库中数据的方法并不复杂,为什么我还要多此一举呢,一是我这里介绍的是删除数据库的所有数据,因为数据之间可能形成相互约束关系,删除操作可 ...

  8. MS SQL SERVER 中的系统表

    MS SQL SERVER 中的系统表 序号 名称 说明 备注 1 syscolumns 每个表和视图中的每列在表中占一行,存储过程中的每个参数在表中也占一行.   2 syscomments 包含每 ...

  9. SQL Server跨库跨服务器访问实现

    我们经常会遇到一个数据库要访问另一个数据库,或者一台服务器要访问另一台服务器里面的数据库. 那么这个如何实现的呢? 相信看完这篇文章你就懂了! 同一台服务器跨库访问实现 1. 首先创建两个数据库Cro ...

  10. 干货 | RDS For SQL Server单库上云

    数据库作为核心数据的重要存储,很多时候都会面临数据迁移的需求,例如:业务从本地迁移上云.数据中心故障需要切换至灾备中心.混合云或多云部署下的数据同步.流量突增导致数据库性能瓶颈需要拆分-- 本文将会一 ...

随机推荐

  1. 测试出来了第一版代码--可以得到用户token啦

    一版一版往前走啦... 先安装vs2010的学习版, 然后用codeblock来搞. 有一个msvcr100.dll这个文件需要和代码同级目录. 这样的好处是合规,然后,codeblock也可以用vs ...

  2. [A]System.Web.WebPages.Razor.Configuration.HostSection 无法强制转换为 [B]System.Web.WebPages.Razor.Configuration.HostSection。

    记录下mvc4升级到mvc5后,运行项目提示: “/”应用程序中的服务器错误. [A]System.Web.WebPages.Razor.Configuration.HostSection 无法强制转 ...

  3. Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.embedded.FilterRegistrationBean

    Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.embedded.FilterRegistr ...

  4. UIView的层介绍

    UIView的层介绍 subview在西安市到屏幕上时,是位于superview上层的. 同一个view的subview时依照增加的顺序显示相应层的.越晚增加的subview显示在越上层,反之也是如此 ...

  5. 解决华为手机不出现logcat日志的问题

    问题描写叙述:公司一部华为手机在连接Eclipse时在Logcat中看不到相关日志 解决方法:1 进入手机拨号界面2 输入*#*#2846579#*#*3 输入完成后自己主动跳转到測试界面4 依次选择 ...

  6. cocos2d-x 多触点监听

    [cpp] view plaincopy //首先到cocos2d-x项目下的ios目录下.找到AppController.mm文件,在函数 didFinishLaunchingWithOptions ...

  7. HDU 2461 线段树扫描线

    给出N个矩形,M次询问 每次询问给出R个.问这R个矩形围成的面积 经典扫面线求面积并,对每次询问的R个点离散化一下 #include "stdio.h" #include &quo ...

  8. js 四种调用方式 Method Invocation Pattern

    4.3. Invocation Invoking a function suspends the execution of the current function, passing control ...

  9. Codesys——TON和TOF的使用方法

    1. 引言 介绍延迟导通.延迟关闭函数的使用方法. 2. 函数描述 TON: 当IN为FALSE时,输出Q为FALSE: 当IN为由FALSE变为TRUE时,延迟导通过程中Q为FALSE,当时间到Q变 ...

  10. Linux ALSA声卡驱动之四:Control设备的创建

    声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢! Control接口 Control接口主要让用户空间的应用程序(alsa-lib)可以访问 ...