您的位置:首页 > 数据库

T-SQL_4 增删改查语句

2010-02-01 22:33 169 查看
数据库四种基本操作:

1, "增" 插入语句

/*插入语句*/
use MyTestsDB
go
--1, 添加标识列
if exists(select name from sysobjects where name = 'newTable')
drop table newTable
select pID, pName, identity(int, 1, 1) as pNo
into newTable
from TB_person
go
--2, 插入一条记录
insert into TB_person(pID, pName)
values(610322198602239777,'达熊')
insert into TB_stuInfo(stuNO, stuName, stuAge, stuID)
values(2009106801, '宁波', 16, 610322198602239777)
--3, 插入多条记录
insert into TB_person
select 610322198602239775,'GOF' union
select 610322198602239771,'Lee' union
select 610322198602239773,'小马哥'


2, "删" 删除语句

use MyTestsDB
go

/*删除数据*/
--已被引用过的数据行,删除时触发REFERENCE约束冲突
--delete from TB_person
--where pID = 610322198602239777
delete from TB_person
where pID = 610322198602239775
/*清空表*/
Truncate table TB_stuInfo


3, "改" 修改语句

/*修改语句*/
update TB_stuInfo set stuAge = 18
where stuAge < 18


4, "查" 查询语句

select stuName as 学生姓名, 学生年龄 = stuAge, stuAddress as 学生住址
from TB_stuInfo
where stuAge > 18
group by stuAge
having COUNT(stuAge)>1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: