sql server 增删改(查太多了)「建议收藏」

sql server 增删改(查太多了)「建议收藏」表: 学生(*学号,姓名,性别,年龄,专业) create table student( sno char(13) primary key, sname varchar(20) not null, s

表:

学生(*学号,姓名,性别,年龄,专业)

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

);

代码100分

insert(增加)

代码100分使用 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 null

select * 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″;

 

级联删除和更新

代码100分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

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/10607.html

(0)
上一篇 2022-12-24
下一篇 2022-12-24

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注