一、許可權相關
1. 登陸
sqlplus system/admin as sysdba; //管理員身份登陸system帳戶
sqlplus scott/tiger; //登陸普通使用者scott
2. 系統管理使用者
create user wahaha; //在系統管理員帳戶下,建立使用者zhangsan
alert user scott identified by tiger; //修改密碼
create user zhangsan; //在系統管理員帳戶下,建立使用者zhangsan
alert user scott identified by tiger; //修改密碼
3. 授予許可權
/*管理員授權*/
grant create session to wahaha; //授予zhangsan使用者建立session的許可權,即登陸許可權,都要有
grant unlimited session to wahaha; //授予zhangsan使用者使用資料表空間的許可權
grant create table to wahaha; //授予建立表的許可權
grante drop table to wahaha; //授予刪除表的許可權
grant insert table to wahaha; //插入表的許可權
grant update table to wahaha; //修改表的許可權
grant all to public; //這條比較重要,授予所有許可權(all)給所有使用者(public)
/*oralce對許可權管理比較嚴謹,普通使用者之間也是預設不能互相訪問的*/
grant select on tablename to wahaha; //授予zhangsan使用者查看指定表的許可權
grant drop on tablename to wahaha; //授予刪除表的許可權
grant insert on tablename to wahaha; //授予插入的許可權
grant update on tablename to wahaha; //授予修改表的許可權
grant insert(id) on tablename to wahaha;
grant update(id) on tablename to wahaha; //授予指定表特定欄位的插入和修改許可權,注意,只能是insert和update
grant alert all table to wahaha; //授予zhangsan使用者alert任意表的許可權
4. 撤銷許可權
基本文法同grant,關鍵字為revoke
5. 查看許可權
select * from user_sys_privs; //查看目前使用者所有許可權
select * from user_tab_privs; //查看所用使用者對錶的許可權
select * from user_sys_privs; //查看目前使用者所有許可權
select * from user_tab_privs; //查看所用使用者對錶的許可權
6. 授予使用者管理員權限
create user songuo identified by admin;
grant create session to songuo with admin option;
gran umlimited tablespace to songuo with admin option;
grant dba to songuo with admin option;
二、 表結構
1. 增加欄位
ALTER TABLE products ADD description text;
也可以同時在該欄位上定義約束:
ALTER TABLE products ADD description text CHECK (description <> '100');
2. 刪除欄位
ALTER TABLE products DROP COLUMN description [CASCADE];
3. 增加約束
要增加一個約束,使用資料表條件約束文法, 如:
ALTER TABLE products ADD CHECK (name <> '');
ALTER TABLE products ADD CONSTRAINT some_name UNIQUE (product_no);
ALTER TABLE products ADD FOREIGN KEY (product_group_id) REFERENCES product_groups;
ALTER TABLE Teacher add constraint df_sex default('男') for sex
要增加一個不能寫成資料表條件約束的非空約束,使用下面文法:
ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;
這個約束將立即進行檢查,所以表在添加約束之前必須符合約束條件。
4. 刪除約束
ALTER TABLE products DROP CONSTRAINT some_name [CASCADE];
刪除非空類型:
ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL;
5. 修改一個欄位的資料類型
ALTER TABLE 表名 modify(欄位名 varchar2(20), 欄位名 varchar2(50))
ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);
6. 給欄位改名字
ALTER TABLE products RENAME COLUMN product_no TO product_number;
8、給表改名字
ALTER TABLE products RENAME TO items;