日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

oracle 表约束非空,oracle--约束(主键、非空、检查)

發布時間:2025/3/8 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle 表约束非空,oracle--约束(主键、非空、检查) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題1:學號重復了,數據還可以插入成功

使用主鍵約束:學號是唯一標識一條數據的,所以必須唯一且不能為空

---(1)、在確定為主鍵的字段后添加 primary key關鍵字

---(2)、在創建表的后面使用:constraints pk_表名_字段名 primary key(字段名)

--(3)、在創建表后使用 alter table 表名 add constraints pk_表名_字段名 primary key(字段名);

--刪除主鍵:alter table 表名 drop constraints pk_表名_字段名

問題2:姓名可以為空。

使用非空約束

---(1)、創建表的時候在字段后面添加not null

---(2)、在創建表字段后使用 constraints ck_表名_字段名 check(字段名 is not null) 了解

--(3)、在創建表后使用alter table 表名 modify 字段名 類型 not null;

---(4)、修改字段可以存儲空值:alter table 表名 modify 字段名 類型 null;

問題3:性別不但可以為空,還可以為其他不知道的字符

使用檢查約束

---(1)、創建表的時候在字段后使用 default 值 check(條件),

---------但是會允許空值的出現,并且默認值只有在字段不聲明的情況下生效

---(2)、在創建表所有字段后使用:constraints ck_表名_字段名 check(條件)

---(3)、在創建表后使用:alter table 表名 add constraints ck_表名_字段名 check(條件)

問題4:年齡可以超過200

--使用檢查約束條件

問題5:qq號一致

使用唯一約束

--(1)、在字段后直接使用unique關鍵字

--(2)、在所有字段后使用:constraints uk_表名_字段名 unique(字段名)

--(3)、 alter table 表名 add constraints uk_表名_字段名 unique(字段名)

--刪除唯一約束:alter table 表名 drop constraints uk_表名_字段名

1、主鍵約束

三種方式主鍵約束方式

create table student(

sno number(10) primary key,

sname varchar2(100),

sage number(3),

ssex char(4),

sbirth date,

sqq varchar2(30)

);

create table student(

sno number(10),

sname varchar2(100),

sage number(3),

ssex char(4),

sbirth date,

sqq varchar2(30),

constraint pk_student_sno primary key(sno) ---添加主鍵約束

);

create table student(

sno number(10),

sname varchar2(100),

sage number(3),

ssex char(4),

sbirth date,

sqq varchar2(30)

);

alter table student add constraint pk_student_sno primary key(sno);

alter table student drop constraint pk_student_sno;

select * from student for update;

drop table student;

非空約束

三種方式

create table student(

sno number(10) primary key,

sname varchar2(100) not null,

sage number(3),

ssex char(4),

sbirth date,

sqq varchar2(30)

);

create table student(

sno number(10) primary key,

sname varchar2(100),

sage number(3),

ssex char(4),

sbirth date,

sqq varchar2(30),

constraints ck_student_sname check(sname is not null)

);

create table student(

sno number(10) primary key,

sname varchar2(100),

sage number(3),

ssex char(4),

sbirth date,

sqq varchar2(30)

);

alter table student add constraint ch_student_sname check(sname is not null);

alter table student drop constraint ch_student_sname

檢查約束

create table student(

sno number(10) primary key,

sname varchar2(100) not null,

sage number(3) check(sage<150 and sage>0),

ssex char(4) check(ssex =‘男‘ or ssex = ‘女‘),

sbirth date,

sqq varchar2(30)

);

create table student(

sno number(10) primary key,

sname varchar2(100) not null,

sage number(3),

ssex char(4) check(ssex =‘男‘ or ssex = ‘女‘),

sbirth date,

sqq varchar2(30),

constraint ch_student_sage check(sage<150 and sage>0)

);

create table student(

sno number(10) primary key,

sname varchar2(100) not null,

sage number(3),

ssex char(4) check(ssex =‘男‘ or ssex = ‘女‘),

sbirth date,

sqq varchar2(30)

);

alter table student add constraint ch_student_sage check(sage<150 and sage>0);

alter table student drop constraint ch_student_sage;

總結

以上是生活随笔為你收集整理的oracle 表约束非空,oracle--约束(主键、非空、检查)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。