sql表的约束练习题
1. 如何创建一个包含非空约束的表?
A. CREATE TABLE t01(id integer, name text, score numeric);
B. CREATE TABLE t01(id integer NOT NULL, name text, score numeric);
C. CREATE TABLE t01(id integer UNIQUE, name text, score numeric);
D. CREATE TABLE t01(id integer CHECK(id > 0), name text, score numeric);
答案:B
2. 创建唯一约束的语法是什么?
A. CREATE TABLE t01(id integer UNIQUE, name text, score numeric);
B. CREATE TABLE t01(id integer, name text, score numeric UNIQUE);
C. CREATE TABLE t01(id integer PRIMARY KEY, name text, score numeric);
D. CREATE TABLE t01(id integer CHECK(id > 0), name text, score numeric);
答案:A
3. 创建表时如何在score列上创建CHECK约束,要求score必须大于0?
A. CREATE TABLE t01(id integer, name text, score numeric CHECK(score > 0));
B. CREATE TABLE t01(id integer, name text, score numeric UNIQUE(score > 0));
C. CREATE TABLE t01(id integer, name text, score numeric INDEX(score > 0));
D. CREATE TABLE t01(id integer, name text, score numeric PRIMARY KEY(score > 0));
答案:A
4. 如何在列级定义id列的主键约束?
A. CREATE TABLE t01(id integer PRIMARY KEY, name text, score numeric);
B. CREATE TABLE t01(id integer, name text, score numeric, PRIMARY KEY(id));
C. CREATE TABLE t01(id integer, name text, score numeric, PRIMARY KEY(name));
D. CREATE TABLE t01(id integer, name text, score numeric, PRIMARY KEY(score));
答案:A
5. 修改表以增加主键约束的语法是什么?
A. ALTER TABLE t01 ADD PRIMARY KEY(id);
B. ALTER TABLE t01 ADD CONSTRAINT pk_t01 PRIMARY KEY(id);
C. ALTER TABLE t01 ADD UNIQUE(id);
D. ALTER TABLE t01 ADD CHECK(id > 0);
答案:B
6. 如何创建一个表,并在no列上定义外键约束,引用另一个表的id主键列?
A. CREATE TABLE t02(no integer constraint t02_fk references t01(id), name text);
B. CREATE TABLE t02(no integer, name text, FOREIGN KEY(no) REFERENCES t01(id));
C. CREATE TABLE t02(no integer REFERENCES t01(id), name text);
D. CREATE TABLE t02(no integer, name text, FOREIGN KEY t02_fk(no) REFERENCES t01(id));
答案:A
7. 如何在t01表的name列上创建非空约束?
A. CREATE TABLE t01(id integer, name text NOT NULL, score numeric);
B. CREATE TABLE t01(id integer, name text, score numeric NOT NULL);
C. ALTER TABLE t01 ALTER COLUMN name SET NOT NULL;
D. ALTER TABLE t01 ADD CONSTRAINT uk_t01 UNIQUE(name);
答案:C
8. 如何在t01表的score列上增加CHECK约束,要求score取值必须小于100?
A. ALTER TABLE t01 ADD CONSTRAINT t01_score_ck CHECK(score < 100);
B. ALTER TABLE t01 ADD CHECK(score < 100);
C. ALTER TABLE t01 ADD UNIQUE(score < 100);
D. ALTER TABLE t01 ADD PRIMARY KEY(score < 100);
答案:A
9.如何更新t01表的no字段(因存在外键关联导致更新主键列失败)?
A. UPDATE t01 SET no=200 WHERE no=100;
B. UPDATE t01 SET id=200 WHERE id=100;
C. UPDATE t01 SET no=200 WHERE 100;
D. UPDATE t01 SET no=200 WHERE name='ACCOUNTING1';
答案:A
10. 如何在事务中临时修改外键约束为延迟约束?
A. BEGIN; ALTER TABLE t02 ALTER CONSTRAINT t02_fk DEFERRABLE; SET CONSTRAINTS t02_fk deferred; END;
B. BEGIN; ALTER TABLE t02 ALTER CONSTRAINT t02_fk NOT DEFERRABLE; SET CONSTRAINTS t02_fk immediate; END;
C. ALTER TABLE t02 ALTER CONSTRAINT t02_fk DEFERRABLE;
D. ALTER TABLE t02 ALTER CONSTRAINT t02_fk NOT DEFERRABLE;
答案:A
11. 如何在id列上创建CHECK约束,要求id列值必须大于10?
A. ALTER TABLE t01 ADD CONSTRAINT t01_ck CHECK(id > 10);
B. ALTER TABLE t01 ADD CHECK(id < 10);
C. ALTER TABLE t01 ADD UNIQUE(id > 10);
D. ALTER TABLE t01 ADD PRIMARY KEY(id > 10);
答案:A
12. 如何修改t01_ck约束为disable novalidate?
A. ALTER TABLE t01 DISABLE NOVALIDATE CONSTRAINT t01_ck;
B. ALTER TABLE t01 DISABLE CONSTRAINT t01_ck;
C. ALTER TABLE t01 ENABLE VALIDATE CONSTRAINT t01_ck;
D. ALTER TABLE t01 ENABLE NOVALIDATE CONSTRAINT t01_ck;
答案:A
13. 如何通过数据字典表查看约束的信息?
A. SELECT constraint_name, status, validated FROM user_constraints WHERE table_name = 'T01';
B. SELECT constraint_name, status, validated FROM constraints WHERE table_name = 'T01';
C. SELECT * FROM user_constraints WHERE table_name = 'T01';
D. SELECT * FROM constraints WHERE table_name = 'T01';
答案:A
14. 如何创建测试表t01,并在id列上创建主键约束?
A. CREATE TABLE t01(id int primary key, name varchar(10));
B. CREATE TABLE t01(id int, name varchar(10), PRIMARY KEY(id));
C. CREATE TABLE t01(id int, name varchar(10), UNIQUE(id));
D. CREATE TABLE t01(id int, name varchar(10), CHECK(id > 0));
答案:AB
15. 如何创建测试表t02,并为id列定义延迟外键约束,参照t01表的id列?
A. CREATE TABLE t02(id int constraint t02_fk references t01(id) initially deferred, name varchar(10));
B. CREATE TABLE t02(id int, name varchar(10), FOREIGN KEY(id) REFERENCES t01(id) INITIALLY DEFERRED);
C. CREATE TABLE t02(id int, name varchar(10), FOREIGN KEY(id) REFERENCES t01(id) INITIALLY IMMEDIATE);
D. CREATE TABLE t02(id int, name varchar(10), CONSTRAINT t02_fk FOREIGN KEY(id) REFERENCES t01(id) INITIALLY DEFERRED);
答案:A
16. 如何修改t02表的t02_fk约束为非延迟?
A. ALTER TABLE t02 ALTER CONSTRAINT t02_fk INITIALLY IMMEDIATE;
B. ALTER TABLE t02 ALTER CONSTRAINT t02_fk NOT DEFERRABLE;
C. ALTER TABLE t02 ALTER CONSTRAINT t02_fk DEFERRABLE;
D. ALTER TABLE t02 ALTER CONSTRAINT t02_fk INITIALLY DEFERRED;
答案:A
17. 在事务中如何临时修改t02_fk约束为延迟约束?
A. BEGIN; ALTER TABLE t02 ALTER CONSTRAINT t02_fk DEFERRABLE; SET CONSTRAINTS t02_fk deferred; END;
B. BEGIN; ALTER TABLE t02 ALTER CONSTRAINT t02_fk NOT DEFERRABLE; SET CONSTRAINTS t02_fk immediate; END;
C. ALTER TABLE t02 ALTER CONSTRAINT t02_fk DEFERRABLE;
D. ALTER TABLE t02 ALTER CONSTRAINT t02_fk NOT DEFERRABLE;
答案:A
18. 如何在t01表的score列上创建CHECK约束,要求score取值必须大于0?
A. CREATE TABLE t01(id integer, name text, score numeric CHECK (score > 0));
B. CREATE TABLE t01(id integer, name text, score numeric CHECK (score < 0));
C. CREATE TABLE t01(id integer, name text, score numeric CHECK (score = 0));
D. CREATE TABLE t01(id integer, name text, score numeric CHECK (score <= 0));
答案:A
19. 如何在t01表的score列上增加CHECK约束,要求score取值必须小于100?
A. ALTER TABLE t01 ADD CONSTRAINT t01_score_ck CHECK(score < 100);
B. ALTER TABLE t01 ADD CONSTRAINT t01_score_ck CHECK(score >= 100);
C. ALTER TABLE t01 ADD CONSTRAINT t01_score_ck CHECK(score = 100);
D. ALTER TABLE t01 ADD CONSTRAINT t01_score_ck CHECK(score > 100);
答案:A
20. 如何在id列上创建主键约束(表级定义)?
A. CREATE TABLE t01 (id integer PRIMARY KEY, name text, score numeric);
B. CREATE TABLE t01 (id integer, name text, score numeric, PRIMARY KEY(id));
C. CREATE TABLE t01 (id integer UNIQUE, name text, score numeric);
D. CREATE TABLE t01 (id integer CHECK(id > 0), name text, score numeric);
答案:B
21. 如何在no列上创建主键约束?
A. CREATE TABLE t01(no integer PRIMARY KEY, name text);
B. CREATE TABLE t01(no integer, name text, PRIMARY KEY(no));
C. CREATE TABLE t01(no integer UNIQUE, name text);
D. CREATE TABLE t01(no integer CHECK(no > 0), name text);
答案:AB
22. 如何在id列上创建外键约束,参照t01表的no列并设置为级联控制?
A. CREATE TABLE t02(id integer REFERENCES t01(no) ON DELETE CASCADE, name text, score numeric);
B. CREATE TABLE t02(id integer, name text, score numeric, FOREIGN KEY(id) REFERENCES t01(no) ON DELETE CASCADE);
C. CREATE TABLE t02(id integer REFERENCES t01(no), name text, score numeric);
D. CREATE TABLE t02(id integer, name text, score numeric, FOREIGN KEY(id) REFERENCES t01(no));
答案:A