SQL SERVER 基礎語句學習 三

2022-05-04 14:18:11 字數 3763 閱讀 2640

1. join

tips:

(1). inner join 同 join

select

alias_name1.column_name, alias_name2.column_name

from table_name1 as

alias_name1

inner

join table_name2 as

alias_name2

on alias_name1.column3 =

alias_name2.column4

order

by alias_name1.column_name1

(2). left join, 即使右表沒有匹配,也返回左表所有行

select

alias_name1.column_name, alias_name2.column_name

from table_name1 as

alias_name1

left

join table_name2 as

alias_name2

on alias_name1.column_name3 =

alias_name2.column_name4

order

by alias_name1.column_name1

(3). right join, 即使左表沒有匹配,也返回右表所有行

select

alias_name1.column_name, alias_name2.column_name

from table_name1 as

alias_name1

right

join table_name2 as

alias_name2

on alias_name1.column_name3 =

alias_name2.column_name4

order

by alias_name1.column_name1

(4). full join, 返回所有行

select

alias_name1.column_name, alias_name2.column_name

from table_name1 as

alias_name1

full

join table_name2 as

alias_name2

on alias_name1.column_name3 =

alias_name2.column_name4

order

by alias_name1.column_name1

2. union 合併多個select 語句的結果

tips:

select 語句必須有相同數量的列,且有相似的資料型別,列的順序也必須相同

union 預設返回不同的值

union all 允許返回重複的值

結果中的列名為第乙個select 語句中的列名

select column_name(s) from

table_name1

union

select column_name(2) from table_name2

3. select into 從乙個表選資料插入另乙個表, 常用於備份復件或存檔記錄

select

*into

new_table_name

from old_table_name

tips:

(1). 從externaldatabase 選

select

*into

new_table_name

from externaldatabase_name.dbo.table_name

(2). 如果表2已經存在,則用 insert into

insert

into

table2_name(column1, column2,…)

select table1_column1,table1_column2,… from table1_name

4. constraint  約束

(1). not null

(2). unique  約束唯一,每個錶可有多個unique

可以不設定unique 名,系統有預設值,但是建議設定,方便使用; 可以同時設定多列

alter

table table_name add

unique

(column_name)

alter

table table_name add

constraint uq_name unique

(column_name1,column_name2)

alter

table table_name drop

constraint uq_name

(3). primary key  約束唯一,每個表只有乙個primary key

alter

table table_name add

constraint pk_name primary

key(column_name1,column_name2)

alter

table table_name drop

constraint pk_name

(4). foreign key

alter

table table_name1 add

constraint fk_name foreign

key(column_name1) references

table_name2(column_name2)

alter

table table_name drop

constraint fk_name

(5). check 約束列中值的取值範圍

alter

table table_name add

constraint chk_name check (column_name1 >

3and column_name2 like

'%abc%')

alter

table table_name drop

constraint chk_name

(6). default 設定預設值

alter

table table_name add

constraint df_name default

'dafult_value

'for

column_name

alter

table table_name drop

constraint df_name

查詢表是否有default 約束

select

*from sys.default_constraints where sys.default_constraints.parent_object_id=

object_id('

table_name

')

檢視表中的約束

exec sp_helpconstraint '

table_name

'

SQL Server 基礎語句學習 二

1.update update table name set column name value new where tips 更新多個值,用逗號分隔 2.delete delete from table name where tips 刪除全部資料 delete from table name 3...

SQL Server 基礎 SQL語句

本文列舉的一些常用的sql語句,從四類資料庫操作語言入手。增 insert into 表名 列1,列2,values 值1,值2,刪 delete from 表名 where 列名稱 值改 update 表名 set 列名 where 列名稱 某值查 基本格式 select 查詢內容 from 查詢...

SQL Server中語句的查詢基礎

首先建立資料庫各種語句的查詢 1 語法格式 select from authors 代表所有列,沒有where子句代表所有行 select authorname,email,age 多個列名間用逗隔開 from authors where age 20 order by age desc,autho...