溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-05-20 10:43:24 來(lái)源:億速云 閱讀:250 作者:小新 欄目:數(shù)據(jù)庫(kù)

小編給大家分享一下怎么使用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:不帶列名,但要求值的類型要和列字段類型對(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)建視圖,全部省略視圖的屬性列名,由子查詢目標(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)列(子查詢)不是單純的屬性列,而是聚集函數(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)

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

  select *

  from StudentView

  --查詢自定義列名的視圖

  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

  --查詢數(shù)據(jù)庫(kù)是否存在

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

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

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

sql
AI