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

oracle表的创建、类型、结构和约束

2014-11-23 23:56 309 查看
查看表结构(不要具体数据,有哪些字段和类型) : desc user_tables

1.表

a).表结构:类似于java类

class Person{

private String name;

private int age;

private double height;

private Date birthday;

}

create table t_person(

name varchar2(20),

age number(3),

height number(3,2),

birthday date

);

查看表内容 select table_name,tablespace_name from user_tables;

b).行(记录):类似于java类的对象实例

Person person=new Person("aa",21,1.72,new SimpleDateFormat("yyyy-MM-dd").parse("1990-12-12"));

insert into t_person values('aa',21,1.72,to_date('1990-12-12','yyyy-MM-dd'));

查看:select * from t_person;

c).单元格(字段):类似于java类的对象实例的属性

Person.name="bb";//java中的方式

update t_person set name='bb';//数据库中的方式

d).表结构的操作:

//添加字段

alter table t_person add sex char(2);

//删除字段

alter table t_person drop column sex;

//修改字段

alter table t_person modify name varchar2(30);

//修改表名

rename t_person to person;

select table_name from user_tables;

2.类型

常用的几种:

1).字符类型:

varchar2(n) //n=1 to 4000字节(可变长度)

char(n) //n=1 to 2000字节(不变长度) char(10)

nvarchar2(n);

nchar(n); //字符(中文占两个字节)

2).数字类型:

number(n); 长度只有38位 => n<=38

number(m,n);//有小数 m=1 to 38 n=-84 to 127

binary_float 4B //oracle 10g以上支持

binary_double 8b

3).日期类型:

date //精确到秒级别

timestamp //精确到毫秒级别

4).大类型:

clob //4G * block size

blob //4G * block size

bfile //类似于目录类型 4G * block size(文本二进制)

3.约束

(修改表结构必须在添加数据之前)

a).用一个id来表示一条记录,类似于java实例对象的对象名 => 针对表的约束

特点: 唯一 , 非空

成为主键( primary key ):一张表里只允许有一个主键

//添加主键字段

alter table person add id number(9);

//清除表内的所有数据

delete from person;

//为字段添加主键约束

alter table person add constraint pk_person_id primary key(id);

//查看约束

select owner,constraint_name,constraint_type,table_name from user_constraints;

b).为了保证数据的有效性,做检查约束(check) => 针对一个字段的约束

//清除表内的所有数据

delete from person;

//为字段添加条件或范围约束

//逻辑运算符: and or not

alter table person add constraint ck_person_age check(age>=0 and age<=150);

//alert table person add constraint ck_person_sex check(sex in('男','女')); --取值只能为男或女

insert into person values('aa',521,1.72,to_date('1990-12-12','yyyy-MM-dd'),1);

c).为了保证数据不重复,做唯一约束(unique):针对一个字段的约束 ===允许为空,为空时不校验===

//添加字段

alter table person add idCard char(18);

//为字段添加唯一约束

alter table person add constraint ck_person_idCard unique(idCard);

d).为了保证数据不为空,做非空约束(not null):针对一个字段的约束 ==========不是约束类型=======

//清除表内的所有数据

delete from person;

//添加约束

alter table person modify name not null;

e).为了减少输入,增强友好性,做默认值约束(default):针对一个字段的约束 ==========不是约束类型=======

//添加约束

alter table person modify age default 18;

insert into person values('aa',21,1.72,to_date('1990-12-12','yyyy-MM-dd'),1,'123456789987654321');

insert into person values('bb',21,1.72,to_date('1990-12-12','yyyy-MM-dd'),2,null);

insert into person values('cc',default,1.93,to_date('1990-12-12','yyyy-MM-dd'),3,987654321012345678);

f).再添加一个表

class phone{

private String brand;

private String type;

private double price;

}

create table phone(

id number(9) primary key,

brand varchar2(30),

phoneType varchar2(30),

price binary_float

);

alter table person add phoneId number(9);

为了保证两个表之间的关系,做外键约束,

确定主表( 外键对应其主键的表)和从表( 含有外键的表),关系建立在从表中

从表_主表

alter table person add constraint fk_person_phone foreign key(phoneId) references phone(id);

插入数据:

//插入数据时,先插入主表,再插入从表

insert into phone values(1,'iphone','5s',4999);

insert into person values('cc',default,1.93,to_date('1990-12-12','yyyy-MM-dd'),4,123456789098765432,1);

删除数据:

//删除数据时,先删除从表,再删除主表

delete from person;

delete from phone;

删除约束:

alter table person drop constraint SYS_C0011096;

alter table person drop constraint CK_PERSON_IDCARD;

alter table person drop constraint CK_PERSON_AGE;

alter table person drop constraint PK_PERSON_PHONE;

alter table person drop constraint PK_PERSON_ID;

直接建立约束:

drop table person; //删除表

create table t_person(

id number(4) primary key;

name varchar2(10) not null,

age number(3) default 18 check(age>=0 and age<=150),

height number(3,2),

birthday date,

idcard char(18) unique,

phoneid number(4) references phone(id)

);

select constraint_name,constraint_type,table_name from user_constraints;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐