Oracle操作语句之DDL语句

DDL:数据库定义语言,可以自动提交事物。(create alter drop rename truncate comment)

数据库三大范式

第一范式:列中的值不能再分割
第二范式:在满足第一范式的基础上,设计的表中的每个列都要依赖于主键列。
第三范式:在满足第二范式的基础上,所有的列都必须直接依赖于主键列,不能间接依赖于主键列(即不能发生依赖传递)
建表语句格式:

    create table 表名(
     列名1 数据类型 列级约束,
     列名2 数据类型 列级约束,
     列名3 数据类型 列级约束,
    ............
    )
或者

    create table 表名(
     列名1 数据类型 列级约束,
     列名2 数据类型 列级约束,
     列名3 数据类型 列级约束,
     .............
     表级约束1,
     表级约束2,
     ..............
    )
比如

 create table teacher(
      id number,
      name varchar2(20) not null,
      email varchar2(100),
      gender char,
      constraint tea_id_pk primary key(id),
      constraint tea_emmail_ck unique(email),
      constraint tea_gender_ck check(gender in('f','m'))
      );


约束:维持两张表之间主外键关系的关键

oracle数据库中的五种约束:

主键约束:用来唯一标示表中的一个列,一个表中的主键约束只能有一个,但是可以在一个主键约束中包含多个列,也称为联合约束。
primary key
外键约束:用来约束两个表中列之间的关系。
foreign key
唯一约束:用来唯一标示表中的列。与主键约束不同的是,在一个数据表中可以有多个唯一约束。
unique
检查约束:用来约束表中列的输入值得范围,比如在输入性别时,要求数据库中只能输入男或者女,就可以使用检查约束来约束该列。
check
非空约束:约束该列一定要输入值。not null(只能写成行级约束,不能写成表级约束。)

联合主键的表级声明

create table teacher(
	 id number,
	 pro number,
	 constraint teacher_id_pro_pk primary key(id,pro)
	)
联合外键的表级声明:

create table order(
        id number primary key,
	price number  not null,
	teacher_id  number,
	pro_id number,
	constraint order_cus_id_pro_fk foreign key (teacher_id,pro_id) references teacher(id,pro)
       )
使用语句创建一张和s_dept一样结构的表:
create table test1
    as 
    select * from s_dept;(此时test1中有s_dept的结构和所有的数据信息。)

或者

create table test2
    as 
    select * from s_dept
    where 1>1;(此时test2中有s_dept的结构,但没有任何的数据信息。)


*************************表结构的修改(alter drop rename truncate comment的使用):

修改表的名字:(rename ...to...)

格式:rename old_name to new_name;
例如:rename test to mytest;(将test表的名字改为mytest)
修改表中某列的名字:
格式:alter table table_name rename column old_name to new_name
例如:alter table test rename column name to myname;(将test表中列为name的列改为myname)
修改表中某列的数据类型:

格式:alter table table_name modify(列名 新的数据类型)
例如:alter table test modify (name (varchar2(500)));(修改test表中name列的数据类型)
向表中添加一列:(alter,add)
格式:alter table table_name add 添加列的名字  添加列的类型
例如:alter table test add birthday date;(在test表中添加birthday列 数据类型设置为date)
向表中删除某一列:(alter,drop column)

格式:alter table table_name drop column 要删除的列名
例如:alter table test drop column name;(删除test表中的name列)
向表中某列添加列的表级约束:(aletr,add,constraint)
格式:alter table table_name add constraint 约束名(unique , not null等)(添加约束的列名);
例如:alter table test add constraint test_name_un unique(name);(给test表中的name列添加唯一性约束)
删除表中的某一列的表级约束
格式:alter table table_name drop constraint 约束名
例如:alter table test drop constraint test_name_un;(删除test表中的test_name_un约束)
让约束失效(此时必须知道约束的名字)
格式:alter table table_name disable constraint  约束名;
例如:aletr table test disable constraint test_name_un;(让test表中的test_name_un约束失效)
让失效的约束再次生效。

格式:alter table table_name enable constraint  约束名;
例如:aletr table test enable constraint test_name_un;(让test表中的test_name_un约束再次生效)
删除表中的数据,不需要提交,默认已经提交,并且不能回滚。

格式:truncate table table_name
例如:truncate table test;(删除test表中的数据)
给表加注释,会自动提交事物
格式: comment on table table_name  is '添加的注释内容'
例如:comment on table test is 'ok';(给test表添加注释)
查看表中的注释,表的名字要大写
格式:select * from user_tab_comments  where table_name='表的名字';
例如:select * from user_tab_comments  where table_name='TEST';(查看test表中的注释)
给表中的列加注释,会自动提交事物

格式:comment on column table_name.列名 is '添加的注释内容';
例如:comment on column test.name is 'good';(给test表的name列添加注释)
查看表中列的注释,表的名字要大写
格式:select * from user_col_comments where comments is null and table_name='表的名字';
例如: select * from user_col_comments where comments is null and table_name='TEST';(查看test表中的列的注释)



你可能感兴趣的:(数据库,数据库,数据库,DDL,Oracle)