您的位置:首页 > 数据库 > Oracle

Oracle SQL Lesson (10) - 使用DDL语句创建和管理表

2013-11-03 16:09 429 查看
数据库对象
Table
View
Sequence
Index
Synonym

对象名称最长30个字符,不能与当前用户下其他对象重名。
create table "select" as select * from emp;
select * from "select";

必须有创建表的权限
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr][, ...]);

conn / as sysdba;
create user test identified by test default tablespace users;
grant create session to test;
conn test/test;
create table t1(id number);

conn / as sysdba;
grant create table to test;
conn test/test;
create table t1(id number);

alter user test quota 1m on users;
insert into t1 values(1111111111111111111111111111111111111111111111111111111);

引用其他用户的表
conn hr/hr;
grant select on employees to scott;
select * from hr.employees;

默认选项
CREATE TABLE hire_dates(id NUMBER(8),
hire_date DATE DEFAULT SYSDATE);

数据类型
LONG->CLOB
RAW(LONG RAW)->BLOB
create table t(d timestamp);
insert into t values(sysdate);
select * from t;
时间戳可以精确到秒后边的小数点。

约束(对列进行限制)
常用约束类型:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK

CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]
[column_constraint],
...
[table_constraint][,...]);

column [CONSTRAINT constraint_name] constraint_type,
column,...
[CONSTRAINT constraint_name] constraint_type
(column, ...),

列级别约束
CREATE TABLE employees(
employee_id NUMBER(6)
CONSTRAINT emp_emp_id_pk PRIMARY KEY,
first_name VARCHAR2(20),
...);
标记别约束
CREATE TABLE employees(
employee_id NUMBER(6),
first_name VARCHAR2(20),
...
job_id VARCHAR2(10) NOT NULL,
CONSTRAINT emp_emp_id_pk
PRIMARY KEY (EMPLOYEE_ID));

唯一约束
允许插入空值
CREATE TABLE employees1(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
department_id NUMBER(4),
CONSTRAINT emp_dept_fk FOREIGN KEY (department_id)
REFERENCES departments(department_id),
CONSTRAINT emp_email_uk UNIQUE(email));

alter table d add constraints pk_d primary key(deptno);
alter table e add constraints fk_e foreign key(deptno) references d;

外键约束关键字
FOREIGN KEY: Defines the column in the child table at the table-constraint level
REFERENCES: Identifies the table and column in the parent table
ON DELETE CASCADE: Deletes the dependent rows in the child table when a row in the parent table is deleted
ON DELETE SET NULL: Converts dependent foreign key values to null

检测约束:
..., salary NUMBER(2)
CONSTRAINT emp_salary_min
CHECK (salary > 0),...

约束综合示例
CREATE TABLE employees
( employee_id NUMBER(6) CONSTRAINT emp_employee_id PRIMARY KEY
, first_name VARCHAR2(20)
, last_name VARCHAR2(25) CONSTRAINT emp_last_name_nn NOT NULL
, email VARCHAR2(25)
CONSTRAINT emp_email_nn NOT NULL
CONSTRAINT emp_email_uk UNIQUE
, phone_number VARCHAR2(20)
, hire_date DATE CONSTRAINT emp_hire_date_nn NOT NULL
, job_id VARCHAR2(10) CONSTRAINT emp_job_nn NOT NULL
, salary NUMBER(8,2) CONSTRAINT emp_salary_ck CHECK (salary>0)
, commission_pct NUMBER(2,2)
, manager_id NUMBER(6) CONSTRAINT emp_manager_fk REFERENCES employees (employee_id)
, department_id NUMBER(4) CONSTRAINT emp_dept_fk REFERENCES departments (department_id));

更改表
ALTER TABLE employees READ ONLY;
ALTER TABLE employees READ WRITE;

alter table e add dname varchar2(10);
update e set dname = (select dname from d where e.deptno = d.deptno);
select * from e;

alter table e modify job varchar2(20);
alter table rename column job to job_new;
alter table e drop column dname;
alter table e raname to e1;

丢弃表
drop table e;
drop table e purge;
show recyclebin;

flashback table e to before drop;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: