oracle(26)_SQL_约束_主键约束 & 非空约束
SQL
约束
- 在数据库开发中,约束是必不可少,使用约束可以更好的保证数据的完整性。
主键约束
-
主键约束都是在 id 上使用,而且本身已经默认了内容不能为空,可以在建表的时候指定。
-
创建一张表,把 person_id 作为主键
-
示例图:
-
查看表的信息
主键不可为空
主键不可重复(唯一), SCOTT.SYS_C0017981是系统自动分配的约束的名字 -
可以自定义主键约束的名字
-
示例图:
非空约束
- 使用非空约束,可以使指定的字段不可以为空。
- 范例:建立一张person表, person_id 和 pname 不可以为空的表
- 示例图:
此时若想要在 person_id 和 pname 列插入空值是不行的!
以上操作完整源码:
--删除原来的 person 表
drop table person;
--创建一张新表 person
create table person(
person_id number(4) primary key,
pname varchar2(10),
age number(3),
birthday date
);
--自定义主键名称
create table person(
person_id number(4) ,
pname varchar2(10),
age number(3),
birthday date,
constraint person_person_id_pk primary key(person_id)
);
--非空约束
create table person(
person_id number(4) ,--主键默认不能为空
pname varchar2(10) not null,
age number(3),
birthday date,
constraint person_person_id_pk primary key(person_id)
);
如有错误,欢迎指正!