久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

怎么使用SQL語(yǔ)句將行和列進(jìn)行轉(zhuǎn)換

共計(jì) 1961 個(gè)字符,預(yù)計(jì)需要花費(fèi) 5 分鐘才能閱讀完成。

自動(dòng)寫(xiě)代碼機(jī)器人,免費(fèi)開(kāi)通

丸趣 TV 小編給大家分享一下怎么使用 SQL 語(yǔ)句將行和列進(jìn)行轉(zhuǎn)換,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

如何使用 SQL 語(yǔ)句將行和列進(jìn)行轉(zhuǎn)換

if exists (select * from sysdatabases where [name]= TestDB )

print Yes, the DB exists

else

print No, need a new one?

– 新建一個(gè)數(shù)據(jù)庫(kù)

create database TestDB on

(

name = TestData ,

filename = G:DBSKeyTest.mdf ,

size = 3,

filegrowth = 2

)

log on

(

name = TestLog ,

filename = G:DBSKeyTest.ldf ,

size = 3,

filegrowth = 10

)

–drop database TestDB

use TestDB

go

– 新建一個(gè)表

create table [Scores]

(

[ID] int identity(1,1) primary key,

[Student] varchar(20) ,

[Subject] varchar(30),

[Score] float

)

–drop table [Scores]

如何使用 SQL 語(yǔ)句將行和列進(jìn)行轉(zhuǎn)換

– 修改表中的一列

alter table Scores alter column [Student] varchar(20) not null

– 新增一列

alter table Scores add Birthday datetime

– 刪除一列

alter table Scores drop column Birthday

– 往表中插入單條數(shù)據(jù),方法 1:帶列名

insert into Scores(Student,Subject,Score)

values(張三 , 語(yǔ)文 , 90)

– 往表中插入單條數(shù)據(jù),方法 2:不帶列名,但要求值的類(lèi)型要和列字段類(lèi)型對(duì)應(yīng)

insert into Scores

values(張三 , 英語(yǔ) , 95)

– 插入多條數(shù)據(jù): 用 union 或者 union all

insert into Scores(Student,Subject,Score)

select 李四 , 語(yǔ)文 , 89

union all

select 李四 , 英語(yǔ) , 78

– 刪除表中數(shù)據(jù),沒(méi)有條件時(shí),刪除所有

delete from Scores where ID in(7,8)

– 修改表中數(shù)據(jù)

update Scores

set Student= 王五 ,Score= 94

where ID=10

– 查看數(shù)據(jù)

select * from Scores

– 查看表中最大的 identity 值

select @@identity

– 或者利用 dbcc 命令查看表中最大的 identity 值

dbcc checkident(Scores ,noreseed)

– 創(chuàng)建視圖,全部省略視圖的屬性列名,由子查詢(xún)目標(biāo)列的字段組成

create view StudentView

as

select Student,Subject,Score

from Scores

– 加上 with check option,以后對(duì)視圖的操作 (增,改,刪,查) 都會(huì)自動(dòng)加上 where ID 3

/*

create view StudentView

as

select Student,Subject,Score

from Scores

where ID 3

with check option

*/

– 創(chuàng)建視圖,全部定義屬性列名,需要定義列名的情況:

—- 某個(gè)目標(biāo)列 (子查詢(xún)) 不是單純的屬性列,而是聚集函數(shù)或列表達(dá)式

—- 多表連接時(shí)選出了幾個(gè)同名列

—- 需要在視圖中為某個(gè)列啟用新的更合適的名字

create view IS_Student(Student,Subject,MaxScore)

as

select Student,Subject,Score

from Scores

where Score=(select max(Score) from Scores)

– 查詢(xún)視圖,和基本表完全樣,只不過(guò)如果視圖中有 with check option,會(huì)自動(dòng)加上那個(gè)條件

select *

from StudentView

– 查詢(xún)自定義列名的視圖

select *

from IS_Student

– 對(duì)視圖的 insert/delete/update,和對(duì)基本表的操作一樣,并且最終都是用 RDBMS 自動(dòng)轉(zhuǎn)換為對(duì)基本表的更新

– 并不是所有的視圖都是可更新的,因?yàn)橛行┮晥D的更新不能有意義的轉(zhuǎn)換成對(duì)相應(yīng)基本表的更新

– 刪除視圖

drop view StudentView

– 查詢(xún)數(shù)據(jù)庫(kù)是否存在

以上是“怎么使用 SQL 語(yǔ)句將行和列進(jìn)行轉(zhuǎn)換”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道!

向 AI 問(wèn)一下細(xì)節(jié)

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-12-04發(fā)表,共計(jì)1961字。
轉(zhuǎn)載說(shuō)明:除特殊說(shuō)明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒(méi)有評(píng)論)
主站蜘蛛池模板: 贵定县| 乃东县| 新昌县| 莫力| 郑州市| 攀枝花市| 闽清县| 泰和县| 河池市| 肃北| 调兵山市| 宁德市| 凤庆县| 勐海县| 高邮市| 奉节县| 台州市| 昭通市| 安新县| 新津县| 闵行区| 巫山县| 芦溪县| 敖汉旗| 家居| 竹溪县| 昭苏县| 新密市| 云南省| 双桥区| 喀喇| 同心县| 科技| 女性| 会理县| 舞钢市| 洮南市| 华蓥市| 临武县| 和林格尔县| 涞源县|