資料庫學習筆記(二)

2021-07-24 03:36:20 字數 4175 閱讀 2969

3.插入資料

3.1.單條插入方式—-指定列

指令碼:insert into table1(field1,field2,…) values()

3.2.單條插入方式—-不指定列

指令碼:insert into table1 values

3.3.多條插入—-into…select

指令碼:insert into table1(field1,field2,…) select value1,value2,… from table1

例子:

–1.建立測試表

create table table1

( a varchar(10),

b varchar(10),

c varchar(10),

constraint [pk_table1] primary key clustered

( a asc

) ) on [primary]

create table table2

( a varchar(10),

c varchar(10),

d int,

constraint [pk_table2] primary key clustered

( a asc

) ) on [primary]

go –2.建立測試資料

insert into table1 values(『趙』,』asds』,』90』)

insert into table1 values(『錢』,』asds』,』100』)

insert into table1 values(『孫』,』asds』,』80』)

insert into table1 values(『李』,』asds』,null)

go select * from table2

–3.insert into select語句複製表資料

insert into table2(a, c, d) select a,c,5 from table1

go –4.顯示更新後的結果

select * from table2

go –5.刪除測試表

drop table table1

drop table table2

3.4.多條插入—-select…into

指令碼:select vale1, value2 into table2 from table1

例子:

–1.建立測試表

create table table1

( a varchar(10),

b varchar(10),

c varchar(10),

constraint [pk_table1] primary key clustered

( a asc

) ) on [primary]

go –2.建立測試資料

insert into table1 values(『趙』,』asds』,』90』)

insert into table1 values(『錢』,』asds』,』100』)

insert into table1 values(『孫』,』asds』,』80』)

insert into table1 values(『李』,』asds』,null)

go –3.select into from語句建立表table2並複製資料

select a,c into table2 from table1

go –4.顯示更新後的結果

select * from table2

go –5.刪除測試表

drop table table1

drop table table2

3.5.多條插入—-mysql特殊方式

指令碼:insert into table1(field1,field2,…) values(…),(…),…;

例子:

–1.建立測試表

create table table1

( a varchar(10),

b varchar(10),

c varchar(10),

constraint [pk_table1] primary key clustered

( a asc

) ) on [primary]

go –2.建立測試資料

insert into table1(a,b,c) values(『趙』,』asds』,』90』),(『錢』,』asds』,』100』),(『孫』,』asds』,』80』),(『李』,』asds』,null)

4.刪除資料

4.1.drop

說明:刪除內容和表的定義,釋放空間

4.2.truncate

說明:刪除內容不刪除定義 釋放空間 不帶where

4.3.delete

說明:刪除內容不刪除定義 不釋放空間 帶where 一行一行刪除效率低

5.修改資料

5.1.sql server 多表更新方法

sql server語法:

update set | @variable = expression | @variable = column = expression } [ ,…n ]

[ ,…n ] ] [ where < search_condition > ] } | [

where current of | cursor_variable_name } ] } [

option ( < query_hint > [ ,…n ] ) ]

sql server示例:

update a set a.gqdltks=b.gqdltks,a.bztks=b.bztks from landleveldata a,gdqlpj b where a.geo_code=b.lxqdm

5.2.oracle 多表更新方法

oracle語法:

update updatedtable set (col_name1[,col_name2…])= (select

col_name1,[,col_name2…] from srctable [where where_definition])

oracel 示例:

update landleveldata a set (a.gqdltks, a.bztks)= (select b.gqdltks,

b.bztks from gdqlpj b where a.geo_code=b.lxqdm)

5.3.mysql 多表更新方法

mysql語法:

update table_references set col_name1=expr1 [, col_name2=expr2 …]

[where where_definition]

mysql示例:

update product p inner join productprice pp on p.productid = pp.productid

set pp.price = pp.price * 0.8 where p.datecreated < 『2004-01-01』

6.查詢資料

6.1.邏輯運算子

運算子 描述

and and運算子允許在乙個sql語句的where子句中的多個條件的存在。

between between運算子用於在給定最小值和最大值範圍內的一系列值中搜尋值。

exists exists運算子用於在滿足一定條件的指定表中搜尋行的存在。

in in運算子用於把某個值與一系列指定列表的值進行比較。

not in in運算子的對立面,用於把某個值與不在一系列指定列表的值進行比較。

like like運算子用於把某個值與使用萬用字元運算子的相似值進行比較。

glob glob運算子用於把某個值與使用萬用字元運算子的相似值進行比較。glob與like不同之處在 於,它是大小寫敏感的。

not not運算子是所用的邏輯運算子的對立面。比如not exists、not between、not in,等等。它是否定運算子。

or or運算子用於結合乙個sql語句的where子句中的多個條件。

is null null運算子用於把某個值與null值進行比較。

資料庫學習筆記二

1 合併兩個查詢結果 select from lmm lz union all select from lmm lz1 3 左連線 select lmm lz.lmm lz1.from lmm lz left join lmm lz1 on lmm lz.event lmm lz1.event 4 ...

資料庫學習筆記(二)

一,使用where選擇限定的資料行 操作符含義 等於 大於 大於或等於 小於 小於或等於 不等於 select ename,job,deptno from emp where deptno 20 比較日期型資料 select ename,job from emp where hiredate 01 ...

資料庫學習筆記二( 關聯式資料庫)

第二章 關聯式資料庫 一 sql 關係代數 關係演算 二 關係 1.域 變數有效範圍 2.笛卡爾乘積 域0x域1x域2x.x域n 1 3.關係 d1xd2xd3.dn的子集,r d1,d2,dn 4.鍵 由乙個或幾個屬性組成 a超鍵 唯一標示元組的屬性集 b候選鍵 無多餘屬性的超鍵 c主鍵 使用者選...