sql server追加削除(調べすぎ)
11959 ワード
表:
Insert(追加)
update(修正)
delete(削除)
delete文を使用してテーブルのデータを削除します.
delete fromテーブル名[where where_definition]
where句を使用しない場合、テーブル内のすべてのデータが削除されます.
delete文はカラムの値を削除できません(updateを使用して値をnullに設定できます)
delete文を使用してレコードのみを削除し、テーブル自体を削除しません.テーブルを削除するには、【drop tableテーブル名】文を使用します.
Insertやupdateと同様に、1つのテーブルからレコードを削除すると、他のテーブルの参照整合性の問題が発生します.データベースデータを変更するときは、この潜在的な問題を常に忘れないでください.
テーブルのすべてのデータを削除
delete tableテーブル名;
テーブルに指定されたデータの削除
delete from student where xh='A001';
カスケード削除と更新
(* , , , , )
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 null
select * from student where ssex is null;
delete(削除)
delete文を使用してテーブルのデータを削除します.
delete fromテーブル名[where where_definition]
where句を使用しない場合、テーブル内のすべてのデータが削除されます.
delete文はカラムの値を削除できません(updateを使用して値をnullに設定できます)
delete文を使用してレコードのみを削除し、テーブル自体を削除しません.テーブルを削除するには、【drop tableテーブル名】文を使用します.
Insertやupdateと同様に、1つのテーブルからレコードを削除すると、他のテーブルの参照整合性の問題が発生します.データベースデータを変更するときは、この潜在的な問題を常に忘れないでください.
テーブルのすべてのデータを削除
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