WITH CHECK OPTION CONSTRAINT
时间: 2024-02-04 15:04:19 浏览: 75
WITH CHECK OPTION CONSTRAINT 是一种与 WITH CHECK OPTION 类似的创建视图时的限制选项,它可以确保在向视图中插入或更新数据时,数据满足视图定义的限制约束条件。
与 WITH CHECK OPTION 不同的是,WITH CHECK OPTION CONSTRAINT 可以在创建视图时指定一个约束名,将视图定义的限制条件作为一个约束加入到数据库中。这个特性可以让我们更好地控制数据的完整性和一致性。
举个例子,我们可以创建一个员工信息的视图,并使用 WITH CHECK OPTION CONSTRAINT 添加一个约束,如下所示:
```
CREATE VIEW emp_info AS
SELECT emp_id, emp_name, dept_name
FROM employees
WHERE salary > 3000
WITH CHECK OPTION CONSTRAINT check_salary;
```
这个视图定义了一个 WHERE 子句,只有满足条件 salary > 3000 的行才能够被插入或更新到视图中,并且使用了一个名为 check_salary 的约束来确保数据的完整性和一致性。如果我们试图插入一个不符合条件的行,就会抛出一个错误,阻止数据插入或更新。
注意,使用 WITH CHECK OPTION CONSTRAINT 创建视图时,约束名不能与数据库中已有的约束名重复。另外,与其他约束一样,我们可以使用 ALTER TABLE 语句来修改或删除视图定义的约束。
相关问题
students(student_id, name, year, GPA, programs_code) instructors(instructor_id, name, title, salary, programs_code) programs(code, name, division, instructor_id) course(course_name, credit, domain, course_number) enroll(student_id, instructor_id, course_name) 1. Create tables according to the schema. 2. Use check or assertion to make sure instructors salary > 5000. 3. Use trigger to deal with cascade delete.
Sure, I can help you with that.
Here's the SQL code to create the tables:
```
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(50),
year INT,
GPA FLOAT,
programs_code INT
);
CREATE TABLE instructors (
instructor_id INT PRIMARY KEY,
name VARCHAR(50),
title VARCHAR(50),
salary FLOAT,
programs_code INT,
CONSTRAINT check_salary CHECK (salary > 5000)
);
CREATE TABLE programs (
code INT PRIMARY KEY,
name VARCHAR(50),
division VARCHAR(50),
instructor_id INT,
FOREIGN KEY (instructor_id) REFERENCES instructors(instructor_id) ON DELETE CASCADE
);
CREATE TABLE course (
course_name VARCHAR(50) PRIMARY KEY,
credit INT,
domain VARCHAR(50),
course_number INT
);
CREATE TABLE enroll (
student_id INT,
instructor_id INT,
course_name VARCHAR(50),
FOREIGN KEY (student_id) REFERENCES students(student_id) ON DELETE CASCADE,
FOREIGN KEY (instructor_id) REFERENCES instructors(instructor_id) ON DELETE CASCADE,
FOREIGN KEY (course_name) REFERENCES course(course_name) ON DELETE CASCADE,
PRIMARY KEY (student_id, instructor_id, course_name)
);
```
In the `instructors` table, I've added a `check_salary` constraint to make sure that the salary of instructors is always greater than 5000.
To deal with cascade delete, I've added a foreign key constraint in the `programs` table that references the `instructors` table and has an `ON DELETE CASCADE` option. This means that when an instructor is deleted from the `instructors` table, all the related programs will also be deleted from the `programs` table.
Let me know if you have any further questions!
1、创建用户user1,user2,user3。 2、授予用户user1和user2在student表上的select权限,对sc表grade属性的修改权限,并且允许user2将其被授予的权限转授其他用户。使用user2登录系统,授予user3对sc表grade属性的修改权限。使用root用户登录,收回user1对student表的select权限。 3、创建计算机系学生视图cs_student,创建角色cs_teacher,授予该角色对cs_student视图的select权限。将该角色授予user3。 4、为sc表添加check约束,确保geade取值在0到100之间。为sc表添加约束,确保当在student表删除学生数据时,删除sc中对用的选课纪录。为sc表添加约束,确保挡在course表中修改sno属性时,sc表中记录做相应修改。 5、使用alter table删除约束第4题中添加的check约束。 6、使用cursor(游标)输出student的内容。 7、创建过程getscore,该过程输入学号和课程号信息,返回对应的成绩 8、创建函数getdept,该函数输入学生的学号,返回对应的sdept 9、在sc表创建触发器tscore,确保“1”号课的成绩大于80. 10|、实现约束:数据库这么课程的选课人数不能超过5人
1. 创建用户user1,user2,user3
```
CREATE USER user1 IDENTIFIED BY 'password1';
CREATE USER user2 IDENTIFIED BY 'password2';
CREATE USER user3 IDENTIFIED BY 'password3';
```
2. 授予用户user1和user2在student表上的select权限,对sc表grade属性的修改权限,并且允许user2将其被授予的权限转授其他用户。使用user2登录系统,授予user3对sc表grade属性的修改权限。使用root用户登录,收回user1对student表的select权限。
```
-- 授予select权限
GRANT SELECT ON student TO user1, user2;
-- 授予对sc表grade属性的修改权限
GRANT UPDATE (grade) ON sc TO user1, user2 WITH GRANT OPTION;
-- user2将其被授予的权限转授其他用户
GRANT UPDATE (grade) ON sc TO user3 WITH GRANT OPTION;
-- 收回user1对student表的select权限
REVOKE SELECT ON student FROM user1;
```
3. 创建计算机系学生视图cs_student,创建角色cs_teacher,授予该角色对cs_student视图的select权限。将该角色授予user3。
```
-- 创建视图cs_student
CREATE VIEW cs_student AS
SELECT * FROM student WHERE sdept = '计算机系';
-- 创建角色cs_teacher
CREATE ROLE cs_teacher;
-- 授予对cs_student视图的select权限
GRANT SELECT ON cs_student TO cs_teacher;
-- 将角色cs_teacher授权给user3
GRANT cs_teacher TO user3;
```
4. 为sc表添加check约束,确保grade取值在0到100之间。为sc表添加约束,确保当在student表删除学生数据时,删除sc中对应的选课记录。为sc表添加约束,确保在course表中修改sno属性时,sc表中记录做相应修改。
```
-- 为sc表添加check约束
ALTER TABLE sc ADD CONSTRAINT ck_grade CHECK (grade >= 0 AND grade <= 100);
-- 为sc表添加外键约束
ALTER TABLE sc ADD CONSTRAINT fk_sc_sno FOREIGN KEY (sno) REFERENCES student(sno) ON DELETE CASCADE;
ALTER TABLE sc ADD CONSTRAINT fk_sc_cno FOREIGN KEY (cno) REFERENCES course(cno) ON UPDATE CASCADE;
```
5. 使用alter table删除约束第4题中添加的check约束。
```
ALTER TABLE sc DROP CONSTRAINT ck_grade;
```
6. 使用cursor(游标)输出student的内容。
```
DECLARE @sno CHAR(9), @sname VARCHAR(20), @ssex VARCHAR(10), @sage INT, @sdept VARCHAR(20);
DECLARE student_cursor CURSOR FOR
SELECT sno, sname, ssex, sage, sdept FROM student;
OPEN student_cursor;
FETCH NEXT FROM student_cursor INTO @sno, @sname, @ssex, @sage, @sdept;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @sno + ' ' + @sname + ' ' + @ssex + ' ' + CONVERT(VARCHAR(10), @sage) + ' ' + @sdept;
FETCH NEXT FROM student_cursor INTO @sno, @sname, @ssex, @sage, @sdept;
END
CLOSE student_cursor;
DEALLOCATE student_cursor;
```
7. 创建过程getscore,该过程输入学号和课程号信息,返回对应的成绩。
```
CREATE PROCEDURE getscore
@sno CHAR(9),
@cno CHAR(4),
@grade INT OUTPUT
AS
BEGIN
SELECT @grade = grade FROM sc WHERE sno = @sno AND cno = @cno;
END
```
8. 创建函数getdept,该函数输入学生的学号,返回对应的sdept。
```
CREATE FUNCTION getdept (@sno CHAR(9))
RETURNS VARCHAR(20)
AS
BEGIN
DECLARE @sdept VARCHAR(20);
SELECT @sdept = sdept FROM student WHERE sno = @sno;
RETURN @sdept;
END
```
9. 在sc表创建触发器tscore,确保“1”号课的成绩大于80。
```
CREATE TRIGGER tscore
ON sc
AFTER INSERT, UPDATE
AS
BEGIN
IF EXISTS (SELECT * FROM inserted WHERE cno = '1' AND grade <= 80)
RAISERROR('成绩必须大于80', 16, 1);
END
```
10. 实现约束:数据库中某门课程的选课人数不能超过5人。
```
CREATE TRIGGER tselect
ON sc
AFTER INSERT
AS
BEGIN
IF EXISTS (SELECT cno FROM sc GROUP BY cno HAVING COUNT(*) > 5)
RAISERROR('选课人数不能超过5人', 16, 1);
END
```
阅读全文