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

 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. wukong引擎源码分析之搜索——docid有序的数组里二分归并求交集,如果用跳表的话,在插入索引时会更快

    searcher.Search(types.SearchRequest{Text: "百度中国"}) // 查找满足搜索条件的文档,此函数线程安全 func (engine *En ...

  2. 书写优雅的shell脚本(插曲)- /proc

    1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...

  3. hihocoder-1347 小h的树上的朋友(lca+线段树)

    题目链接: 小h的树上的朋友 时间限制:18000ms 单点时限:2000ms 内存限制:512MB 描述 小h拥有n位朋友.每位朋友拥有一个数值Vi代表他与小h的亲密度.亲密度有可能发生变化. 岁月 ...

  4. ImportError: No module named 'httplib'

    原因:Python 2.x中的"httplib"模块在Python 3.x中变成了"http.client" 原代码: import httplib impor ...

  5. 相对定位relative与绝对定位absolute

    relative:相对定位,并没有脱离原来文档流,依然在原来的位置上,可以通过设置left,top,来设置自己的偏移量,但是它依然占据自己原来的位置,偏移的位置会遮盖其他的元素 absolute:绝对 ...

  6. POJ2371【水题】

    mdzz- wa了一发,没看清题意,真是智障 //#include <bits/stdc++.h> #include<iostream> #include<string. ...

  7. c# 异常找不到源代码的情况

    简单说下原因,调用的是dynamic参与的函数 dynamic dataqueue = pi.GetValue(this,null); var eo = ErrorObject.True; var v ...

  8. bzoj1179: [Apio2009]Atm 【缩点+spfa最长路】

    题目传送门 Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruser i 银行的 ATM 取款机.令人奇怪的是,S ...

  9. iOS [CIContext initWithOptions:]: unrecognized selector sent to instance 模拟器 iOS 8.4

    在模拟器(iPhone 4s,iOS 8.4)中运行应用时, 应用crash在了使用CIContext(options:nil) 这个API的一个纯Swift第三方库. StackOverFlow的解 ...

  10. bzoj 4859 [BeiJing2017]机动训练

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4859 题解 和管道取珠类似 首先把平方转化成两条路径经过的图案相同的方案数 对于一条路径 方 ...