Oracle数据库表约束
程序员文章站
2022-03-04 19:12:52
...
--新建表BB
CREATE TABLE BB(
BID NUMBER(3) PRIMARY KEY,
BNAME VARCHAR2(10)
);
INSERT INTO BB VALUES(1,'JAVA');
INSERT INTO BB VALUES(2,'ORACLE');
select * from bb;
--新建表AA
CREATE TABLE AA(
AID NUMBER(3),--PRIMARY KEY 主键约束,
ANAME VARCHAR2(20) NOT NULL,--非空约束,
AEMAIL VARCHAR2(20) UNIQUE,--唯一约束
ASEX VARCHAR(20) DEFAULT '男' check(ASEX='男'OR ASEX='女'),--检查约束
ACLAZZID NUMBER(3) --REFERENCES BB(Bid) 外键约束
);
ALTER TABLE AA ADD CONSTRAINTS FK_AA_ACLAZZID FOREIGN KEY(ACLAZZID) REFERENCES BB(BID);
ALTER TABLE AA ADD CONSTRAINTS PK_AA PRIMARY KEY(AID);
--新建序列 0开始 自增1
create sequence seq_aa start with 0 increment by 1 minvalue 0;
--添加数据
INSERT INTO AA VALUES(seq_aa.nextval,'李白','aaa@qq.com','男',2);
INSERT INTO AA VALUES(seq_aa.nextval,'杜甫','aaa@qq.com','男',2);
INSERT INTO AA VALUES(seq_aa.nextval,'白居易','aaa@qq.com','男',2);
INSERT INTO AA VALUES(seq_aa.nextval,'王安石','aaa@qq.com','男',2);
--查看数据
SELECT * FROM AA;