【20060407發表于blog.csdn.net,20090424重新編輯】
關系型數據庫模型由Codd博士在1970年提出
SQL成為ANSI標準是在1986年
最基本查詢
select * from table1 where name=’Lincoln’;
select col1,col2 from table1;
無重復查詢
select distinct amount from checks;
(distinct在一個語句中只能用一次,放在所有字段之前)
MySQL的一些常用命令
show databases; 顯示所有數據庫
show tables; 顯示當前數據庫的表
use db1; 當前數據庫切換到db1
describe table1; 顯示table1表的表結構
運用表達式和別名
select item, wholesale+0.15 from price;
select item, (wholesale+0.15) retail from price;
select * from friends where state<=’la’;
select * from friends where firstname<>’al’;
select * from parts where location like ’%back%’;
select * from friends where st like ’C_’;
select firstname||lastname entirename from friends; (mysql中不能用)
(mysql可以用select concat(name1, ’ ’, name2) name from friends;)
select lastname from vacation where years<=5 and leavetaken>20;
select * from vacation where lastname not like ’B%’;
集合運算,MySQL不一定能用
select name from softball union select name from football; 合集
select name from softball union all select name from football; 全合集
select * from football intersect select * from softball; 交集
select * from football minus select * from softball; 差集
In與Between運算符
select * from friends where areacode in(100,381,204);
select * from price where wholesale between 0.25 and 0.75;
內置函數的使用
count 求總數
sum 求總和
avg 求平均
max 求最大
min 求最小
variance 標準方差
stddev 標準差
add_month 給日期類型增加一月
add_date 給日期類型增加一天
last_day 返回月份的最后一天
month_between 日期相差的月份
sysdate 系統時間
abs 絕對值
ceil “天花板”
floor “地板”
exp 指數函數
log 上面的反函數
pow a^b
sign 正1零0負-1
sqrt 根號
chr 轉換成字符
concat 連接字符串
initcap 首字符大寫,其他小寫
lower 小寫
upper 大寫
lpad 左填充,原始,長度,字符
rpan 右填充
ltrim 左剪切
rtrim 右剪切
trim 剪切
replace 字符替換,原始,替換,替換成
substr 獲取子字符串,原始,起始,長度
instr 查找字符串,原始,查找,開始,序號
length 字符串長度
to_char 轉換成字符串
to_number 轉換成數字
子句的運用
(Where, Group by, Order by, Having)
select * from checks where amount>100;
select * from checks order by check; 可以加上asc代表升序
select * from checks order by payee desc;
select * from checks order by remarks, payee;
select * from checks order by 1;
select payee, sum(amount), count(payee) from checks group by payee;
select sum(amount), count(payee) from checks group by payee, remarks;
where之句中不允許用合計函數,因此下面的語句是錯誤的。
select team, avg(salary) from orgchart where avg(salary)<3800 group by team;
這時候需要having子句:
select team, avg(salary) from orgchart group by team having avg(salary)<3800;
…… having team in(’pr’,’research’);
交叉連接(笛卡爾積)
select * from table1, table2;
select o.orderedon, o.name, o.partnum, p.partnum, p.description
from orders o, part p
where o.partnum=p.partnum;
內部連接
select p.partnum, p.price, o.name, o.partnum
from part p inner join orders o on orders.partnum=54;
除了使用了“on”來代替“where”之外,和交叉連接沒多少區別。
外部連接
左連接:左邊的表全部顯示
select p.partnum, p.description, p.price, o.name, o.partnum
from part p left outer join orders o on o.partnum=54;
右連接:右邊的表全部顯示
select p.partnum, p.description, p.price, o.name, o.partnum
from part p right outer join orders o on o.partnum=54;
子查詢(MySQL不支持)
select * from orders
where partnum=(select partnum from part where description like “ROAD%”);
下面是較復雜查詢示例:
select o.name, o.orderedon, o.quantity * p.price total
from orders o, part p
where o.partnum=p.partnum and o.quantity * p.price >
(select avg(o.quantity * p.price)
from orders o, part p
where o.partnum=p.partnum);
Exist使用(判斷集合是否存在)
select name, orderedon from orders where exists
(select * from orders where name=’Mostly harmless’)
數據庫三范式
1、數據集合分成多張表而不只是一張大表,分成的每張表都有主鍵;主鍵
2、找出僅僅依賴于主鍵的列,將其存儲在另一個表中;僅依賴的列抽出
3、從一個表中刪除不依賴于主鍵的列。不依賴的列抽出
缺點:降低性能。
建立數據庫
很難吧?很難!不難吧?不難。僅僅告訴你最簡單情況:
create database PAYMENTS;
建立表
create table bills(name char(30), amount number, account_id number);
create table empname(id number not null, ename char(30));
create table newtable as select * from oldtable;
改變表:增加列
alter table emp add new_col_name char(20);
改變表:修改列
alter table emp modify new_col_name char(21);
改變表:改列名(Oracle 9i Release 2才能用)
alter table emp rename column new_col_name to old_col_name;
改變表:刪除列
alter table emp drop column old_col_name;
刪除表
drop table tablename;
刪除數據庫
drop database databasename;
主鍵、非空、唯一約束
主鍵primary key
非空not null
唯一unique 除了排序,和primary key功能一致
create table emp
(emp_id char(9) primary key,
emp_name varchar2(40) not null,
phone number(10) null unique);
外鍵約束foreign key
create table emp_pay
(emp_id char(9) not null,
position varchar2(15) not null,
pay_rate number(4,2) not null);
alter table emp_pay add constraint emp_id_fk foreign key(emp_id)
references emp(emp_id);
校驗約束check
create table emp
(emp_id char(9) not null primary key,
emp_name varchar2(40) not null,
emp_rate number(4,2) not null,
zip number(5) not null);
alter table emp add constraint chk_zip check(emp_zip = '46234');
alter table emp add constraint chk_zip check(emp_zip in ('47634', '13451'));
alter table emp add constraint chk_zip check(emp_rate < 12.5);
emp_rate為校驗約束名。
刪除約束
alter table emp drop constraint emp_no_constraint;
emp_no_constraint為約束名。
更新記錄
update collection set worth = 555, price = 666 where itemid = 1110;
刪除記錄
delete from collection where itemid = 1113;
事務處理
……
commit;
……
rollback;
……
savepoint save_it;
……
rollback to savepoint save_it;
日期時間
掌握兩個函數就可以了。
to_char(empdate, 'YYYY/MM/DD HH24:MI:SS');
將date轉變成字符串:“2004/11/12 16:30:02”
to_date('1981/11/12 00:03:16', 'YYYY/MM/DD HH24:MI:SS');
將字符串“1981/11/12 00:03:16”轉變成日期類型。
還有要注意的事項,在中文Oracle中,'26-JAN-03'并不被認為是合法的日期,
'26-1月-03'才是合法的,真別扭,也蠻郁悶的。
建立視圖
create view debts as select * from bills;
刪除視圖
drop view debts;
建立索引
create index empno_index on emp(empno);
刪除索引
drop index empno_index;
創建用戶
create user jguogang identified by mypassword;
授予用戶角色
grant connect to jguogang;
grant resource to jguogang;
grant dba to jguogang;
刪除用戶的角色
revoke resource from jguogang;
給予系統特權
grant alter any type to public;
其中“alter any type”為一種系統特權,將被授予全部用戶。
grant drop any trigger to jguogang;
將系統特權“drop any trigger”授予用戶“jguogang”。
grant create user to connect;
將系統特權“create user”授予角色“connect”。
收回系統特權
revoke alter any type from public;
給予對象特權
grant select on emp to jack;
授予用戶“jack”:對“emp”的“select”對象特權。
grant select, update(salary) on emp to jill;
授予用戶“jill”:對“emp”的“select”、“salary”列的“update”對象特權。
收回對象特權
revoke select on emp from jack;
字典
字典:我是誰
select * from user_users;
字典:我們是誰
select * from all_users;
字典:我能干什么
select * from user_sys_privs;
字典:我充當什么角色
select * from user_role_privs;
字典:我擁有什么表、視圖和“SEQUENCE”
select * from user_catalog;
字典:我可訪問什么表、視圖和“SEQUENCE”(多)
select * from all_catalog;
字典:我擁有什么對象
select * from user_object;
字典:我可訪問什么對象(多)
select * from all_catalog;
……關于字典,暫時就介紹那么點了。
注釋
表的注釋
comment on table emp is 'Employee';
列的注釋
comment on column emp.ename is '名字';
列出所有包含注釋的表
select * from user_tab_comments where comments is not null;
系統時間取得
select sysdate from dual;
20050906筆記
查看未提交的事務
select * from v$transaction;
select * from v$locked_object;
通過locked_object視圖獲知被鎖定的對象的ID(object_id)
那么通過下面的語句能獲知到底哪個對象被鎖定
select * from sys.all_objects t where object_id = ###;
alter system kill session 30;
修改用戶密碼相關
select username,password from dba_users;
alter user aaa identified by aaaspwd;