1. 程式人生 > >【資料庫】SQL語言基礎

【資料庫】SQL語言基礎

SQL:結構化查詢語言,資料庫操作的國際標準語言。

SQL分為資料定義(CREATE、ALTER、DROP)、資料查詢(SELECT)、資料操縱(INSERT、DELETE、UPDATE)

資料控制(GRANT、REVOKE、COMMIT、ROLLBACK)。

SQL關鍵字、物件名、列名不區分大小寫。

1.按某一列(column)升序排列

select * from table order by column;

2.刪除表(表的結構、屬性以及索引也會被刪除)

drop table 表名稱;

3.刪除表中所有記錄(記錄清空,表結構還在)【效率比delete高】

truncate table 表名稱;

4.刪除表中某條/所有記錄(記錄清空,表結構還在)

delete from 表名稱;

delete from 表名稱 where 列名稱=值;

5.查詢表中某列/所有記錄

select * from 表名稱;

select 列名稱 from 表名稱;

6.建立表結構

create table dwq_test

(

   id              number(7) not null,

   name            varchar2(20) not null,

   email           varchar2(20) not null,

   status          number(1) not null,

   primary key (id)

);

7.插入表記錄

insert into dwq_test (id,name,email,status) values (1,'dwq','[email protected]',1);

8.查詢表所有記錄

select * from dwq_test;

9.更新表記錄中的列

【更新記錄中的一列】update dwq_test set name='jtd' where name='dwq';

【更新記錄中的多列】update dwq_test set name='lch', email='[email protected]

', status=0 where id=1;

10.刪除表中記錄

delete from dwq_test where id=1;

11.排序記錄【不可同時排序多個,order by後只能排序一列】

【預設升序排列(省略結尾的ASC),字母/數字】

<1>select * from dwq_t order by email;

<2>select * from dwq_t order by email ASC;

注:<1>和<2>等價。

【指定逆序排列,字母/數字】select * from dwq_t order by email DESC;

12.使用多條件查詢記錄

【and只能查詢一條記錄】select * from dwq_t where id=1 and name='dwq';

【or查詢多條記錄】select * from dwq_t where id=1 or name='jtd';

【and和or同時出現,需要加括號判定結合型】select * from dwq_t where id=1 or (name='lch' and name='dwq');

13.去掉一個列中重複的記錄【只能去掉一個列】

select distinct email from dwq_t;

14.PL/SQL中修改表資料

【加表名.rowid才能修改資料】select dwq_t.* , dwq_t.rowid from dwq_t;

15.查詢表中前兩行記錄

select * from dwq_tt where rownum <=2;

16.查詢一列中滿足字首/字尾的記錄

【滿足字首】select * from dwq_tt where email like '570%';

【滿足字尾】select * from dwq_tt where email like '%qq.com';

【滿足一段(模糊查詢)】select * from dwq_tt where email like '%0045%';

【不滿足一段(模糊查詢)】select * from dwq_tt where email not like '%0045%';

【用_代替一個字元,查詢第一個字元之後的字母,不適用於數字】select * from dwq_ttt where name like '_wq';

【用_代替多個字元,不適用於數字】select * from dwq_ttt where name like '_w_';

17.以某一列/多列為主進行組合

【一列為主組合】select Customer,sum(OrderPrice) from test_customer1 group by Customer;

【多列為主組合】

<1>select Customer,sum(OrderPrice) from test_customer1 group by Customer,OrderPrice;【】

<2>select Customer,OrderPrice from test_customer1;【插入資料順序】

<3>select Customer,sum(OrderPrice) from test_customer1 group by OrderPrice;【報錯,不能以OrderPrice為主】

注:查詢結果順序不同。

原表順序:

語句<1>順序:

語句<2>順序:

18.有函式時使用where條件查詢【where 和 函式不能同時出現】

select Customer,sum(OrderPrice) from test_customer1 group by Customer having sum(OrderPrice)>3000;

19.返回資料記錄的條數

【根據表中某一列(沒必要)】select count(OrderPrice) from test_customer1;

【根據表中某一列中滿足條件的記錄的條數(把列名“count(Customer)”改為“orders”)】select count(Customer) as orders from test_customer1 where Customer='dwq';

【表中所有記錄的條數】select count(*) from test_customer1;

【根據某一列的值(不重複計數)】select count(distinct Customer) from test_customer1;

20.求平均數【NULL 值不包括在計算中】

【非巢狀】select avg(orderprice) as avg from test_customer1;

【巢狀】select customer from test_customer1 where orderprice>(select avg(orderprice) from test_customer1);

21.查詢某一列的第一條/最後一條記錄【oracle中無first/last關鍵字】

【oracle報錯】select first(orderprice) as first_orderprice from test_customer1;

【oracle報錯】select last(orderprice) as last_orderprice from test_customer1;

22.求最大/小值【NULL 值不包括在計算中】

【比較一列的最大值】select max(orderprice) as max_orderprice from test_customer;

【比較一列的最小值】select min(orderprice) as min_orderprice from test_customer;

【比較一行(即多個列表)的最大值和最小值】

select id, chinese, math, english, greatest (chinese, math, english) max,least(chinese, math, english) min from score;

23.oracle註釋:【”--“後為註釋】

select * from test_customer;   --查詢

24.返回多列第一個非空值

select coalesce (chinese,math,english) from score;

25.字母大小寫轉換

【大寫】select upper(customer) as Upname from test_customer;

【小寫】select lower(customer) as Upname from test_customer;

26.求和

【某一列】select sum(orderprice) as sum_orderprice from test_customer;

27.

select * from test_customer;

select customer,max(orderprice) from test_customer;

(不是單組分組函式)

【改為:select customer,max(orderprice) from test_customer group by customer;】

28.check約束

CREATE TABLE Persons ( Id_P int NOT NULL CHECK (Id_P>0), LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )

29.default預設值

CREATE TABLE Persons ( Id_P int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) DEFAULT 'Sandnes' )

30.unique不可重複

CREATE TABLE Persons ( Id_P int NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )

31.級聯刪除【如果主表記錄刪除,則主表中作為外來鍵的記錄也刪除】

 表a保持不變 

create table a

(

    id varchar(20) primary key,

    password varchar(20) not null

);

 表b外來鍵資訊中加入級聯刪除 

create table b

(

name varchar(50) not null,

userId varchar(20),

foreign key (userId) references a(id) on delete cascade

);

32.case when

33.decode

34.over

partition by是批量分組,而group by是對某一個分組。

35.nvl

nvl(引數1,引數2)

當引數1為空時,值為引數2;

當引數1不為空時,值為引數1.

36.日期函式

【獲取系統日期】select sysdate from dual;

【把系統日期轉換成字串】select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;

【將日期字串轉換為日期型別]】insert into emp(hiredate) values(to_date('2004-10-10','yyyy-mm-dd'));

【將日期字串轉換成數字型別】select to_number(to_char(sysdate,'hh12')) from dual; 

37.字串擷取

select substr('abcdef',1,3) from dual;

38.查詢子串位置

select instr('abcfdgfdhd','fd') from dual ;

39.連線字串

select 'HELLO'||'hello world' from dual;

40.字串長度

select length('abcdef') from dual

41.修改列型別

【新型別和舊型別相容,舊型別列值為空】alter table 表名 modify(列名 新型別);

【新型別和舊型別不相容,舊型別有列值】

//修改舊列名

alter table 表名 rename column 舊列名 to 新列名;

//增加與舊列名相同的列

alter table 表名 add 列名 列型別;

//將修改後的列值更新到增加的列中

update 表名 set 增加的列名=trim(修改後的列名);

//刪除修改後的列

alter table 表名 drop column 修改後的列名;

42.sign

oracle sign 函式  取數字n的符號,大於0返回1,小於0返回-1,等於0返回0

SQL> select sign(100),sign(-100),sign(0) from dual;

sql執行順序

  1. FROM:對FROM子句中前兩個表執行笛卡爾積生成虛擬表vt1

  2. ON: 對vt1表應用ON篩選器只有滿足 join_condition 為真的行才被插入vt2

  3. OUTER(join):如果指定了 OUTER JOIN保留表(preserved table)中未找到的行將行作為外部行新增到vt2,生成t3,如果from包含兩個以上表,則對上一個聯結生成的結果表和下一個表重複執行步驟和步驟直接結束。

  4. WHERE:對vt3應用 WHERE 篩選器只有使 where_condition 為true的行才被插入vt4

  5. GROUP BY:按GROUP BY子句中的列列表對vt4中的行分組生成vt5

  6. CUBE|ROLLUP:把超組(supergroups)插入vt6,生成vt6

  7. HAVING:對vt6應用HAVING篩選器只有使 having_condition 為true的組才插入vt7

  8. SELECT:處理select列表產生vt8

  9. DISTINCT:將重複的行從vt8中去除產生vt9

  10. ORDER BY:將vt9的行按order by子句中的列列表排序生成一個遊標vc10

  11. TOP:從vc10的開始處選擇指定數量或比例的行生成vt11 並返回呼叫者