-------添加约束、增删改

 use StudentDB2
go
--------创建学生表---------
create table StudentInfo(
--studentId int primary key not null identity(1,1),---设置为主键,并自增长
studentId int not null identity(1,1),
studentNum varchar(16) not null,
studentName nvarchar(16) not null,
studentMobile int null,
classNo int null
)
--------添加约束----------
--主键约束
alter table StudentInfo
add constraint PK_StudentInfo_StudentId primary key (studentId)
go
--唯一约束
alter table StudentInfo
add constraint UQ_StudentInfo_studentNum unique (studentNum)
go
--默认约束
alter table StudentInfo
add constraint DF_StudentInfo_studentMobile default '号码不详' for studentMobile
go
--检查约束
alter table studentInfo
--check (len(studentMobile)=11):检查电话号码的长度是否为11位
--check后的括号中的表达式可以是and或者or连接的多个简单逻辑表达式组成的复合型逻辑表达式
add constraint CK_StudentInfo_studentMobile check (len(studentMobile)=11)
go
--======为了避免重复书写的麻烦,可使用如下方式添加约束
alter table studentInfo
add constraint DF_StudentInfo_studentMobile default '号码不详' for studentMobile,
constraint CK_StudentInfo_studentMobile check (len(studentMobile)=11)
go
--删除约束
alter table studentInfo
drop constraint DF_StudentInfo_studentMobile --constraint关键字是可选的,可写可不写
go
------------修改数据表-----------
--添加列
alter table StudentInfo
add remark1 varchar(20) null,
remark2 varchar(20) null
go
--删除列
alter table StudentInfo
drop column ramark1
go
--------修改列
alter table classNo
--修改了数据类型、长度和可空性
alter column classId varchar(20) not null
go
--修改列名
exec sp_rename 'studentInfo.classNo','classNum'
go
--------------删除数据表--------------------
--再删除之前要先进行判断数据表是否存在,否则会发生错误
--判断方法1
if exists (select * from sys.sysobjects where [name]='studentinfo')
drop table StudentInfo
go
--判断方法2
if OBJECT_ID('studentinfo') is not null
drop table studentinfo
go

-------外键约束

 Use StudentDB2
go
--创建表
create table Score(
studentId int not null identity(1,1),
score int
)
--添加外键约束
alter table score
add constraint FK_score_studentinfo_stuId foreign key (studentId) references studentinfo(studentId)
go

--------插入、更新、删除

Use StudentDB2
go
--全部列均插入数据,提供所有列的数据值,并按照表中各列的顺序列出这些值,故不必指定列名
insert into StudentInfo values(
1,'000001','大壮',124565689,10086,'01','001'
);
go
--按照顺序提供了所有的列,并且相应的给出了所有的值(推荐写法)
insert into StudentInfo(studentId,studentNum,studentName,classNo,remark1,remark2)
values (1,'000001','大壮',124565689,10086,'01','001');
go
--也可以不按照表中的顺序来插入数据(但是提供了所有的列)
insert into StudentInfo(studentId,classNo,studentNum,remark1,studentName,remark2)
values(1,2,'000002','02','二狗','003');
go
--插入部分列值(插入值的个数小于列的个数),必须要声明是哪一列
insert into StudentInfo(studentId,classNo,studentNum)
values(3,03,'000003');
go
---------------更新数据--------------
--简单的update语句
update studentinfo set remark1=000;
go
--带where条件的update语句
update studentinfo set studentnum=0101,studentname='王二麻子' where classno=222;
go
-----------删除数据-------------
--删除整个表格
delete from studentinfo;
go
--删除某一行
delete from studentinfo where studentid=001;
go

  

SQL Server之增删改操作的更多相关文章

  1. 使用java对sql server进行增删改查

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  2. Linq to SQL 简单的增删改操作

    Linq to SQL 简单的增删改操作. 新建数据库表tbGuestBook.结构如下: 新建web项目,完成相应的dbml文件.留言页面布局如下 <body> <form id= ...

  3. Sql Server——数据增删改

    所谓数据的增删改就是在创建好数据库和表后向表中添加数据.删除表中的数据.更改表中的一些数据. 新增数据: 语法一: insert into 表名 values (数据内容)        --这里需要 ...

  4. java对sql server的增删改查

    package Database; import java.sql.*; public class DBUtil { //这里可以设置数据库名称 private final static String ...

  5. SQL Server数据库————增删改查

    --增删改查--增 insert into 表名(列名) value(值列表) --删 delect from 表名 where 条件 --改 update 表名 set 列名=值1,列名2=值2 w ...

  6. VS 使用Sql Server 数据库增删改查

    /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString&quo ...

  7. 详解连接SQL Server数据库的方法,并使用Statement接口实现对数据库的增删改操作

    总结一下,连接SQL Server数据库需要以下几个步骤: 1. 导入驱动Jar包:sqljdbc.jar 2. 加载并注册驱动程序 3. 设置连接路径 4. 加载并注册驱动 5. 连接数据库 6. ...

  8. ASP.NET MVC深入浅出(被替换) 第一节: 结合EF的本地缓存属性来介绍【EF增删改操作】的几种形式 第三节: EF调用普通SQL语句的两类封装(ExecuteSqlCommand和SqlQuery ) 第四节: EF调用存储过程的通用写法和DBFirst模式子类调用的特有写法 第六节: EF高级属性(二) 之延迟加载、立即加载、显示加载(含导航属性) 第十节: EF的三种追踪

    ASP.NET MVC深入浅出(被替换)   一. 谈情怀-ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态 ...

  9. C# winform窗体设计-对数据库执行增删改操作

    对于学习数据库的人来说,数据库的增删改可谓是最基本的了(小编其实也只是一个小白=-=),这篇文章,小编将于大家讲解数据库增删改操作 在执行数据库增删改的时候主要使用的:Command 类       ...

随机推荐

  1. 用JS或jQuery访问页面内的iframe

    用JS或jQuery访问页面内的iframe,兼容IE/FF 注意:框架内的页面是不能跨域的!假设有两个页面,在相同域下.index.html 文件内含有一个iframe: <!DOCTYPE ...

  2. 一些优秀的iOS第三方库

    文章目录 Kits ProgressHUD 加载与刷新 图像 引导页 Views Others Kits RegexKitRegexKit是一个正则表达式工具类. JSONKitJSONKit是一个比 ...

  3. Ubuntu 16.04 如何使用Samba服务器

    对于Windows与Ubuntu之间的数据传输,我们习惯于使用FTP工具,不过还是有学员问到samba服务器搭建和使用的问题,这便是本文的来由. Ubuntu版本:ARM裸机1期加强版配套的Ubunt ...

  4. 【转】git修改文件后,提交到远程仓库

    原文地址:https://blog.csdn.net/nly19900820/article/details/73613654 修改文件后,怎么提交到远程仓库1.git status 查看git是否有 ...

  5. django QQ认证登录

    一.开发环境 django 1.10.2 python3.5.2 django-social-auth 0.3.6 二.申请QQ互联 APPID及SECRECT_KEY 大致步骤: 1.成为QQ互联开 ...

  6. Windows 下openssl安装与配置

    编译thirift失败 网上方法很多,大部分是针对32位机的,自己的电脑因为是win7,64位,摸索了很久才安装成功. 环境 WIN7, 64位, vs2005 下载ActivePerl 配置过程中需 ...

  7. bzoj 3365: [Usaco2004 Feb]Distance Statistics 路程统计【容斥原理+点分治】

    统计在一个root下的两个子树,每个子树都和前面的运算一下再加进去对于这种需要排序的运算很麻烦,所以考虑先不去同子树内点对的算出合法点对个数,然后减去每一棵子树内的合法点对(它们实际上是不合法的,相当 ...

  8. 高级开发不得不懂的Redis Cluster数据分片机制

    Redis 集群简介 Redis Cluster 是 Redis 的分布式解决方案,在 3.0 版本正式推出,有效地解决了 Redis 分布式方面的需求. Redis Cluster 一般由多个节点组 ...

  9. mybatis使用要点(2019.5.19)

    接口入参 只有一个参数,叫啥都没问题 有两个参数以上,需使用@Param,否则名字依次为0.1.2和param1.param2.param3 一般用#,防sql注入:偶尔用$,比如需要动态表名等 接口 ...

  10. JSP | 基础 | 在同一表单中提交两个不同的action

    通过与跟JS配合使用实现需求 <head> <title>Chat Room</title> <script type="text/javascri ...