亚洲国产日韩欧美在线a乱码,国产精品路线1路线2路线,亚洲视频一区,精品国产自,www狠狠,国产情侣激情在线视频免费看,亚洲成年网站在线观看

SQl常用增刪改查

時(shí)間:2020-11-10 19:58:39 SQL 我要投稿

SQl常用增刪改查模板

  篇一:SQl常用增刪改查

  SQL常用增刪改查語句

  增加

  現(xiàn)在有一張表,表(Test)里面有三個(gè)字段,分別為sno,sname,age。舉例用一條增加SQL語句,插入一條數(shù)據(jù)進(jìn)庫。

  語句:

  Insert into 表名 value(‘?dāng)?shù)據(jù)1’,’數(shù)據(jù)2’,’數(shù)據(jù)3’)

  具體操作: Insert into testvalues('test','test','1')

  通過上面這條語句,Test表里面就多了一條數(shù)據(jù)。如下圖所示:

  上面這個(gè)例子,是在每條字段都需要插入的時(shí)候?yàn)榱朔奖愣苯釉?into 后面跟表名。但是也會(huì)遇到一些特殊的情況,比如一張表,因?yàn)橛兄魍怄I約束(我這里只有一張表),而我只想插入被約束的字段sno(主鍵)加上age這個(gè)字段,在 into的時(shí)候就需要指明需要插入的字段,下面舉例說明:

  語句:

  Insert into 表名(‘字段名1’,’字段名2’) values(‘?dāng)?shù)據(jù)1’,’數(shù)據(jù)2’)

  具體操作:

   into test(sno,age)values('彭宇','21')

  這樣數(shù)據(jù)庫里面,又多了一條數(shù)據(jù),而沒有插入任何數(shù)據(jù)那個(gè)字段默認(rèn)為NULL。如下圖所示:

  刪除

  在我們?cè)黾訑?shù)據(jù)入庫的時(shí)候,難免會(huì)出現(xiàn)數(shù)據(jù)錄入錯(cuò)誤,或者信息過期后不再需要的數(shù)據(jù),所以我們要利用刪除語句將表里面不需要的數(shù)據(jù)刪除掉。下面舉例說明。

  語句:

  Delete from 表名 where 字段名='需要?jiǎng)h除的數(shù)據(jù)'

  具體操作:

   from test where sno='test'

  通過這條SQL語句,Test表主鍵sno字段里面數(shù)據(jù)為test的該條數(shù)據(jù)就已經(jīng)被刪除了。

  Ps:一般來說都以主鍵為條件進(jìn)行刪除,因?yàn)橹麈I是不可重復(fù)的,我們可以設(shè)想一下,如果沒使用主鍵為刪除條件,假設(shè)一個(gè)公司有兩個(gè)叫彭宇的人。我使用sname=’彭宇’作為刪除條件的話,那么這兩個(gè)同名同姓人的資料都會(huì)被刪除掉,所以這是不可取的。

  批量刪除

  當(dāng)有多條數(shù)據(jù)需要?jiǎng)h除的時(shí)候,我們可以使用批量刪除語句來實(shí)現(xiàn)一次刪除多條數(shù)據(jù)。

  語句:

   from表名where字段名in('該字段里面的數(shù)據(jù)1','該字段里面的數(shù)據(jù)2',……)

  具體操作:

  首先,看一下Test表里面有多少條數(shù)據(jù),如下圖:

  現(xiàn)在我想利用一條SQL語句,將前三條數(shù)據(jù)刪除掉。

   from test where sno in('test','test2','test3')

  通過執(zhí)行這條SQL語句后,前三條數(shù)據(jù)已經(jīng)被我批量刪除了。

  修改

  一條已經(jīng)錄入數(shù)據(jù)庫里面的數(shù)據(jù)如果需要更新、修正,我們就需要用到SQL修改語句。

  語句:

  Update 表名set字段='修改后的數(shù)據(jù)' where 字段='修改條件'

  具體操作:

  Update test set sno='SQL修改語句' where sno='test'

  修改前后比較,下圖所示: (

  修改前

  )(修改后)

  查詢

  上面進(jìn)行了增加,修改操作后,數(shù)據(jù)庫里面已經(jīng)存在有數(shù)據(jù)了,最后我們要利用SQL查詢語句將它們查詢并顯示出來。

  全部查詢

  語句:

  Select * from 表名

  具體操作:

  Select * from test

  執(zhí)行了上面這句話,那么test表里面存在的數(shù)據(jù)都會(huì)被查詢出來,如果我想要單獨(dú)查詢出某個(gè)人的數(shù)據(jù)怎么辦?很簡(jiǎn)單,只需要加上一個(gè)關(guān)鍵詞where就能夠?qū)崿F(xiàn)了。

  單條件查詢

  語句:

  Select * from 表名 where 字段=’需要查詢的數(shù)據(jù)’

  具體操作:

  Select * from test where sno=’彭宇’

  這樣我就查詢出數(shù)據(jù)庫里面sno字段為彭宇的數(shù)據(jù)了。

  多條件查詢

  多條件查詢就是比起單條件查詢多了一個(gè)and關(guān)鍵詞,使用多條件查詢,查出來的結(jié)構(gòu)能夠更加的精確。

  語句:

  Select * from 表名 where 字段=’需要查詢的數(shù)據(jù)’ and 字段=’需要查詢的數(shù)據(jù)’

  具體操作:

  Select * from test where sno=’彭宇’’21’ and age=

  篇二:SQL語句增刪改查

  一、刪:有2中方法

  1.使用刪除數(shù)據(jù)某些數(shù)據(jù)

  語法: from <表名> [where <刪除條件>]

  例: from a where name='開心朋朋'(刪除表a中列值為開心朋朋的行) 注意:刪除整行不是刪除單個(gè)字段,所以在后面不能出現(xiàn)字段名

  2.使用truncate table 刪除整個(gè)表的數(shù)據(jù)

  語法:truncate table <表名>

  例:truncate table tongxunlu

  注意:刪除表的所有行,但表的結(jié)構(gòu)、列、約束、索引等不會(huì)被刪除;不能用語有外建約束引用的表

  二、改

  使用update更新修改數(shù)據(jù)

  語法:<表名> set <列名=更新值> [where <更新條件>]例:tongxunlu set 年齡=18 where 姓名='藍(lán)色小名'

  注意:set后面可以緊隨多個(gè)數(shù)據(jù)列的更新值;where子句是可選的,用來限制條件,如果不選則整個(gè)表的所有行都被更新

  四、查

  1.普通查詢

  語法:select <列名> from <表名> [where <查詢條件表達(dá)試>] [order by <排序的列名>[asc或desc]]

  1).查詢所有數(shù)據(jù)行和列

  例:select * from a

  說明:查詢a表中所有行和列

  2).查詢部分行列--條件查詢

  例:select i,j,k from a where f=5 說明:查詢表a中f=5的所有行,并顯示i,j,k3列

  3).在查詢中使用AS更改列名

  例:select name as 姓名 from a whrer xingbie='男'

  說明:查詢a表中性別為男的所有行,顯示name列,并將name列改名為(姓名)顯示

  4).查詢空行

  例:select name from a where email is null

  說明:查詢表a中email為空的所有行,并顯示name列;SQL語句中用is null或者is not null來判斷是否為空行

  5).在查詢中使用常量

  例:select name '唐山' as 地址 from a

  說明:查詢表a,顯示name列,并添加地址列,其列值都為'唐山'

  6).查詢返回限制行數(shù)(關(guān)鍵字:top percent)

  例1:select top 6 name from a

  說明:查詢表a,顯示列name的前6行,top為關(guān)鍵字

  例2:select top 60 percent name from a

  說明:查詢表a,顯示列name的60%,percent為關(guān)鍵字

  7).查詢排序(關(guān)鍵字:order by , asc , desc)

  例:select name

  from a

  where chengji>=60

  order by desc

  說明:查詢表中chengji大于等于60的所有行,并按降序顯示name列;默認(rèn)為ASC升序

 。.模糊查詢

  1).使用like進(jìn)行模糊查詢

  注意:like運(yùn)算副只用語字符串,所以僅與char和varchar數(shù)據(jù)類型聯(lián)合使用 例:select * from a where name like '趙%'

  說明:查詢顯示表a中,name字段第一個(gè)字為趙的記錄

  2).使用between在某個(gè)范圍內(nèi)進(jìn)行查詢

  例:select * from a where nianling between 18 and 20

  說明:查詢顯示表a中nianling在18到20之間的記錄

  3).使用in在列舉值內(nèi)進(jìn)行查詢

  例:select name from a where address in ('北京','上海','唐山')

  說明:查詢表a中address值為北京或者上;蛘咛粕降挠涗洠@示name字段3.分組查詢

  1).使用group by進(jìn)行分組查詢

  例:select studentID as 學(xué)員編號(hào),AVG(score) as 平均成績(jī) (注釋:這里的score是列名)

  from score (注釋:這里的score是表名)

  group by studentID

  說明:在表score中查詢,按strdentID字段分組,顯示strdentID字段和score字段的平均值;select語句中只允許被分組的列和為每個(gè)分組返回的一個(gè)值的表達(dá)試,例如用一個(gè)列名作為參數(shù)的聚合函數(shù)

  2).使用having子句進(jìn)行分組篩選

  例:select studentID as 學(xué)員編號(hào),AVG(score) as 平均成績(jī) (注釋:這里的score是列名)

  from score (注釋:這里的score是表名)

  group by studentID

  having count(score)>1

  說明:接上面例子,顯示分組后count(score)>1的行,由于where只能在沒有分組時(shí)使用,分組后只能使用having來限制條件,

 。.多表聯(lián)接查詢

  1).內(nèi)聯(lián)接

 、僭趙here子句中指定聯(lián)接條件

  例:select a.name,b.chengji

  from a,b

  where a.name=b.name

  說明:查詢表a和表b中name字段相等的記錄,并顯示表a中的name字段和表b中的chengji字段

 、谠趂rom子句中使用join…on

  例:select a.name,b.chengji

  from a inner join b

  on (a.name=b.name)

  說明:同上

  2).外聯(lián)接

 、僮笸饴(lián)接查詢

  例:select s.name,c.courseID,c.score

  from strdents as s

  left outer join score as c

  on s.scode=c.strdentID

  說明:在strdents表和score表中查詢滿足on條件的行,條件為score表的.strdentID與strdents表中的sconde相同

 、谟彝饴(lián)接查詢

  例:select s.name,c.courseID,c.score

  from strdents as s

  right outer join score as c

  on s.scode=c.strdentID

  說明:在strdents表和score表中查詢滿足on條件的行,條件為strdents表中的sconde與score表的strdentID相同

  三、增:有4種方法

  1.使用插入單行數(shù)據(jù):

  語法: [into] <表名> [列名] values <列值>

  例: into Strdents (姓名,性別,出生日期) values ('開心朋朋','男','1980/6/15')

  注意:into可以省略;列名列值用逗號(hào)分開;列值用單引號(hào)因上;如果省略表名,將依次插入所有列

  2.使用 select語句將現(xiàn)有表中的數(shù)據(jù)添加到已有的新表中

  語法: into <已有的新表> <列名>

  select <原表列名> from <原表名>

  例: into tongxunlu ('姓名','地址','電子郵件')

  select name,address,email

  from Strdents

  注意:into不可省略;查詢得到的數(shù)據(jù)個(gè)數(shù)、順序、數(shù)據(jù)類型等,必須與插入的項(xiàng)保持一致

  3.使用select into語句將現(xiàn)有表中的數(shù)據(jù)添加到新建表中

  語法:select <新建表列名> into <新建表名> from <源表名>例:select name,address,email into tongxunlu from strdents

  注意:新表是在執(zhí)行查詢語句的時(shí)候創(chuàng)建的,不能夠預(yù)先存在

  在新表中插入標(biāo)識(shí)列(關(guān)鍵字‘identity’):

  語法:select identity (數(shù)據(jù)類型,標(biāo)識(shí)種子,標(biāo)識(shí)增長(zhǎng)量) AS 列名

  into 新表 from 原表名

  例:select identity(int,1,1) as 標(biāo)識(shí)列,dengluid,password into tongxunlu from Struents

  注意:關(guān)鍵字‘identity’

  4.使用union關(guān)鍵字合并數(shù)據(jù)進(jìn)行插入多行

  語法: <表名> <列名> select <列值> tnion select <列值>

  例: Students (姓名,性別,出生日期)

  select '開心朋朋','男','1980/6/15' union(union表示下一行)

  select '藍(lán)色小明','男','19**/**/**'

  注意:插入的列值必須和插入的列名個(gè)數(shù)、順序、數(shù)據(jù)類型一致

  篇三:SQL常用增刪改查語句

  SQLSQL常用增刪改查語句

  作者:hiker

  一. Insert 插入語句

  1. Insert into 表名(列名) values (對(duì)應(yīng)列名值)//插入一行.

  2. Insert into 新表名(列名)

  Select (列名) 舊表名

  3. Select 舊表名.字段…

  Into 新表名 from 舊表名

  4. Select identity ( 數(shù)據(jù)類型,標(biāo)識(shí)種子,標(biāo)識(shí)增長(zhǎng)量) as 列名

  Into新表名

  From 舊表名

  5. Insert 表名(列名)

  Select (對(duì)應(yīng)列名值) union

  Select (對(duì)應(yīng)列名值) union

  Select (對(duì)應(yīng)列名值)

  二. Update 更新語句

  1. Update 表名 set 列名=’更新值’ where 更新條件

  三.  刪除語句

  1.  from 表名 where 刪除條件

  2. truncate table 表名 //刪除表中所有行

  四. select 基本查詢語句

  1. select 列名 from 表名 where 查詢條件

  order by 排序的列名asc或desc升/降

  2. select 列名 as 別名 from 表名 where 查詢條件

  3. select 列名 from 表名 where 列名 is null //查詢空值

  4. select 列名 , ‘常量值’ as 別名 from 表名//查詢時(shí)定義輸出一列常量值

  5. select top 5 列名 from 表名 //查詢前5行

  6. select top 5 percent 列名 from 表名 //查詢前百分之5的數(shù)據(jù)行

  五.

  1.

  2.

  3.

  4. select 函數(shù)查詢語句 selectLEN(Class_Name)fromClass //查詢class_Name字符串長(zhǎng)度 selectupper(Class_Name)fromClass //查詢class_Name并轉(zhuǎn)換為大寫 ltrim和rtrim //清除字符串左右空格 selectREPLACE(card_No,'0','9')fromCardRecord//修改列中字符串中的字符 列名字符串中0修改為9

  5. selectSTUFF(Card_No,2,3,'8888')fromCardRecord

  列名字符串中第2個(gè)開始刪除3個(gè)字符,再從第二個(gè)開始插入8888字符串

  6. selectGETDATE()//顯示系統(tǒng)日期

  六.

  1.

  2.

  3.

  4.

  5. select 高級(jí)查詢語句 select * from 表名 where列名 like ‘ %s%’ //模糊查詢 select * from 表名 where 列名 between 60 and 80 //范圍查詢 select * from 表名 where 列名 in (‘列舉’,’’,’’) //在列舉范圍內(nèi)查詢 selectSUM(Score_Num)fromscores //查詢分?jǐn)?shù)總和 avg max min count //查詢平均分/最大數(shù)/最小數(shù)/行數(shù)

  selectcourse_Id,SUM(Score_Num)fromscores

  groupbyCourse_Id//分組查詢

  havingCourse_Id='jsj001'//分組子句篩選

  七. Select 多表連接查詢語句

  1.selects.stu_Nameas'姓名',c.Course_nameas'科目',sc.Score_Num

  fromStudentsass

  innerjoinScoresasscon(sc.Stu_Id=s.Stu_ID)

  innerjoinCoursesascon(sc.Course_Id=c.Course_Id)

  orderbys.Stu_Namedesc //三表內(nèi)聯(lián)查詢

  2.selects.stu_Nameas'姓名',c.Course_nameas'科目',sc.Score_Num

  fromStudentsass

  leftouterjoinScoresasscon(sc.Stu_Id=s.Stu_ID)

  leftouterjoinCoursesascon(sc.Course_Id=c.Course_Id)

  //三表左外聯(lián)查詢,以stu表為主,其它表為從。

  3.selects.stu_Nameas'姓名',c.Course_nameas'科目',sc.Score_Num

  fromCoursesasc

  rightouterjoinScoresasscon(sc.Course_Id=c.Course_Id)

  rightouterjoinStudentsasson(sc.Stu_Id=s.Stu_ID)

  //三表右外聯(lián)查詢,以stu右表為主,其它表為從。

  八. Create 創(chuàng)建數(shù)據(jù)庫語句

  1. create database 數(shù)據(jù)庫名

  on[primary]

  (

  <數(shù)據(jù)文件參數(shù)>[,…n] [<文件參數(shù)>]

  )

  [log on]

  (

  {<日志文件參數(shù)> […n]}

  )

  文件參數(shù):

  Name=邏輯文件名,filename=物理文件名,size=大小,maxsize=最大容量,

  Filegrowth=增長(zhǎng)

  文件組參數(shù):

  Filegroup 文件組名<文件參數(shù)>

  例:

  usemaster

  go

  ifexists(select*fromsysdatabaseswherename='abc')

  dropdatabaseabc

  createdatabaseabc

  onprimary

  (

  name='abc',

  filename='d:abc.mdf',

  size=5,

  maxsize=50,

  filegrowth=10%

  )

  logon

  (

  name='abc_log',

  filename='d:abc_log.ldf',

  size=2,

  maxsize=20,

  filegrowth=1

  )

  2. use 數(shù)據(jù)庫名

  go

  create table 表名

  (

  字段數(shù)據(jù)類型列的特征

  )

  Go

  例:

  usedb_myschool

  go

  ifexists(select*fromsysobjectswherename='test1')

  droptabletest1

  createtabletest1

  (

  Idintnotnull,

  SNamenvar50)notnull,

  Telintnotnull

  )

  go

  3.使用SQL語句創(chuàng)建和刪除約束

  alter table表名

  Add constraint 約束名約束類型描述說明

  altertabledbo.testaddconstraintPK_IDprimarykey (ID)

  主鍵:primary keyPK_ 唯一:uniqueUQ_ 檢查:check CK_ 默認(rèn):defaultDF_外鍵:foreign keyFK_

  1.execsp_addlogin'abc','abc'//添加SQL用戶名

  usedb_myqq

  go

  execsp_grantdbaccess'abc'//添加用戶名到數(shù)據(jù)庫中

  3. 授權(quán)語句

  Grant 權(quán)限 on 表名 to 數(shù)據(jù)庫用戶名 九. 登錄驗(yàn)證語句

  十. SQL編程語句

  局部變量/全局變量

  1.以@標(biāo)記符作前綴

  Declare @name var8)//聲明

  Set @name = value

  Select @name=value//賦值

  2.以@@標(biāo)記符作前綴

  @@error //最后一個(gè)T-SQL錯(cuò)誤的錯(cuò)誤號(hào)

  @@identity //最后一次插入的標(biāo)識(shí)值

  @@language//當(dāng)前使用的語言的名稱

  @@max_connections //可以創(chuàng)建的同時(shí)連接的最大數(shù)目

  @@rowcount //受上一個(gè)SQL語句影響的行數(shù)

  @@servername//本地服務(wù)器的名稱

  @@servicename //該計(jì)算機(jī)上的SQL服務(wù)的名稱

  @@timeticks //當(dāng)前計(jì)算機(jī)上每刻度的微秒數(shù)

  @@transcount //當(dāng)前連接打開的事務(wù)數(shù)

  @@version //SQL Server的版本信息

  4. 輸出

  print'SQL服務(wù)名:'+@@servicename

  select@@SERVICENAMEas'SQL服務(wù)名'

  5. 邏輯控件語句

  declare@avgfloat

  select@avg=avg(Score_Num)fromScoreswhereStu_Id='sc0002'

  print'平均分為'+convert(var8),@avg)+'分'

  if(@avg>90)

  begin

  print'最高分'

  selectMAX(Score_Num)fromScores

  end

  else

  begin

  print'最低分'

  selectMIN(Score_Num)fromScores

  6. while 循環(huán)語句

  declare@nint

  while(1=1)

  begin

  select@n=COUNT(*)fromScoreswhereScore_Num<60

  if(@n>0)

  updateScoressetScore_Num+=2 whereScore_Num<60

  else

  break

  end

  print'加分后的成績(jī)'

  select*fromScores

  7. Case多分支語句

  selectStu_id,score=case

  whenScore_Num>90 then'A'

  whenScore_Numbetween 80 and 89 then'B'

  whenScore_Numbetween 60 and 79 then'C'

  else'D'

  end

  fromScores

  十一.高級(jí)查詢

  1. where子查詢

  2. in 和 not in 子查詢

  3. if exists (子查詢)

【SQl常用增刪改查模板】相關(guān)文章:

1.SQL常用增刪改查語句

2.SQL常用增刪改查語句大全

3.50個(gè)常用的SQL語句

4.SQL常用語句大全

5.SQL Server常用數(shù)據(jù)類型

6.ORACLE 常用的SQL語法和數(shù)據(jù)部分

7.Dedecms中常用數(shù)據(jù)調(diào)用的sql語句

8.數(shù)據(jù)庫常用sql語句有哪些