--创建数据库
create database students
on primary
(
    name=stu_data,
    filename='f:\SQL\stu_data.mdf',
    size=1,
    maxsize=10,
    filegrowth=1
)
log on
(
    name=stu_log,
    filename='f:\SQL\stu_log.ldf',
    size=1,
    maxsize=10,
    filegrowth=1
)
--删除数据库
drop database students
--使用数据库
use students
--删除表
drop table score
drop table student
drop table class
--自定义类型
Exec sp_addtype age,int ,'not null'
--定义表时第一种方法创建主键外健

create table student
(
    fCode char(10) identity(1,2) constraint PK_fCode1 primary key,
    fName varchar(15) not null constraint DF_fName default 'sss',
    fClass varchar(5) not null default '',
    fSex varchar(2)  not null default ''constraint CK_fSex check (fSex in('男 ','女')),
    fAge age
)
create table score
(
    fId char(10) constraint FK_fId foreign key (fId)references student,
    fSubject varchar(10) not null default '',
    fScore int not null default 0 constraint CK_fScore check(fScore>=0 and fScore<=100)
)
--定义表时第二种方法创建主键外键
create table class
(
    fClassId int not null,
    fTeacherName varchar(10) not null,
    fYear varchar(4)not null unique,
    constraint PK_fClassId primary key (fClassId),
    constraint FK_fClassId foreign key (fClassId) references student
    
)
--实例表
drop table A
create table A
(
    fCode int not null,
    fClassId varchar(5)not null,
    fName varchar(4)not null,
    fSex varchar(2)not null,
    
)
--添加唯一性约束
alter table A
add constraint UQ_fClassId_A unique(fClassId)
--添加主键,默认值,检查,外键约束
alter table A
add constraint  PK_fCode_A primary key(fCode)

alter table A
add constraint DF_fName_A default '' for fName

alter table A
add constraint CK_fSex_A check(fsex in('男','女'))

alter table A
add constraint FK_fCode_A foreign key(fCode) references student
--删除约束
alter table A
drop constraint DF_fName_A
--增加列
alter table A
add fAge int null
--删除列
alter table A
drop column fAge

/*注意
    1、主键表中主键列的类型和长度必须和外键表中列的类型和长度相同
    2、外键表中列的取值只能是主键表中主键列的子集*/
    
    
/*
--新增所有列数据
insert into student values('李鸿','0201','女',23)
insert into student values('John','0201','男',23)
insert into score values(1,'英语',90)
insert into class values(1,'逐月','2002')
insert into score values(2,'英语',102)
--新增数据部分列
insert into student(fName,fSex) values('桐港','男')

select * from student
select * from score
select * from class*/

SQLServer创建约束的更多相关文章

  1. 【转】SQL Server 创建约束图解 唯一 主键-界面操作

    SQL Server 创建约束图解 唯一 主键-界面操作 SQLServer中有五种约束,Primary Key约束.Foreign Key约束.Unique约束.Default约束和Check约束, ...

  2. ABP框架使用Mysql数据库,以及基于SQLServer创建Mysql数据库的架构和数据

    ABP默认的数据库是SQLServer,不过ABP框架底层是EF框架,因此也是很容易支持其他类型的数据库的,本篇随笔介绍在ABP框架使用Mysql数据库,以及基于SQLServer创建MySql数据库 ...

  3. SqlServer--代码创建约束

    约束-保证数据完整性先用设计器创建约束,再用代码创建约束.数据库约束是为了保证数据的完整性(正确性)而实现的一套机制非空约束 (选择复选框)not null主键约束(PK)primary key co ...

  4. SqlServer创建数据表描述及列描述信息

    SqlServer创建数据表描述及列描述信息 Intro Q: 为什么要创建描述信息? A: 鼠标悬停在对应表和列上时,会出现描述信息,可以提高工作的效率,借助工具我们根据数据表可以生成Model,可 ...

  5. 约束Constraints--主键约束、外键约束、唯一约束、检查约束、默认约束、NOT NULL约束、列约束与表约束、创建约束、删除约束

    约束   Including Constraints 以下内容转自:https://www.cnblogs.com/wcl2017/p/7043939.html和http://blog.csdn.ne ...

  6. Oracle的创建表和创建约束的Sql语句

    Oracle的创建表和创建约束的Sql语法 1.创建表的语句 ---1.创建模拟的数据表 --- --1.1.创建学生表Student create table Student( StuId NUMB ...

  7. SQLServer创建链接服务器

    --SQLServer创建链接服务器----1.访问接口中Oracle接口 属性 选择 允许进程内-- --删除链接服务器EXEC master.dbo.sp_dropserver @server=N ...

  8. Oracle、Mysql、SqlServer创建表和给表和字段加注释

    一.Oracle --创建表 create table test (      id varchar2(200) primary key not null,      sort number,     ...

  9. PowerDesigner创建表 拷贝创建表语句 SQLSERVER创建数据库 使用查询 创建表 并且添加数据

    PowerDesigner创建表 : 1.双击打开PowerDesigner   2.双击打开Create model 3左键点击Model  types,再点击Physical    Data  m ...

随机推荐

  1. The Data Way Vol.5|这里有一场资本与开源的 battle

    关于「The Data Way」 「The Data Way」是由 SphereEx 公司出品的一档播客节目.这里有开源.数据.技术的故事,同时我们关注开发者的工作日常,也讨论开发者的生活日常:我们聚 ...

  2. 题解「雅礼集训 2017 Day7」事情的相似度

    题目传送门 Description 给出一个长度为 \(n\) 的 \(01\) 串为 \(s\),设 \(t_i\) 为 \(s_{1,2,..,i}\),有 \(m\) 次查询,每次查询给出 \( ...

  3. 题解 2020.10.24 考试 T3 数列

    题目传送门 题目大意 给出一个数 \(n\),你要构造一个数列,满足里面每个数都是 \(n\) 的因子,且每一个数与前面不互质的个数不超过 \(1\).问有多少种合法方案. 保证 \(n\) 的不同质 ...

  4. 基于TLS证书手动部署kubernetes集群

      一.简介 Kubernetes是Google在2014年6月开源的一个容器集群管理系统,使用Go语言开发,Kubernetes也叫K8S. K8S是Google内部一个叫Borg的容器集群管理系统 ...

  5. SpringBoot-Thymeleaf模板引擎

    模板引擎,我们其实大家听到很多,其实jsp就是一个模板引擎,还有用的比较多的freemarker,包括SpringBoot给我们推荐的Thymeleaf,模板引擎有非常多,但再多的模板引擎,他们的思想 ...

  6. Salesforce 生命周期管理(一)应用生命周期浅谈

    本篇参考: https://trailhead.salesforce.com/en/content/learn/trails/determine-which-application-lifecycle ...

  7. linux Samba 搭建

    Samba is a free and open-source software package that provides seamless file and print services to S ...

  8. DDD领域驱动设计-概述-Ⅰ

     如果我看得更远,那是因为我站在巨人的肩膀上.(If I have seen further it is by standing on ye shoulder of Giants.)         ...

  9. 第六次Scrum Metting

    日期:2021年5月3日 会议主要内容概述:讨论前后端进度,前端各模块对接以及前后端对接. 一.进度情况 组员 负责 两日内已完成的工作 后两日计划完成的工作 工作中遇到的困难 徐宇龙 后端 数据模块 ...

  10. 微信小程序实现上拉和下拉加载更多

    在上一篇文章中,我们知道了使用 scroll-view 可以实现上拉加载更多,但是由于 scroll-view 的限制,它无法实现下拉加载更多,这篇文章我们使用 view 组件来实现 上拉和下拉加载更 ...