《Oracle總結 05》--檢視、序列、索引及約束等用法
1:建立一個檢視,包含20號部門的員工資訊,欄位:empno,ename,sal,job,deptno
CREATE VIEW v_emp007_20 AS
SELECT empno,ename,sal,job,deptno FROM emp_xiangyoulu WHERE deptno=20;
SELECT * FROM v_emp007_20;
2:建立一個序列seq_emp_no,從10開始,步進為10
create sequence seq_emp008_no start with 1 increment by 2;
CREATE SEQUENCE seq_emp007_no START WITH 10 INCREMENT BY 10;
3:編寫SQL語句檢視seq_emp_no序列的下一個數字
select seq_emp007_no.nextval from dual;
select seq_emp008_no.nextval from dual;
4:編寫SQL語句檢視seq_emp_no序列的當前數字
select seq_emp007_no.currval from dual;
5:為emp表的ename欄位新增索引:idx_emp_ename
create index idx_emp_ename on emp_xiangyoulu(ename);
6:為emp表的LOWER(ename)欄位新增索引:idx_emp_lower_ename
CREATE INDEX idx_emp007_lower_ename ON emp_xiangyoulu(LOWER(ename));
7:為emp表的sal,comm新增多列索引
CREATE INDEX idx_emp007_sal_comm ON emp_xiangyoulu(sal,comm);
8:建立myemployee表,欄位:
id NUMBER(4) ,
name VARCHAR2(20),
birthday DATE,
telephone VARCHAR2(11)
scoreNUMBER(9,2)
其中id作為主鍵,name要求不能為空,telephone需要唯一,score值必須>=0
CREATE TABLE myemployee_xiangyoulu(
id NUMBER(4) PRIMARY KEY,
name VARCHAR2(20) NOT NULL,
birthday DATE,
telephone VARCHAR2(11) ,
score NUMBER(9,2),
CONSTRAINT myemployee_xiangyoulu_bir_uc UNIQUE(telephone)
);
ALTER TABLE myemployee_xiangyoulu
ADD CONSTRAINT employee_xiangyoulu_score_check
CHECK(score>=0);