打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
sql server 增删改(查太多了)

表:

学生(*学号,姓名,性别,年龄,专业)create table student(   sno char(13) primary key,   sname varchar(20) not null,   ssex char(2),   sage smallint,   sdept varchar(30));  课程(*课程号,课程名,学分)create table course(   cno char(4),   cname varchar(40) not null,   ccredit smallint not null,   我们可以将字段的定义和主外键的定义分开   primary key (cno));  选课(学号,课程号,分数)create table sc(   sno char(13),   cno char(4),   grade smallint,    primary key (sno,cno),--定义联合主键   foreign key (sno) references student(sno),   constraint FK_sc_cno foreign key (cno) references course(cno)); 创建一个用户表create table tb_user(   userid int identity(1,1),【设置整型字段自动增长】   username varchar(20) not null,   userpass varchar(16) not null,   groupid int);创建用户组表create table tb_group(   groupid int primary key identity(1001,1),   groupname varchar(30) not null);

insert(增加)

使用 insert 语句向表中插入数据。insert into table [(column [, column...])]values (value [, value...]);插入的数据应与字段的数据类型相同。 举例:方法一:不指定列,插入所有字段insert into student values('2010040','kangji','',22,'计算机科学学院');--SQLServer总是尝试转化为相同的类型insert into student values(20100402,'张三','',22,'计算机科学学院');方法二:指定列,插入部分字段insert into student (sno,sname) values('20100403','李四'); 注意:1)      数据的大小应在列的规定范围内,例如:不能将一个长度为80的字符串加入到长度为40的列中。2)      在values中列出的数据位置必须与被加入的列的排列位置相对应。3)      字符和日期型数据应包含在单引号中。4)      插入空值,不指定或insert into table value(null)注意:在SQLServer 中,''=null; ' '=null; '   '=null; 批量插入数据insert into u(username,userpass) select sname,sno from student where ssex=''; 

update(修改)

使用 update语句修改表中数据。 update 表名 set 列名=表达式[,列名=表达式 ...] [where where_definition]    update语法可以用新值更新原有表行中的各列。set子句指示要修改哪些列和要给予哪些值。update student set sname='康吉' where sno='20100401'; update student set sname='康吉',sage=23 where sno='20100401'; where子句指定应更新哪些行。如没有where子句,则更新所有的行。 修改还有 null 值的数据 is nullselect * from student where ssex is null;

 

delete(删除)

使用 delete语句删除表中数据。

delete from 表名 [where where_definition]

 

如果不使用where子句,将删除表中所有数据。

delete语句不能删除某一列的值(可使用update对值置null)

使用delete语句仅删除记录,不删除表本身。如要删除表,使用【drop table表名】语句。

同insert和update一样,从一个表中删除记录将引起其它表的参照完整性问题,在修改数据库数据时,头脑中应该始终不要忘记这个潜在的问题。

 

删除表中全部数据

delete table 表名; 

 

删除表中指定数据

delete from student where xh='A001';

 

级联删除和更新

create table class(   id int primary key,   name varchar(10)); create table student(   id int primary key,   class_id int references class(id) on delete/update cascade); alter table student add constraint FK_classid foreign key (class_id) references class(id) on update cascade on delete cascade 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
SQL servel 查询练习及答案
经典SQL练习题
【02】MySQL:SQL 基础
数据描述、定义(实验一)
一套Oracle SQL练习题及答案
Oracle触发器使用介绍
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服