//建立表空間,表空間用於存儲數據庫對象,如表、視圖、存儲過程等。下面的語句建立一個名爲myspace的表空間,數據物理文件爲D:\oracle\myspace.dbf,大小爲100M //create tablespace myspace datafile 'D:\oracle\myspace.dbf' size 100M; //刪除表空間,先執行下面的sql而後刪除物理文件 //drop tablespace myspace; //建立用戶,用戶名爲test密碼爲test。該用戶建立的表默認保存在myspace表空間內。多個用戶能夠共享一個表空間 //create user test identified by test default tablespace myspace temporary tablespace temp; //爲用戶受權,通常只爲用戶授予connect,resource角色,由於dba角色權限過高,容易破壞數據庫,resource角色權限已經很高。用戶的角色權限能夠傳遞 //grant connect,resource,dba to test; /* 建立表和刪除表 指定表列名,數據類型,長度限制,列值完整性約束 主鍵約束保證唯一性和非空性 */ //create table users ( // id integer default 0, //default 約束默認值 // uname varchar2(10) not null unique, //not null 約束列值不容許爲空,unique不容許值重複 // birthday date, // height number(3,2), // folk varchar2(4), // primary key (id) //); //drop table users; //注意刪除表,全部的數據都會丟失 //修改表 //alter table users add (weight number(4,1) ,email varchar2(20)); //增長表列 //alter table users drop column weight; //刪除表列 //alter table users modify (email varchar2(100)); //改變列大小會受列已有數據的限制,如已有數據的類型,長度等 //刪除表主鍵 //alter table users drop primary key; //增長表主鍵 //alter table users add primary key (id); /* 視圖 視圖的做用: 視圖可以簡化用戶的操做。 視圖機制能夠使用戶以不一樣的方式看待同一數據 視圖對數據庫的重構提供了必定程度的邏輯獨立性 視圖能夠對機密的數據提供安全保護 建立視圖讀法: CREATE VIEW <視圖名> [(列名1,列名2,......,列名n)] AS <子查詢> [WITH CHECK OPTION]; */ //建立視圖 //create view v_users as select * from users; //建立有條件選擇的視圖,用戶只能看到身高大於1.75的用戶姓名和身高信息 //create view v_users as select uname,height from users where height>1.75; //drop view v_users; /* 基本查詢語法 SELECT <目標列組> FROM <數據源> [WHERE <元組選擇條件> ] [GROUP BY <分列組> [HAVING <組選擇條件> ]] [ORDER BY <排序列1> <排序要求1> [,…n]]; */ //查詢全部記錄的全部字段 //select * from users; //查詢部分字段 //select id,name,folk from users; //插入數據,注意主鍵和非空字段必須給定輸入值 //不指定列名,輸入的值順序必須和數據庫字段順序(建立表時的順序)一致;指定的值要和數據庫字段一樣多 //insert into users values (1,'user1',sysdate,1.75,'漢','[email]user1@email.com[/email]'); commit ; //按指定列的順序插入數據,而且能夠只插入部分數據 //insert into users(id,height,uname,folk) values (2,1.78,'user2','漢'); commit ; //插入測試數據 //insert into users ( ID, UNAME, BIRTHDAY, HEIGHT, FOLK, EMAIL) values (3,'USER3',sysdate,1.74,'鮮','[email]user3@email.com[/email]'); //insert into users ( ID, UNAME, BIRTHDAY, HEIGHT, FOLK, EMAIL) values (4,'user4',current_timestamp,1.76,'鮮','[email]user4@email.com[/email]'); //insert into users ( ID, UNAME, BIRTHDAY, HEIGHT, FOLK, EMAIL) values (5,'USER5',current_date,1.85,'漢','[email]user5@email.com[/email]'); //commit ; // //rollback ; //insert into users (id,uname,height,folk) values (6,'user6',1.73,'鮮'); //insert into users values (7,'user7', to_date('1980-01-01','yyyy-mm-dd') ,1.78,'漢','[email]user7@email.com[/email]'); //insert into users(id,uname,birthday,height) // select id+1 , substr(uname,1,length(uname)-1)||(id+1), sysdate,1.99 from users // where id = (select max(id) from users); //select * from users where id > 5; //rollback ; //修改數據,注意必定要用where子句限定條件,不然會影響表中全部的記錄(如:update users set height = 1.75 全部人的身高都會變爲1.75) //update users set height = 1.75,birthday=to_date('1982-08-15','yyyy-mm-dd') where id =1 ; commit ; //刪除數據,與update同樣要有嚴格的where子句限制 //delete from users where id = 4; commit ; /* DELETE 不能刪除個別的字段,它對於給定表只能刪除整條記錄 與INSERT 和UPDATE 同樣,刪除一個表中的記錄可能會致使與其它表的引用完整性問題。當對數據庫進行修改時必定在頭腦中有這個概念。 DELETE 語句只會刪除記錄不會刪除表,若是要刪除表需使用DROP TABLE命令 */ //Oracle不能建立自增列,當須要實現基於數據庫的自增列須要使用sequence對象,建立sequence簡單語句以下: //create sequence seq_pkgen; //查看sequence的當前值 //select seq_pkgen.currval from dual; //使用sequence對象實現列自增 //insert into users(id,uname,folk) values(seq_pkgen.nextVal,user3,'鮮'); commit ; /* select 語句完整語法: [select …] [from …] [where …] [group by …] [having …] [order by…] 除了變量值外,查詢語句對大小寫並不敏感。 因此 SeLeCT 與 sELEct 以及 SELECT 是相同的。 */ //查詢全部數據 //select * from users; //條件查詢 //select * from users where id = 5; //選取部分字段 //select id,uname from users; //select users.uname from users; //能夠爲列和表使用別名,用as關鍵字 //select uname as username from users as myusers; //列別名和表別名是能夠單獨使用的 //或 //select uname username from users; //select myusers.uname username from users myusers; //列別名是username,表別名是myusers,能夠用myusers.uname取得uname字段 /* SQL中的運算符 數學運算符+, -, *, / 字符串鏈接符...||... or concat(...,...) */ //數字和字符常量的計算 //select 1+2,5-6,2*3,6/5,'abc'||'def',concat('abc','def') from dual; //對字段進行運算 //select id+height,id*height from users; //常量與字段混合使用 //select uname||' email : '||email from users; /* 邏輯運算符and, or, not ,[not ]in, any ,all,exists,[not ]between .. and ,is [not] null,[not] like */ //select * from users where uname='a' and height = 20; //select * from users where uname='a' or height = 20; //select * from users where uname='a' and height = 20; //select * from users where uname='a' and height = 20 or uname='b'; //select * from users where exists (select * from users where id >= 5); //select * from users where exists // (select * from user_contact where users.id = 1 and user_contact.address = '北京市海淀區'); //同上 //select * from users ,user_contact where users.id = 1 and user_contact.address = '北京市海淀區'; //select * from users where id in (1,2); //select * from users where id > any (1,2); //select * from users where id > all (1,2); //select * from users where email is null; //select * from users where email is not null; //查詢用戶名爲user1和user2的用戶 //select * from users where uname in ('user1','user2') ; //查詢除了user1和user3外的全部用戶 //select * from users where uname not in ('user1','user3') ; //查詢身高在1.72和1.75之間的記錄,包括1.72和1.75 //select * from users where height between 1.72 and 1.77; //等價的語句 //select * from users where height >= 1.72 and height <= 1.77; //查詢email是null的記錄,注意null與''是不一樣的,null表明未知的意義,''是空字符串 //select * from users where email is null; //select * from users where height is not null; //select * from users where email like 'u%'; //select * from users where uname like 'user_'; //經過lower把字段內容變爲小寫再查詢,這樣實現不分大小寫的查詢 //select * from users where lower(email) like 'user%'; //變爲大寫 //select upper(uname),folk,email from users where upper(email) like 'USER%'; //能夠用()對查詢條件分組,改變優先級 //select * from users where (uname='user1' and email='[email]user1@email.com[/email]') or email='[email]user3@email.com[/email]'; /* 比較運算符=, >=, <=, <>, !=,^= */ //select * from users where height > 1.76; //select * from users where height >= 1.76; //select * from users where height <> 1.76; //select * from users where height != 1.76; //select * from users where height ^= 1.76; /* 組合查詢操做符和其餘SQL操做符 語法: <查詢1> <組合操做符> <查詢2> union 並集 minus 差查詢 intersect 交集 all distinct 抑制重複值 */ //select * from users where id >3 union select * from users where id <5 ; //select * from users where id >1 minus select * from users where id <3 ; //select * from users where id >1 intersect select * from users where id <3 ; //select avg(all height) from users; //通常與統計函數一塊兒使用 //select distinct folk from users; 本文轉自程式先鋒網站 www.javabiz.cn