基本的oracle语句以及sql语法(二)
21.添加列名同时和约束:alter table EMP add (dept_id number(7) constraint my_emp_dept_id_fk references dept(ID));
22.改变列:alter table dept80 modify(last_name varchar2(30));
23.增加一行:insert into table_name values();
24.添加主键: alter Table EMP add constraint my_emp_id_pk primary key (ID);
25.添加一个有check约束的新列:alter table EMP add (COMMISSION number(2) constraint emp_commission_ck check(commission>0))
26.删除表:drop table emp;
27.创建视图:
CREATE VIEW empvu80
AS SELECT employee_id, last_name, salary
FROM employees WHERE department_id = 80;
28.删除视图:drop view view_name
29.找到工资最高的5个人。(top-n分析)(行内视图)
select rownum,employee_id from (select employee_id,salary from
employees order by salary desc)
where rownum<5;
30.建立同义词:
create synonym 同义词名 for 原来的名字
或者 create public synonym 同义词名 for 原来的名字
31.建立序列:
CREATE SEQUENCE dept_deptid_seq
INCREMENT BY 10
START WITH 120
MAXVALUE 9999
NOCACHE
NOCYCLE
32.使用序列:insert into dept(ID,NAME) values(DEPT_ID_SEQ.nextval,'Administration');
33.建立索引:CREATE INDEX emp_last_name_idx ON employees(last_name);
34.建立用户:
create user username(用户名)
identified by oracle(密码)
default tablespace data01(表空间名//默认存在system表空间里面)
quota 10M(设置大小,最大为unlimited) on 表空间名//必须分配配额!
35.创建角色:create ROLE manager
36.赋予角色权限:grant create table,create view to manage
37.赋予用户角色:grant manager to DENHAAN,KOCHHAR( 两个用户)
38.分配权限:
GRANT update (department_name, location_id)
ON departments
TO scott, manager;
39.回收权限
REVOKE select, insert
ON departments
FROM scott;
40.从时间中提取年,月,日:使用函数extract
select extract(year from sysdate) year,extract(month from sysdate),
extract(day from sysdate) from dual;