oracle_2 表和约束

来源:互联网 发布:三益钢琴js 600na图片 编辑:程序博客网 时间:2024/05/17 02:52

–创建表 create table
create table t_test(
id number(10),
name varchar2(20),
age number(3)
);
–删除表 drop table
drop table t_test;

–表的作用 : 存储有用的数据
–有用的数据 是指 数据的完整性 : 正确性 和 相容性

–表与java的类 类似
–一条记录与java一个实例对象类似

–完整性:
–域完整性 : 表示一个字段
–数据类型,唯一约束,检查约束,非空约束,默认值约束

  --1.唯一约束unique : 表示数据不允许重复     --1.在字段上创建     create table t_test(            id number(10),            name varchar2(20), constraint uk_test_name unique(name),  --创建  name  的唯一性,uk_test_name为约束者            age number(3)     );     --查看用户的约束     select * from user_constraints;     --插入数据     insert into t_test(id,name,age) values(1001,'小明',23);     insert into t_test(id,name,age) values(1002,'小明',22);     --2.在表的内部创建      create table t_test(            id number(10),            name varchar2(20),             age number(3),            constraint uk_test_name unique(name)     );     --3.在表的外部创建      create table t_test(            id number(10),            name varchar2(20),             age number(3)     );     alter table t_test     add constraint uk_test_name unique(name);  --2.检查约束check:表示数据在一定的指定的值中      --写法1       alter table t_test        add constraint ck_test_age check(age>=0 and age<=150);     --删除约束     alter table t_test     drop constraint ck_test_age;      --写法2       alter table t_test       add constraint ck_test_age check(age between 0 and 150);       insert into t_test(id,name,age) values(1003,'小红',222);  --3.非空约束 not null    alter table t_test    modify name not null;    insert into t_test(id,name,age) values(1004,'',34);  --4.默认值约束  alter table t_test  modify age default 20;  --设置age默认值20   insert into t_test(id,name,age) values(1005'小黑');  --实体完整性 :                --唯一,非空的字段来表示(推荐使用无意义的字段)                --使用主键约束                --(唯一性:一张表只允许有一个主键约束,被约束的字段代表一条记录)                --primary key()               --添加主键               alter table t_test                add constraint pk_test_id primary key(id);               --模拟类的关系                 --人                 create table person(                        personId number(10) constraint pk_person_personId primary key,                        name varchar2(20) not null,                        age number(3) constraint ck_person_age check(age between 0 and 150)                 );                 --手机                 create table phone(                        phoneId number(10) constraint pk_phone_phoneId primary key,                        brand varchar2(50),                        color varchar2(10),                        price number,                        pId number(10)                 );  --关系完整性:                --外键约束                --关于外键的创建                  --表与表对应关系                    -- 1 对 1 :外键可以创建在任何一方                    -- 1 对 n :外键创建在多的一方                    -- n 对 n :外键不创建在任何一方                --外键关系:两个表的关系是主表和子表(含有对应外键的表为子表)                  --person 是 phone 的主表,phone 是 person 的子表,通过外键约束来维持这种关系                --添加外键约束:  foreign key(...) references ...                --在含有外键的表中创建约束                  alter table phone                  add constraint fk_phone_person_pId                  foreign key(pId) references person(personId);                --删除表时,先删除子表,再删除主表                  drop table phone;                  drop table person;
0 0
原创粉丝点击