oracle數據庫學習相關筆記-相關約束

1、主鍵約束的三種方式java

1.在字段後加primary key  約束 id  varchar(32) primary key
2.在表建立好以後添加外鍵約束 alter table  student  add  constraints pk_student_id PRIMARY key(id);
3.在建立表的語句的最後面使用 constraints pk_表名_字段名 primary key(字段名)python

4.刪除主鍵約束alter  table  student drop constraints  pk_student_id;sql

驗證流程:學習

1-1.建立一張表,在id後加上主鍵約束 primary key測試

create table student(
	id  varchar(32) primary key,
    name  varchar(32) not null
)

1-2.插入兩條數據code

insert into student(id,name) values('1','張三');
insert into student(id,name) values('1','李四');

1-3.報錯以下blog

1-4.刪除表,而後從新建立ci

-- 刪除表
drop table student;
-- 從新建立
create table student(
	id  varchar(32),
    name  varchar(32) not null,
	constraints pk_student_id PRIMARY key(id)
)

1-5.插入原來的兩條數據table

1-6.報錯以下class

1-7.刪除表,而後從新建立

​drop table student;

​create table student(
    id  varchar(32),
    name  varchar(32) not null
)

1-8.修改表的外鍵約束

alter table  student  add  constraints pk_student_id PRIMARY key(id);

1-9.插入原來的兩條數據,報一樣的錯誤

2、檢查約束的三種方式

1.直接在建立表的字段後使用 check(條件) 例如      sage number(3) check(sage<150 and sage>0),
2.在建立表的語句的最後面使用 constraints ck_表名_字段名 check(條件)
3.在建立表後使用 alter table 表名 add  constraints ck_表名_字段名 check(條件);
4.刪除檢查約束 alter table student drop  constraints 檢查約束名;

3、非空約束的三種方式

1.直接在建立表的字段後使用 not null 關鍵字 
2.在建立表的語句的最後面使用 constraints ck_表名_字段名 check(字段名 is not null)
3.在建立表後使用 alter table 表名 add  constraints ck_表名_字段名 check(字段名 is not null);
4.刪除非空約束 alter table student drop  constraints 非空約束名;

4、惟一約束的三種方式

1.直接在建立表的字段後使用 unique
2.在建立表的語句後面使用 constraints un_表名_字段名 unique(字段名);
3.在建立表後使用 alter table 表名 add  constraints un_表名_字段名 unique(字段名);
4.刪除約束:alter table 表名 drop  constraints 惟一約束名;

5、外鍵約束

--二維表建立 外鍵約束學習:
   --建立學生表
   create table student(
         sno number(10) primary key,
         sname varchar2(100) not null,
         sage number(3) check(sage>0 and sage<150),
         ssex char(4) check(ssex='男' or ssex='女'),
         sfav varchar2(500),
         sqq varchar2(30) unique,
         cid number(10) --references clazz(cno)
         --constraints fk_student_cid foreign key(cid) references clazz(cno)--外鍵
      )
      --添加外鍵
      alter  table student add constraints fk_student_cid foreign key(cid) references clazz(cno) on delete set null
      alter  table student drop constraints fk_student_cid 
      drop table student
      --添加測試數據
      insert into student values(1,'張三001',18,'男','唱歌','657889900',1);
      insert into student values(2,'張三002',18,'男','唱歌','657889901',1);
      insert into student values(3,'李四001',18,'男','唱歌','657889903',2);
      insert into student values(4,'李四002',18,'男','唱歌','657889904',2);
   --建立班級表
      create table clazz(
       cno number(10) primary key,
       cname varchar2(100) not null,
       cdesc varchar2(300)
      
      )
      --添加測試數據
       insert into clazz values(1,'java','6666');
       insert into clazz values(2,'python','33333');
  --查詢學生及其班級信息
      select * from student  s
      inner join clazz c
      on s.cno=c.cno
  --問題:居然能夠在學生表中插入一個不存在班級
      insert into student values(5,'李四003',18,'男','唱歌','657889905',3);
  --使用外鍵:
      --做用:當在子表中插入的數據在父表中不存在,則會自動報錯。
      --概念:當一張表的某個字段的值須要依賴另一張表的某個字段的值,則使用外鍵約束。
             --其中主動依賴的表稱爲子表,被依賴的表稱爲父表。外鍵加在子表中。
      --使用: 
             --在子表中的字段後直接使用   references 父表名(字段) 例如: cid number(10) references clazz(cno)
             --在建立表語句的最後面使用  constraints fk_子表名_字段名 foreign key(字段名) references 父表名(字段名)
             --在建立表後使用:alter table 表名 add constraints fk_子表名_字段名 foreign key(字段名) references 父表名(字段名)
            --刪除外鍵:alter table 表名 drop constraints 外鍵約束名
      --外鍵選取:
            --通常選取父表的主鍵做爲子表的外鍵。
      --外鍵的缺點:
            --沒法直接刪除父表數據,除非級聯刪除
            --級聯刪除:在添加外鍵約束時,使用關鍵字 on delete cascade
                     --使用:當刪除父表數據時,自動刪除子表相關全部數據。
                     --缺點:沒法保留子表歷史數據。
                     --使用關鍵字 on delete set null
                           --刪除父表數據時,將子表中的依賴字段的值設置爲null。
                           --注意:子表依賴字段不能添加非空約束。
           --刪除班級1的信息
           select * from student
           delete from clazz where cno=1
相關文章
相關標籤/搜索