首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

oracle惯用表操作

2012-07-28 
oracle常用表操作1.首先当然是创建表语法格式create table t_student(student_id number(10),student_name

oracle常用表操作
1.首先当然是创建表
语法格式
create table t_student( student_id number(10), student_name varchar2(30), sex char(2), birthday date, email varchar2(30), classes_id number(10));

向t_student表中加入一条数据

insert into t_student(student_id,student_name,sex,birthday,email,classes_id) values(1000,'zzg',to_date('1985-10-30','yyyy-mm-dd'),1234);


向t_student表中加入数据(使用默认值)
create table t_student(    student_id number(10),    student_name varchar2(30),    sex char(2) default '男',    birthday date default sysdate,    email varchar2(30),    classes_id number(10));


2.创建表加入约束
常见的表约束
非空约束,not null唯一约束,unique key主键约束,primary key外键约束,foreign key自定义检查约束,check

非空约束,针对某个字段设置其值不为空,如:学生的姓名不能为空
create table t_student(    student_id number(10),    student_name varchar2(30) not null,    sex char(2) default '男',    birthday date default sysdate,    email varchar2(30),    classes_id number(10));


以上,我可以自己起约束名称,如:
create table t_student(    student_id number(10),    student_name varchar2(30) constraint student_name_not_null not null,    sex char(2) default '男',    birthday date default sysdate,    email varchar2(30),    classes_id number(10));


通过user_constraints表可以查询到约束的名称,如:
select constraint_name from user_constraints;


唯一约束,unique key
唯一性约束,它可以使某个字段的值不能重复,如:email不能重复
create table t_student(student_id  number(10),student_name varchar2(20),sexchar(2),birthdaydate,emailvarchar2(30) unique,classes_idnumber(3));


同样可以为唯一约束起个约束名
create table t_student(student_id  number(10),student_name varchar2(20),sexchar(2),birthdaydate,emailvarchar2(30) constraint email_unique unique,classes_idnumber(3));


以上约束放到字段上了,也成为字段级的约束,还有一种约束叫表级约束,也就是说可以把约束信息放到字段的后面
create table t_student(student_id  number(10),student_name varchar2(20),sexchar(2),birthdaydate,emailvarchar2(30),classes_idnumber(3),        constraint email_unique unique(email));


主键约束,primary key
每个表应该具有主键,主键可以标识记录的唯一性,主键分为单一主键和复合(联合)主键,单一主键是由一个字段构成的,复合(联合)主键是由多个字段构成的
create table t_student(student_id  number(10) primary key,student_name varchar2(20),sexchar(2),birthdaydate,emailvarchar2(30),classes_idnumber(3));

可以采用字段级和表级起约束名称
--字段级约束create table t_student(student_id  number(10) constraint pk_student_id primary key,student_name varchar2(20),sexchar(2),birthdaydate,emailvarchar2(30),classes_idnumber(3));--表级约束create table t_student(student_id  number(10),student_name varchar2(20),sexchar(2),birthdaydate,emailvarchar2(30),classes_idnumber(3),        constraint pk_student_id paimary key(student_id));


复合主键,采用学生代码和学生名称构成主键
create table t_student(student_id  number(10),student_name varchar2(20),sexchar(2),birthdaydate,emailvarchar2(30),classes_idnumber(3),        constraint pk_student_id paimary key(student_id,student_name));


外键约束,foreign key
外键主要是维护表之间的关系的,主要是为了保证参照完整性,如果表中的某个字段为外键字段,那么该字段的值必须来源于参照的表的主键

建立学生和班级表之间的连接
--首先建立班级表t_classescreate table t_classes(classes_id number(3),classes_name varchar2(40),constraint pk_classes_id primary key(classes_id));--在t_student中加入外键约束create table t_student(student_id  number(10),student_name varchar2(20),sexchar(2),birthdaydate,emailvarchar2(30),classes_idnumber(3) references t_classes(classes_id),constraint      student_id_pk primary key(student_id));


也可以使用表级约束来描述外键关系
create table t_student(student_id   number(10),student_name varchar2(20),sex     char(2),birthday     date,email     varchar2(30),classes_id   number(3),constraint   student_id_pk primary key(student_id),constraint   fk_classes_id foreign key(classes_id) references t_classes(classes_id)     );


自定义检查约束,check(不建议使用)
使用check可以检查表中的字段,是否符合某一个表达式,如:性别只能为“男”和“女”
create table t_student(student_id  number(10),student_name varchar2(20),sexchar(2),birthdaydate,emailvarchar2(30),classes_idnumber(3),constraint      student_id_pk primary key(student_id),constraint      chk_sex check(sex in('男', '女'))     );


3.增加/删除/修改表结构
采用alter table来增加/删除/修改表结构,不影响表中的数据

添加字段
如:需求发生改变,需要向t_student中加入联系电话字段,字段名称为:contatct_tel 类型为varchar2(40)
alter table t_student add(contact_tel varchar2(40));


修改字段
如:student_name无法满足需求,长度需要更改为100
alter table t_student modify(student_name varchar2(100));


删除字段
如:删除联系电话字段
alter table t_student drop(contact_tel);


4.增加/删除/修改表约束

删除约束
将t_student中的classes_id外键约束删除
alter table t_student drop constraint fk_classes_id;


添加约束
将t_student中的classes_id加入外键约束
alter table t_student add constraint fk_classes_id foreign key(classes_id) references t_classes(classes_id);


修改约束,其实就是修改字段
alter table t_student modify(student_name varchar2(30) not null);


5.删除表
drop table t_classes;


如果存在父子表(存在外键关系),先删除子再删除父

热点排行