主题:oracle的问题
1.在我们把附件1这个文件的所有的SQL语句都执行后,我们的数据库所在的状态其实在一个不一致的状态,
比如我们在往registered_students表里面插入数据的时候并没有考虑到要对别的表进行更新(比如对students表和classes表等);
再比如,我们对students表进行插入、删除、更新的时候并没有考虑对major_stats表的影响。
以后我们在实际的工作中可能会经常遇到这种情况,就是你的当前数据库不在一个一致的状态,
那么我们就要编一段程序,把数据库调整到一致的状态。
附件1如下:
PROMPT students table...
DROP TABLE students CASCADE CONSTRAINTS;
CREATE TABLE students (
id NUMBER(5) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(20),
major VARCHAR2(30),
current_credits NUMBER(3)
);
INSERT INTO STUDENTS (id, first_name, last_name, major, current_credits)
VALUES (10000, 'Scott', 'Smith', 'Computer Science', 0);
INSERT INTO STUDENTS (id, first_name, last_name, major, current_credits)
VALUES (10001, 'Margaret', 'Mason', 'History', 0);
INSERT INTO STUDENTS (id, first_name, last_name, major, current_credits)
VALUES (10002, 'Joanne', 'Junebug', 'Computer Science', 0);
INSERT INTO STUDENTS (id, first_name, last_name, major, current_credits)
VALUES (10003, 'Manish', 'Murgratroid', 'Economics', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES(10004, 'Patrick', 'Poll', 'History', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10005, 'Timothy', 'Taller', 'History', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10006, 'Barbara', 'Blues', 'Economics', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10007, 'David', 'Dinsmore', 'Music', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10008, 'Ester', 'Elegant', 'Nutrition', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10009, 'Rose', 'Riznit', 'Music', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10010, 'Rita', 'Razmataz', 'Nutrition', 0);
PROMPT major_stats...
DROP TABLE major_stats;
CREATE TABLE major_stats (
major VARCHAR2(30),
total_credits NUMBER,
total_students NUMBER);
PROMPT rooms table...
DROP TABLE rooms CASCADE CONSTRAINTS;
CREATE TABLE rooms (
room_id NUMBER(5) PRIMARY KEY,
building VARCHAR2(15),
room_number NUMBER(4),
number_seats NUMBER(4),
description VARCHAR2(50)
);
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99999, 'Building 7', 310, 1000, 'Large Lecture Hall');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99998, 'Building 6', 101, 500, 'Small Lecture Hall');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99997, 'Building 6', 150, 50, 'Discussion Room A');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99996, 'Building 6', 160, 50, 'Discussion Room B');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99995, 'Building 6', 170, 50, 'Discussion Room C');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99994, 'Music Building', 100, 10, 'Music Practice Room');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99993, 'Music Building', 200, 1000, 'Concert Room');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99992, 'Building 7', 300, 75, 'Discussion Room D');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99991, 'Building 7', 310, 50, 'Discussion Room E');
PROMPT classes table...
DROP TABLE classes CASCADE CONSTRAINTS;
CREATE TABLE classes (
department CHAR(3),
course NUMBER(3),
description VARCHAR2(2000),
max_students NUMBER(3),
current_students NUMBER(3),
num_credits NUMBER(1),
room_id NUMBER(5),
CONSTRAINT classes_department_course
PRIMARY KEY (department, course),
CONSTRAINT classes_room_id
FOREIGN KEY (room_id) REFERENCES rooms (room_id)
);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('HIS', 101, 'History 101', 30, 0, 4, 99999);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('HIS', 301, 'History 301', 30, 0, 4, 99995);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('CS', 101, 'Computer Science 101', 50, 0, 4, 99998);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('ECN', 203, 'Economics 203', 15, 0, 3, 99997);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('CS', 102, 'Computer Science 102', 35, 0, 4, 99996);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('MUS', 410, 'Music 410', 5, 0, 3, 99994);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('ECN', 101, 'Economics 101', 50, 0, 4, 99992);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('NUT', 307, 'Nutrition 307', 20, 0, 4, 99991);
PROMPT registered_students table...
DROP TABLE registered_students CASCADE CONSTRAINTS;
CREATE TABLE registered_students (
student_id NUMBER(5) NOT NULL,
department CHAR(3) NOT NULL,
course NUMBER(3) NOT NULL,
grade CHAR(1),
CONSTRAINT rs_grade
CHECK (grade IN ('A', 'B', 'C', 'D', 'E')),
CONSTRAINT registeredStudentPK
PRIMARY KEY (student_id,department, course),
CONSTRAINT rs_student_id
FOREIGN KEY (student_id) REFERENCES students (id),
CONSTRAINT rs_department_course
FOREIGN KEY (department, course)
REFERENCES classes (department, course)
);
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10000, 'CS', 102, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10002, 'CS', 102, 'B');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10003, 'CS', 102, 'C');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10000, 'HIS', 101, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10001, 'HIS', 101, 'B');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10002, 'HIS', 101, 'B');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10003, 'HIS', 101, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10004, 'HIS', 101, 'C');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10005, 'HIS', 101, 'C');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10006, 'HIS', 101, 'E');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10007, 'HIS', 101, 'B');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10008, 'HIS', 101, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10009, 'HIS', 101, 'D');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10010, 'HIS', 101, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10008, 'NUT', 307, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10010, 'NUT', 307, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10009, 'MUS', 410, 'B');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10006, 'MUS', 410, 'E');
PROMPT temp_table...
DROP TABLE temp_table;
CREATE TABLE temp_table (
num_col NUMBER,
char_col VARCHAR2(60)
);
请问哪位高手给与指点,谢谢!重谢!只要能执行一定给高分!
比如我们在往registered_students表里面插入数据的时候并没有考虑到要对别的表进行更新(比如对students表和classes表等);
再比如,我们对students表进行插入、删除、更新的时候并没有考虑对major_stats表的影响。
以后我们在实际的工作中可能会经常遇到这种情况,就是你的当前数据库不在一个一致的状态,
那么我们就要编一段程序,把数据库调整到一致的状态。
附件1如下:
PROMPT students table...
DROP TABLE students CASCADE CONSTRAINTS;
CREATE TABLE students (
id NUMBER(5) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(20),
major VARCHAR2(30),
current_credits NUMBER(3)
);
INSERT INTO STUDENTS (id, first_name, last_name, major, current_credits)
VALUES (10000, 'Scott', 'Smith', 'Computer Science', 0);
INSERT INTO STUDENTS (id, first_name, last_name, major, current_credits)
VALUES (10001, 'Margaret', 'Mason', 'History', 0);
INSERT INTO STUDENTS (id, first_name, last_name, major, current_credits)
VALUES (10002, 'Joanne', 'Junebug', 'Computer Science', 0);
INSERT INTO STUDENTS (id, first_name, last_name, major, current_credits)
VALUES (10003, 'Manish', 'Murgratroid', 'Economics', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES(10004, 'Patrick', 'Poll', 'History', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10005, 'Timothy', 'Taller', 'History', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10006, 'Barbara', 'Blues', 'Economics', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10007, 'David', 'Dinsmore', 'Music', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10008, 'Ester', 'Elegant', 'Nutrition', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10009, 'Rose', 'Riznit', 'Music', 0);
INSERT INTO STUDENTS(id, first_name, last_name, major, current_credits)
VALUES (10010, 'Rita', 'Razmataz', 'Nutrition', 0);
PROMPT major_stats...
DROP TABLE major_stats;
CREATE TABLE major_stats (
major VARCHAR2(30),
total_credits NUMBER,
total_students NUMBER);
PROMPT rooms table...
DROP TABLE rooms CASCADE CONSTRAINTS;
CREATE TABLE rooms (
room_id NUMBER(5) PRIMARY KEY,
building VARCHAR2(15),
room_number NUMBER(4),
number_seats NUMBER(4),
description VARCHAR2(50)
);
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99999, 'Building 7', 310, 1000, 'Large Lecture Hall');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99998, 'Building 6', 101, 500, 'Small Lecture Hall');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99997, 'Building 6', 150, 50, 'Discussion Room A');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99996, 'Building 6', 160, 50, 'Discussion Room B');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99995, 'Building 6', 170, 50, 'Discussion Room C');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99994, 'Music Building', 100, 10, 'Music Practice Room');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99993, 'Music Building', 200, 1000, 'Concert Room');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99992, 'Building 7', 300, 75, 'Discussion Room D');
INSERT INTO ROOMS (room_id, building, room_number, number_seats, description)
VALUES (99991, 'Building 7', 310, 50, 'Discussion Room E');
PROMPT classes table...
DROP TABLE classes CASCADE CONSTRAINTS;
CREATE TABLE classes (
department CHAR(3),
course NUMBER(3),
description VARCHAR2(2000),
max_students NUMBER(3),
current_students NUMBER(3),
num_credits NUMBER(1),
room_id NUMBER(5),
CONSTRAINT classes_department_course
PRIMARY KEY (department, course),
CONSTRAINT classes_room_id
FOREIGN KEY (room_id) REFERENCES rooms (room_id)
);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('HIS', 101, 'History 101', 30, 0, 4, 99999);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('HIS', 301, 'History 301', 30, 0, 4, 99995);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('CS', 101, 'Computer Science 101', 50, 0, 4, 99998);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('ECN', 203, 'Economics 203', 15, 0, 3, 99997);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('CS', 102, 'Computer Science 102', 35, 0, 4, 99996);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('MUS', 410, 'Music 410', 5, 0, 3, 99994);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('ECN', 101, 'Economics 101', 50, 0, 4, 99992);
INSERT INTO CLASSES(department, course, description, max_students,
current_students, num_credits, room_id)
VALUES ('NUT', 307, 'Nutrition 307', 20, 0, 4, 99991);
PROMPT registered_students table...
DROP TABLE registered_students CASCADE CONSTRAINTS;
CREATE TABLE registered_students (
student_id NUMBER(5) NOT NULL,
department CHAR(3) NOT NULL,
course NUMBER(3) NOT NULL,
grade CHAR(1),
CONSTRAINT rs_grade
CHECK (grade IN ('A', 'B', 'C', 'D', 'E')),
CONSTRAINT registeredStudentPK
PRIMARY KEY (student_id,department, course),
CONSTRAINT rs_student_id
FOREIGN KEY (student_id) REFERENCES students (id),
CONSTRAINT rs_department_course
FOREIGN KEY (department, course)
REFERENCES classes (department, course)
);
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10000, 'CS', 102, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10002, 'CS', 102, 'B');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10003, 'CS', 102, 'C');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10000, 'HIS', 101, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10001, 'HIS', 101, 'B');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10002, 'HIS', 101, 'B');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10003, 'HIS', 101, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10004, 'HIS', 101, 'C');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10005, 'HIS', 101, 'C');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10006, 'HIS', 101, 'E');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10007, 'HIS', 101, 'B');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10008, 'HIS', 101, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10009, 'HIS', 101, 'D');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10010, 'HIS', 101, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10008, 'NUT', 307, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10010, 'NUT', 307, 'A');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10009, 'MUS', 410, 'B');
INSERT INTO registered_students (student_id, department, course, grade)
VALUES (10006, 'MUS', 410, 'E');
PROMPT temp_table...
DROP TABLE temp_table;
CREATE TABLE temp_table (
num_col NUMBER,
char_col VARCHAR2(60)
);
请问哪位高手给与指点,谢谢!重谢!只要能执行一定给高分!