T-SQL 查詢、修改數據表

T-SQL修改表數據

 

INSERT語句

語法:

INSERT 
[TOP(expression) [PERCENT]]
[INTO] 
  { <object> | rowset_function_limited 
    [ WITH ( <Table_Hint_Limited> [ ...n ] ) ] /*指定表提示*/
    | view_name } /*視圖名*/
  {
    [(column_list) ] /*指定列名*/
    [<OUTPUT Clause> ] 
    {VALUES /*指定列名的取值*/
      {DEFAULT | NULL | expression}[1…n]) /*列值的構成形式*/   
        |derived_table|execute_statement } } /*結果集*/
    |DEFAULT VALUES /*所有列均取默認值*/

 

例子:

/*插入單個元組*/
/*向student表中插入一個學生記錄(‘200’,‘曾雷’,‘女’,‘1978-2-3’,‘05001’)*/
USE test            
INSERT INTO student_1 
VALUES(100,'曾雷','女','1995-2-3',20)

/*查詢student表,查看結果*/
select * from student_1
-----------------------------------------------------------------------------------


/*向student表中插入一個學生記錄(‘201’,‘孫浩’,‘男’,‘1977-8-4’,NULL)*/
INSERT INTO student_1(sno,sname,ssex,sbirthday,sage) 
VALUES(200,'孫浩','男','1996-8-4',null)

select * from student_1
delete from student_1 where sno=200

INSERT INTO student_1(sno,sname,ssex,sbirthday) 
VALUES(200,'孫浩','男','1996-8-4')
-----------------------------------------------------------------------------------


/*插入元組集合*/
/*將student_1表中的相關數據插入到student表中*/
select * from student
select * from student_1

insert into student(sno,sname,ssex,sage)
select sno,sname,ssex,sage 
from student_1 


/*向student表中添加兩個新生*/
INSERT INTO student_1(sno,sname,ssex,sbirthday,sage) 
VALUES(300,'王明','男','1996-8-4',19),(400,'趙強','男','1996-4-1',19)
-----------------------------------------------------------------------------------


/*向自增列添加數據*/
create table testidentity(
id int identity,
words varchar(10))

insert into testidentity values('a')  --標識列不指定
insert into testidentity values('b')  --指定除了標識列外的列

set IDENTITY_INSERT testidentity on   
insert into testidentity(id,words) values(10,'c')  --指定標識列

set IDENTITY_INSERT testidentity off  
insert into testidentity values('d')

select * from testidentity


UPDATE語句

語法:

UPDATE{table_name|view_name}
SET column_name = {expression | DEFAULT | NULL}[1…n]
where where_clause

例子:

/*將sc表中的成績小於60的加5。*/
UPDATE sc
SET grade=grade+5
WHERE grade<70

/*將張三選修1號課程的成績置零。*/
UPDATE sc
SET grade=0
WHERE cno=1 and sno in
  (select sno from student where sname='張三')
  
  
/*將學號爲1的學生的姓名改爲張三十,年齡改小2歲。*/
UPDATE student
SET sname='張三十',sage=sage-2   --同時更新多列
WHERE sno=1

select * from student
-----------------------------------------------------------------------------------

/*使用top表達式*/
UPDATE top(2) student
SET sage=sage-2  

UPDATE top(50) percent student
SET sage=sage-2

DELETE語句

語法:

DELETE table_name 
WHERE search_condition

例子:

/*刪除student表中學號爲200的記錄*/
select * from student
select * from sc

DELETE student  
WHERE sno='200'

/*刪除張三的選修1號課程的選課記錄*/
DELETE sc  
WHERE cno=1 and sno in 
    (select sno from student where sname='張三')
-----------------------------------------------------------------------------------

/*TRANCATE*/
/*TRUNCATE TABLE  table_name*/
/*一次刪除表中所有數據,即清空表,
  但表的結構及約束保持不變,且該操作不記錄日誌,無法恢復,使用時必須慬慎。*/
  
  
/*刪除student_1表中的記錄*/
TRUNCATE TABLE  student_1

select * from student_1



T-SQL查詢數據

 

SELECT 語句語法:

SELECT select_list
[INTO new_table]
FROM table_source
[WHERE search_condition]
[GROUP BY group_by_expression]
[HAVING search_condition]
[ORDER BY order_expression [ASC | DESC]]

 

簡單查詢例子:

/*查詢列*/

/*查詢student表中所有記錄的sname、ssex和sage列*/
SELECT sname,ssex,sage
FROM student

/*查詢有選課記錄的課程cno*/
select distinct cno   --避免重複項
from sc

/*查詢有85分以上成績的課程cno*/
SELECT DISTINCT cno
FROM  sc 
WHERE grade>85


/*查詢student表的所有記錄*/
SELECT *
FROM student

SELECT sno as '學號',sname as '姓名',ssex as '性別'    
FROM student


/*返回部分結果top*/
select top(1) * from student
select top(1) * from student where ssex='女'
select top(1) with ties *  from student order by sage
select top(50) percent * from student


/*計算列*/
select sno,sname,2015-sage as '出生年月' from student
select sno,cno,grade*1.1 from sc
-----------------------------------------------------


/*選擇查詢

查詢sc表中成績大於60的所有記錄*/
SELECT *
FROM sc
WHERE grade>60


/*查詢sc表中1號課程成績大於60的所有記錄*/
SELECT *
FROM sc
WHERE cno=2 and grade>60


/*查詢score表中成績在60~80之間的所有記錄*/
SELECT *
FROM sc
WHERE grade between 60 and 80


/*查詢sc表中成績爲85、86或88的記錄*/
SELECT *
FROM sc
WHERE grade in(85,86,88)


/*字符串匹配*/

/* % 匹配任意字符
   _ 匹配單個字符
  [] 匹配括號中的任意一個字符
  [^]或[!]匹配沒有出現在括號中的單個字符
   escape換碼字符  */

select * from student

/*查詢student表中姓張的或性別爲“女”的學生記錄*/
SELECT *
FROM student
WHERE sname like '張%'or ssex='女'

/*查詢student表中姓李的學生*/
SELECT *
FROM student
WHERE sname like '李%'

SELECT *
FROM student
WHERE sname like '[李]%'

SELECT *
FROM student
WHERE sname like '李_'

SELECT *
FROM student
WHERE sname like '[^李]%'

SELECT *
FROM student
WHERE sname not like '[李]%'

SELECT *
FROM student
WHERE sname like '%[四]%'


/*查詢sc表中沒成績的記錄*/
SELECT *
FROM sc
WHERE grade is null

SELECT *
FROM sc
WHERE grade is not null


/*查詢結果排序*/
SELECT *
FROM sc
order by grade 

SELECT *
FROM sc
order by cno,grade desc


/*分組查詢*/
/*group by group_by_expression[with rollup|cube]*
  having search_condition
  with rollup 只返回第一個分組條件制定的列的統計行;
  而with cube除返回group by制定的列外,還返回按組統計的行*/

SELECT cno,AVG(grade)
FROM sc
group by cno

SELECT cno,AVG(grade)
FROM sc
group by cno
having AVG(grade)>60

SELECT cno,tno,AVG(grade)
FROM sc
group by cno,tno

SELECT cno,tno,AVG(grade)
FROM sc
group by cno,tno with rollup

select AVG(grade)
from sc

高級查詢例子:

/*嵌套查詢*/
use test

/*使用IN或NOT IN*/
select sname
from student
where sno in
    (select sno 
     from sc
     where cno=2)

select sname
from student
where sno not in
    (select sno 
     from sc
     where cno=2)
     
    
/*比較運算符的子查詢*/

select sno,grade
from sc sc1
where sc1.cno=1 and 
    sc1.grade=(select sc2.grade 
                from sc sc2 
                where sc2.cno=1 and sc2.sno=1)
                
select * from sc

select sno,grade
from sc sc1
where sc1.cno=1 and 
    sc1.grade>(select sc2.grade 
                from sc sc2 
                where sc2.cno=1 and sc2.sno=1)
                
select sno
from sc 
where cno=1 and 
      grade>all(select grade from sc where sno=1)
      
select sno
from sc 
where cno=1 and 
      grade>(select max(grade) from sc where sno=1)
            

select student.sno,sname,cno,grade
from sc as a,student
where student.sno=a.sno and  --不相關子查詢
    grade>(select avg(grade)
           from sc b
           where b.cno=a.cno)  


/*exists*/
SELECT sname
FROM student 
WHERE EXISTS
 (SELECT *
  FROM sc
  WHERE student.sno=sc.sno and sc.cno=2)
  
SELECT sname
FROM student 
WHERE not EXISTS
 (SELECT *
  FROM sc
  WHERE student.sno=sc.sno and sc.cno=2)


/*多層嵌套查詢
查詢最高分的學生姓名*/

select *
from student
where not exists
    (select * 
     from course
     where not exists
        (select * 
         from sc
         where sc.sno=student.sno and sc.cno=course.cno))


select sname
from student
where sno in
    (select sno
     from sc
     where  grade=
        (select max(grade)
        from sc))
go

select * from sc
select * from student


/*DELETE、UPDATE和INSERT語句中的子查詢*/
------------------------------------------------------------


/*聯接查詢*/
use test

/*查詢學生的姓名,選課號及成績*/

select sname,cno,grade
from student,sc 
where student.sno=sc.sno

select sname,cname,grade 
from student,sc,course  --多表連接
where student.sno=sc.sno and sc.cno=course.cno

select sname,cno,grade
from student inner join sc on(student.sno=sc.sno) --內連接


select student.sname,sc.cno,sc.grade
from student left join sc on (student.sno=sc.sno)  --左向外連接


select student.sname,sc.cno,sc.grade
from student right join sc on (student.sno=sc.sno)   --右向外連接


select student.sname,sc.cno,sc.grade
from student full outer join sc on (student.sno=sc.sno)  --完全外部連接


select student.sno,sc.sno,sc.cno,sc.grade  
from student cross join sc                  --交叉連接

select student.sno,sc.sno,sc.cno,sc.grade  
from student cross join sc                  --帶限定條件的交叉連接
where student.sno<2


select c1.cno,c2.cname
from course c1,course c2    --自連接
where c1.cpno=c2.cno
--------------------------------------------------------------------------

/*無法使用ntext、text或image列上直接連接表,
但是使用substring函數在ntext、text或image列上間接聯接表,如*/

select *
from student join sc
on substring(student.mytext,1,2)=substring(sc.mytext,1,2)
------------------------------


/*使用UNION運算符組合多個結果
查詢所有作者和客戶的號碼和名稱*/

select sno,sname from student where sno=1
union
select sno,sname from student where sno=2
go
---------------------------

 /*在查詢的基礎上創建新表
將查詢得到的學生學號、姓名、課程和分數輸入到新建的表score1中,
再顯示該新表的記錄*/

select student.sno,avg(grade) as 平均成績
into avggrade    --該表自動生成
from student inner join sc on (student.sno=sc.sno)
group by student.sno

select * from avggrade


drop table avggrade

博客園博客:欠扁的小籃子

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章