oracle create table(转)
- //建测试表
- create table dept(
- deptno number(3) primary key,
- dname varchar2(10),
- loc varchar2(13)
- );
- create table employee_info(
- empno number(3),
- deptno number(3),
- ename varchar2(10),
- sex char(1),
- phone number(11),
- address varchar2(50),
- introduce varchar2(100)
- );
- --
- //0.重命名
- //0.1 表:rename dept to dt;
- rename dt to dept;
- //0.2 列:alter table dept rename column loc to location;
- alter table dept rename column location to loc;
- //1.添加约束
- //1.1 primary key
- alter table employee_info add constraint pk_emp_info primary key(empno);
- //1.2 foreign key
- alter table employee_info add constraint fk_emp_info foreign key(deptno)
- references dept(deptno);
- //1.3 check
- alter table employee_info add constraint ck_emp_info check
- (sex in ('F','M'));
- //1.4 not null
- alter table employee_info modify phone constraint not_null_emp_info not null;
- //1.5 unique
- alter table employee_info add constraint uq_emp_info unique(phone);
- //1.6 default
- alter table employee_info modify sex char(2) default 'M';
- //2.添加列
- alter table employee_info add id varchar2(18);
- alter table employee_info add hiredate date default sysdate not null;
- //3.删除列
- alter table employee_info drop column introduce;
- //3.修改列
- //3.1 修改列的长度
- alter table dept modify loc varchar2(50);
- //3.2 修改列的精度
- alter table employee_info modify empno number(2);
- //3.3 修改列的数据类型
- alter table employee_info modify sex char(2);
- //3.4 修改默认值
- alter table employee_info modify hiredate default sysdate+1;
- //4.禁用约束
- alter table employee_info disable constraint uq_emp_info;
- //5.启用约束
- alter table employee_info enable constraint uq_emp_info;
- //6.延迟约束
- alter table employee_info drop constraint fk_emp_info;
- alter table employee_info add constraint fk_emp_info foreign key(deptno)
- references dept(deptno)
- deferrable initially deferred;
- //7.向表中添加注释
- comment on table employee_info is 'information of employees';
- //8.向列添加注释
- comment on column employee_info.ename is 'the name of employees';
- comment on column dept.dname is 'the name of department';
- //9.清除表中所有数据
- truncate table employee_info;
- //10.删除表
- drop table employee_info;
- --
- //下面来看看刚刚才我们对表dept和表employee_info所做的更改
- //user_constraints视图里面包含了刚刚才我们创建的所有约束,以及其他信息,
- //你可以用desc user_constraints命令查看其详细说明
- select constraint_name,constraint_type,status,deferrable,deferred
- from user_constraints
- where table_name='EMPLOYEE_INFO';
- --
- CONSTRAINT_NAME CONSTRAINT_TYPE STATUS DEFERRABLE DEFERRED
- ------------------------------ --------------- -------- -------------- ---------
- PK_EMP_INFO P ENABLED NOT DEFERRABLE IMMEDIATE
- FK_EMP_INFO R ENABLED DEFERRABLE DEFERRED
- NOT_NULL_EMP_INFO C ENABLED NOT DEFERRABLE IMMEDIATE
- SYS_C005373 C ENABLED NOT DEFERRABLE IMMEDIATE
- UQ_EMP_INFO U ENABLED NOT DEFERRABLE IMMEDIATE
- CK_EMP_INFO C ENABLED NOT DEFERRABLE IMMEDIATE
- //我们可以通过user_cons_columns视图查看有关列的约束信息;
- select owner,constraint_name,table_name,column_name
- from user_cons_columns
- where table_name='EMPLOYEE_INFO';
- --
- OWNER CONSTRAINT_NAME TABLE_NAME COLUMN_NAME
- ------------------------------ ------------------------------ ------------------------------ ---------------
- YEEXUN PK_EMP_INFO EMPLOYEE_INFO EMPNO
- YEEXUN CK_EMP_INFO EMPLOYEE_INFO SEX
- YEEXUN NOT_NULL_EMP_INFO EMPLOYEE_INFO PHONE
- YEEXUN SYS_C005373 EMPLOYEE_INFO HIREDATE
- YEEXUN UQ_EMP_INFO EMPLOYEE_INFO PHONE
- YEEXUN FK_EMP_INFO EMPLOYEE_INFO DEPTNO
- //我们将user_constraints视图与user_cons_columns视图连接起来
- //查看约束都指向哪些列
- column column_name format a15;
- select ucc.column_name,ucc.constraint_name,uc.constraint_type,uc.status
- from user_constraints uc,user_cons_columns ucc
- where uc.table_name=ucc.table_name and
- uc.constraint_name=ucc.constraint_name and
- ucc.table_name='EMPLOYEE_INFO';
- --
- COLUMN_NAME CONSTRAINT_NAME CONSTRAINT_TYPE STATUS
- --------------- ------------------------------ --------------- --------
- EMPNO PK_EMP_INFO P ENABLED
- DEPTNO FK_EMP_INFO R ENABLED
- PHONE NOT_NULL_EMP_INFO C ENABLED
- HIREDATE SYS_C005373 C ENABLED
- PHONE UQ_EMP_INFO U ENABLED
- SEX CK_EMP_INFO C ENABLED
- --
- //这里有个constraint_type,他具体指下面几种类型:
- //C:check,not null
- //P:primary key
- //R:foreign key
- //U:unique
- //V:check option
- //O:read only
- --
- //我们可以通过user_tab_comments视图获得对表的注释
- select * from user_tab_comments
- where table_name='EMPLOYEE_INFO';
- TABLE_NAME TABLE_TYPE COMMENTS
- ------------------------------ ----------- --------------------------
- EMPLOYEE_INFO TABLE information of employees
- --
- //我们还可以通过user_col_comments视图获得对表列的注释:
- select * from user_col_comments
- where table_name='EMPLOYEE_INFO';
- --
- TABLE_NAME COLUMN_NAME COMMENTS
- ------------------------------ ------------------------------ ---------------------------
- EMPLOYEE_INFO EMPNO
- EMPLOYEE_INFO DEPTNO
- EMPLOYEE_INFO ENAME the name of employees
- EMPLOYEE_INFO SEX
- EMPLOYEE_INFO PHONE
- EMPLOYEE_INFO ADDRESS
- EMPLOYEE_INFO ID
- EMPLOYEE_INFO HIREDATE
- --
- select * from user_col_comments
- where table_name='EMPLOYEE_INFO' and
- comments is not null;
- --
- TABLE_NAME COLUMN_NAME COMMENTS
- ------------------------------ ------------------------------ ------------------------
- EMPLOYEE_INFO ENAME the name of employees
- --
- //最后我们来查看一下修改后的表:
- desc employee_info;
- Name Type Nullable Default Comments
- -------- ------------ -------- --------- ---------------------
- EMPNO NUMBER(2)
- DEPTNO NUMBER(3) Y
- ENAME VARCHAR2(10) Y the name of employees
- SEX CHAR(2) Y 'M'
- PHONE NUMBER(11)
- ADDRESS VARCHAR2(50) Y
- ID VARCHAR2(18) Y
- HIREDATE DATE sysdate+1
- --
- desc dept;
- Name Type Nullable Default Comments
- ------ ------------ -------- ------- ----------------------
- DEPTNO NUMBER(3)
- DNAME VARCHAR2(10) Y the name of department
- LOC VARCHAR2(50) Y
oracle create table(转)的更多相关文章
- oracle, create table, insufficient privileges
SQL> exec pro_gz_day_report; ORA-01031: insufficient privileges ORA-06512: at & ...
- create table 使用select查询语句创建表的方法分享
转自:http://www.maomao365.com/?p=6642 摘要:下文讲述使用select查询语句建立新的数据表的方法分享 ---1 mysql create table `新数据表名` ...
- oracle job create table insert into
create or replace procedure proc_tzyj is begin insert into t_trade_activity@dw3_link.regress.rdbms.d ...
- Oracle Demo ->> CREATE TABLE
Demo One CREATE TABLE employees_demo ( employee_id ) , first_name ) , last_name ) CONSTRAINT emp_las ...
- Oracle创建表语句(Create table)语法详解及示例
创建表(Create table)语法详解1. ORACLE常用的字段类型ORACLE常用的字段类型有VARCHAR2 (size) 可变长度的字符串, 必须规定长度CHAR(size) 固定长度的字 ...
- sql: Oracle 11g create table, function,trigger, sequence
--书藉位置Place目录 drop table BookPlaceList; create table BookPlaceList ( BookPlaceID INT PRIMARY KEY, -- ...
- Oracle创建表语句(Create table)语法详解及示例、、 C# 调用Oracle 存储过程返回数据集 实例
Oracle创建表语句(Create table)语法详解及示例 2010-06-28 13:59:13| 分类: Oracle PL/SQL|字号 订阅 创建表(Create table)语法详解 ...
- oracle数据库【表复制】insert into select from跟create table as select * from 两种表复制语句区别
create table as select * from和insert into select from两种表复制语句区别 create table targer_table as select ...
- ORACLE的Copy命令和create table,insert into的比较
在数据表间复制数据是Oracle DBA经常面对的任务之一,Oracle为这一任务提供了多种解决方案,SQL*Plus Copy 命令便是其中之一.SQL*Plus Copy 命令通过SQL*Net在 ...
随机推荐
- eclipse导入Android项目后,项目的名称变为了主Activity的名称
不要使用File -> Import -> Android -> Existing Android Code into Workspace,而是用 File -> Import ...
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- ASP.NET 5系列教程 (三):view components介绍
在ASP.NET MVC 6中,view components (VCs) 功能类似于虚拟视图,但是功能更加强大. VCs兼顾了视图和控制器的优点,你可以把VCs 看作一个Mini 控制器.它负责控制 ...
- ThreadLocal线程范围内的共享变量
模拟ThreadLocal类实现:线程范围内的共享变量,每个线程只能访问他自己的,不能访问别的线程. package com.ljq.test.thread; import java.util.Has ...
- atitit.j2ee 1.5 1.6 的不同跟 Servlet 3.0新特性总结
atitit.j2ee 1.5 1.6 的不同跟 Servlet 3.0新特性总结 1. jar比较,j2ee 1.6 添加了许多的jar 1 2. ,Servlet 3.0 2 2.1. 可插性 ...
- paip.环境设置 mybatis ibatis cfg 环境设置
paip.环境设置 mybatis ibatis cfg 环境设置 三部分 //////////1. 电泳.... ............2. 猪配置文件 com/mijie/homi/searc ...
- DOS命令中的For
前几天做安装包,需要获取某个目录下所有文件夹的名字和路径.本来想用C++写段代码,后来想起DOS有可能满足需求,于是研究了一下FOR命令,竟然非常容易的达到目的,看来小看了DOS这个门口的东西. 说下 ...
- Leetcode-203 Remove Linked List Elements
#203. Remove Linked List Elements Remove all elements from a linked list of integers that have val ...
- 冒泡算法应用(坐标Y值降序X值升序)
今天有个客户需求是有一坐标数组,希望按Y值降序X值升序排列,我临时写了个算法.先写个坐标类: class XYZ { public XYZ() { } public XYZ(doubl ...
- ClamAV安装使用及API例子
ClamAV是一款由Sourcefire组织开发的开源杀毒引擎,Sourcefire同时也是Snort入侵检测引擎的所有者.ClamAV提供了一种更为快速灵活的框架用以检测恶意代码和软件产品.可以作为 ...