1.表和列的命名規則
(1)必須由字母開始,字符長度爲1-30個字符,只能包含a-z A-Z 0-9 _ $ #
(2)同一個用戶所擁有的對象之間不能重名,不能用oracle服務器的保留字
(3)建議使用描述性的名字爲表和其餘數據庫對象命名
(4)表和列名對大小寫不敏感
2.create table語法
create table tbl_name(coll datatype,......) tablespace tablespace_name;
建立表
create table emp(emp_no number not null,emp_name varchar(50),constraint pk_emp primary key(emp_no));
select dbms_metadata.get_ddl('TABLE','EMP') from dual;
查看錶結構
3.增長備註
comment on table emp is 'employees table';
comment on column emp.emp_no is 'employees comment';
查看備註
set linesize 400;
col comments for a30;
select * from user_tab_comments where table_name='EMP';
TABLE_NAME TABLE_TYPE COMMENTS
------------------------------ ----------- ------------------------------
EMP TABLE employees table
SQL> select * from user_col_comments where table_name='EMP';
TABLE_NAME COLUMN_NAME COMMENTS
------------------------------ ------------------------------ ------------------
EMP EMP_NO employees comment
EMP EMP_NAME
4.用子查詢建立表
create table a as select owner,object_name,subobject_name from all_objects;
create table tbl_01(owner,object_name,subobject_name) as select owner,object_name,subobject_name from all_objects;
select count(*) from tbl_01;
COUNT(*)
----------
71859
5.制定default值
create table tbl_02(emp_no number,emp_name varchar(50) default 'shiwei',create_date date);
insert into tbl_02(emp_no,create_date) values(1,sysdate);
select * from tbl_02;
EMP_NO EMP_NAME CREATE_D
---------- -------------------------------------------------- --------
1 shiwei 10:33:14
6.刪除表
drop table a;
drop table a purge;
7.rename對象
rename old_name to new_name;
old_name:存在的table view sequence private synonym的名字
8.截斷表
truncat table a;
truncat 與delete的區別
truntcat釋放表空間
delete只刪除表數據
9.sequence序列號
oracle刪除序列化:
DROP SEQUENCE seq_a;
oracle建立序列化:
CREATE SEQUENCE seq_a
INCREMENT BY 1 -- 每次加幾個
START WITH 1 -- 從1開始計數
NOMAXVALUE -- 不設置最大值
NOCYCLE -- 一直累加,不循環
CACHE 10; -- 緩存存儲10個sequence,oracle默認存20個
oracle修改序列化:Alter Sequence
.若是想要改變 start值,必須 drop sequence 再create .
想改變序列化的minvalue必須刪除序列化後再從新創建序列化。
Alter sequence 的例子
ALTER SEQUENCE seq_a
INCREMENT BY 10
MAXVALUE 10000
CYCLE -- 到10000後從頭開始
NOCACHE ;
sequence的使用:
SELECT seq_a.nextval FROM dual; --當前值的下一個值,第一次執行爲開始值10
SELECT seq_a.currval FROM dual; -- 當前值
注:每個登陸用戶都必須先執行seq_a.nextval後,才能執行seq_a.currval,
不然會報ORA-08002: sequence seq_a.CURRVAL is not yet defined in this session錯誤
//查詢sequence
select sequence_name from user_sequences;
SELECT * FROM user_sequences ; -- 查詢當前用戶全部的sequences
SELECT * FROM all_sequences; --查詢全部用戶的sequences
create table tbl_03(id number,name varchar(50),primary key(id));
create sequence seq_tbl_03
minvalue 1
maxvalue 99999
start with 1
increment by 2
cache 20
nocycle;
初始化
select seq_tbl_03.nextval from dual;
insert into tbl_03 values(seq_tbl_03.currval,'guoyf');
insert into tbl_03 values(seq_tbl_03.nextval,'guo');
10.建立同義詞synonym
同義詞其實在使用一種替換操做,oracle會自動將同義詞替換爲它所表明的對象名,再對對象進行操做。
若是表和同義詞同名,oracle優先操做表。
語法:
create or replace [public] for objects_name;
SQL> create synonym offices for hr.locations;
SQL> select count(*) from offices;
COUNT(*)
----------
25
drop synonym offices;
11.約束(constrains)
oracle服務器用約束來防止無效數據輸入到表中。
約束作的事情:在插入行數據或者從表中刪除行數據時強制遵循規則。
not null
unique
primary key
foreign key:在列和引用表的一個列之間創建,而且強制一個外鍵關係
check:指定一個必須爲真的條件
drop table teacher purge;
create table teacher(
teacher_id number not null primary key,
teacher_name varchar2(50),
ages number(3) check (ages < 200));
drop table stu purge;
drop table student purge;
create table student(
stu_id number not null primary key,
teacher_id number not null,
stu_name varchar2(50),
address varchar2(50),
#constraint uk_name unique(stu_name),
#constraint pk_student primary key(stu_id)
);
alter table student add constraint fk_student_teacher foreign key(teacher_id) references teacher(teacher_id);
刪除teacher表中數據時,先刪除student表中的數據依賴
添加惟一索引
alter table student add constraint uk_name unique(stu_name);
添加check約束
alter table teacher add constraint check_ages check(ages<200);
查看約束
select owner,constraint_name,constraint_type,table_name,status from user_constraints;
CREATE TABLE promotions_var1
( promo_id NUMBER(6)
CONSTRAINT promo_id_u UNIQUE --自定義約束名
, promo_name VARCHAR2(20)
, promo_category VARCHAR2(15)
, promo_cost NUMBER(10,2)
, promo_begin_date DATE
, promo_end_date DATE
) ;
11.用戶管理
建立角色
create role dba_group1;
賦予角色權限
grant create session,create table,create view to dba_group1;
查看角色權限
select * from role_sys_privs where role='DBA_GROUP1';
建立用戶
create user test identified by test default tablespace tbs_01;
給用戶賦予dba_group1的權限
grant dba_group1 to test;
用戶鏈接
conn test/test
查看用戶權限
select * from user_sys_privs ;--爲空 由於直接賦予的是角色,不是具體權限
//角色視圖
---授予角色的系統權限
SQL> select * from role_sys_privs;
---可由用戶訪問的角色
SQL> select * from user_role_privs;
---授予用戶的系統權限
SQL> select * from user_sys_privs;
12.revoke回收權限
revoke create view from dba_group1;
revoke dba_group1 from test;
create user test identified by test;
grant connect, create view to test;
revoke create view from test;
數據庫