日期:2014-05-16  浏览次数:20432 次

oracle常用表操作
1.首先当然是创建表
语法格式
引用

create table 表名(
   列名称 类型,
   …………….,
   …………….
);

建立学生信息表,字段包括:学号、姓名、性别、出生日期、email、班级标识
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),
	sex		char(2),
	birthday	date,
	email		varchar2(30) unique,
	classes_id	number(3)	
);


同样可以为唯一约束起个约束名
create table t_student(
	student_id  	number(10),
	student_name 	varchar2(20),
	sex		char(2),
	birthday	date,
	email		varchar2(30) constraint email_unique unique,
	classes_id	number(3)
);


以上约束放到字段上了,也成为字段级的约束,还有一种约束叫表级约束,也就是说可以把约束信息放到字段的后面
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 email_unique unique(email)
);


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

可以采用字段级和表级起约束名称
--字段级约束
create table t_student(
	student_id  	number(10) constraint pk_student_id primary key,
	student_name 	varchar2(20),
	sex		char(2),
	birthday	date,
	email		varchar2(30),
	classes_id	number(3)
);
--表级约束
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 pk_student_id paimary 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 pk_student_id paimary key(student_id,student_name)
);


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

建立学生和班级表之间的连接
--首先建立班级表t_classes
create table t_classe