課程來源:慕課網(wǎng)-Oracle數(shù)據(jù)庫開發(fā)必備利器之SQL基礎(chǔ)踢械; 課程地址:http://www.imooc.com/learn/337
這是我所學(xué)習(xí)總結(jié)的筆記蜈抓,新手報(bào)到驯妄,有不對(duì)的地方請(qǐng)留言指教能耻。
一帆竹、用戶
1.登錄用戶:
username/password
2.切換用戶:
connect username/password
3.啟用用戶語句:
alter user username account unlock
4.查看當(dāng)前用戶:
show user
二、表空間
5.創(chuàng)建表空間:
create [temporary] tablespace tablespace_name tempfile|datafile 'xxx.dbf' size file_size(10M);
ss
6.查看表空間位置:
desc dba_data_files;
select file_name from dba_data_files where tablespace_name='TABLE_SPACENAME';
7.設(shè)置表空間聯(lián)機(jī)/脫機(jī)脓规,只讀/讀寫狀態(tài):
alter tablespace tablespace_name online/offline|read only/read write;
8.為表空間添加/刪除數(shù)據(jù)文件:
alter tablespace tablespace_name add/drop datafile 'xxx.dbf' [size 10M];
9.刪除表空間[和表空間的數(shù)據(jù)文件]:
drop tablespace tablespace_name [including contents];
三栽连、管理表修改表
10.創(chuàng)建表:
create table table_name
( column_name number(6,0),
column_name varchar2(30)...
);
11.為表添加字段:
alter table table_name add new_column datatype;
12.更改表字段的數(shù)據(jù)類型:
alter table table_name modify column datatype;
13.刪除字段:
alter table table_name drop column column_name;
14.修改字段名:
alter table table_name rename column column_name to new_column_name;
15.修改表名:
rename table_name to new_table_name;
16.截?cái)啾恚▌h除表中的數(shù)據(jù)):
truncate table table_name;
17.刪除表(刪除數(shù)據(jù)同時(shí)表也刪除了):
drop table table_name;
18.為表添加值:
insert into table_name (column1,column2,...) values(value1,value2,...);
19.為表字段設(shè)置默認(rèn)值:
alter table table_name modify column default default_value;
20.復(fù)制表(創(chuàng)建的時(shí)候復(fù)制):
create table table_name as select (column1,columnn3...)|* from copy_table_name;
21.復(fù)制表(插入表的時(shí)候復(fù)制):
insert into table_name (column1,...) select (column1,...) from copy_table_name;
22.修改,更新表:
update table_name set column_name='***' [where conditions]
23.刪除表數(shù)據(jù):
delete from table_name [where conditions];
四侨舆、約束
24.非空約束:
alter table table_name modify column_name datatype NUT NULL;
25.主鍵約束:
1:create table table_name (column column_name primary key,...);(創(chuàng)建表時(shí)直接在需要設(shè)置主鍵約束的字段后面添加 primary key)
2:create table table_name (col col_name,...,constraint constraint_name1 key(col1,col2,...);(創(chuàng)建表時(shí)設(shè)置了約束名constraint)
3:修改表時(shí)添加主鍵約束:
alter table table_name add constraint constraint_name primary key(column);
26.修改主鍵名:
alter table table_name rename constraint constraint_name to new_name;
27.創(chuàng)建表時(shí)添加外鍵約束:
create table table2(col2 datatype references table1(col1),...);
28.創(chuàng)建表時(shí)設(shè)置外鍵約束(接連刪除):
create table table2(col_new datatype,..., constraint constraint_name foreign key(col_new) references table1(col)[on DELETE CASCADE]);
29.修改表時(shí)添加外鍵約束:
alter table table2 add constraint constraint_name foreign key(col2) references table1(col1);
30.禁用/刪除外鍵約束:
alter table table_name disable|enable constraint constraint_name;
alter table tablename drop constraint constraint_name;
31.創(chuàng)建唯一約束:(唯一約束的字段可以為null,而主鍵約束不能秒紧;一張表中唯一約束可以為多個(gè),主鍵約束只能有一個(gè))
create table table_name (col datatype unique,...);
或者:create table table_name (col datatype,constraint constraint_name unique(col));
32.修改表的時(shí)候添加唯一約束:
alter table table_name add constraint constraint_name unique(column);
33.檢查約束:
create table table_name (col datatype check(expressions),...);
34.檢查約束:
alter table table_name add constriant constrian_name check(expressions);
35.刪除主鍵約束:
alter table table_name drop primary key;
五挨下、查詢
36.基本查詢:
select [distinct] *|col_name,.. from table_name [where conditions];(distinct去掉重復(fù)的記錄)
37.更改查詢結(jié)果顯示的字段名:
col|column col_name heading col_name_new;
38.設(shè)置查詢結(jié)果顯示的字段長(zhǎng)度:
col|column col_name format a10/999.9(字符/數(shù)字的格式例$99.9|¥999.9);
39.清除設(shè)置的字段格式:
col column_name clear;
40.查詢的時(shí)候更改查詢結(jié)果字段名的顯示:
select col as new_col,col2 as col2_new from table_name;
41.運(yùn)算符優(yōu)先級(jí):
比較運(yùn)算符高于邏輯運(yùn)算符熔恢,not高于and高于or
42.模糊查詢(關(guān)鍵字:like,通配符:,%):
select * from table_name where username like 'a%'|'a_'|'a%';(代表一個(gè)字符,%代表一個(gè)或者多個(gè)字符臭笆。這里查詢用戶名已a(bǔ)開頭|其他類型);
43.范圍運(yùn)算符(between and,in/not in):
select * from table_name where salary between 800 and 2000;
select * from table_name where username in('aaa', 'bbb');
44.對(duì)查詢結(jié)果進(jìn)行排序(order by...desc/asc):
select ... from table_name [where] order by column1 desc/asc,...
45.case...when...then語句:
例1:select username,case username when 'aaa' then '計(jì)算機(jī)部門' when 'bbb' then '市場(chǎng)部門' else '其他部門' end as 部門 from table_name;
例2:select salary,case when salary<1000 then '底薪階層' when salary between 1000 and 3000 then '中等階層' when salary>5000 then '高新階層' else '其他' end as 薪資階層 from table_name;
46.decode函數(shù):
select username,decode(username,'aaa','計(jì)算機(jī)部門','bbb','市場(chǎng)部門','其他')as 部門 from table_name;
結(jié)束語:合抱之木叙淌,生于毫末秤掌;?九層之臺(tái),起于累土鹰霍;?千里之行闻鉴,始于足下。