如何删掉数据库中的表、视图、触发器等所有的数据,然后重新整
时间: 2024-12-23 22:12:34 浏览: 8
为了彻底清除数据库中的表、视图、触发器等对象,并重新初始化整个数据库,可以按照以下步骤进行操作:
### 1. 删除现有的表、视图、触发器等对象
#### 删除表
首先,删除所有表。由于表之间可能存在外键约束,建议先禁用外键约束,然后再删除表。
```sql
-- 禁用所有外键约束
ALTER SESSION SET CONSTRAINTS = DEFERRED;
-- 删除表
DROP TABLE Enrollment CASCADE CONSTRAINTS;
DROP TABLE Class CASCADE CONSTRAINTS;
DROP TABLE Member CASCADE CONSTRAINTS;
DROP TABLE Trainer CASCADE CONSTRAINTS;
DROP TABLE Course CASCADE CONSTRAINTS;
```
#### 删除视图
接下来,删除所有视图。
```sql
DROP VIEW v_Course;
DROP VIEW v_Enrollment;
DROP VIEW v_DetailedMember;
DROP VIEW v_DetailedTrainer;
DROP VIEW v_MemberCourses;
DROP VIEW v_MaleMembers;
DROP VIEW v_Member;
```
#### 删除触发器
最后,删除所有触发器。
```sql
DROP TRIGGER before_enroll;
DROP TRIGGER after_delete_class;
DROP TRIGGER instead_of_enrollment;
DROP TRIGGER cascade_delete_course;
DROP TRIGGER trig_drop_objects;
```
### 2. 重新创建表、视图、触发器等对象
#### 重新创建表
按照原始定义重新创建表。
```sql
-- 会员表 (Member)
CREATE TABLE Member (
mid CHAR(6) NOT NULL PRIMARY KEY,
mname CHAR(12) NOT NULL,
msex CHAR(3) NOT NULL CHECK (msex IN ('男', '女')),
mbirthdate DATE NOT NULL,
mphone CHAR(15) UNIQUE
);
-- 教练表 (Trainer)
CREATE TABLE Trainer (
tid CHAR(6) NOT NULL PRIMARY KEY,
tname CHAR(12) NOT NULL,
tsex CHAR(3) NOT NULL CHECK (tsex IN ('男', '女')),
tbirthdate DATE NOT NULL,
texpertise CHAR(50) NOT NULL
);
-- 课程表 (Course)
CREATE TABLE Course (
cid CHAR(6) NOT NULL PRIMARY KEY,
cname CHAR(50) NOT NULL UNIQUE,
duration NUMBER NOT NULL,
capacity NUMBER NOT NULL
);
-- 报名表 (Enrollment)
CREATE TABLE Enrollment (
eid CHAR(6) NOT NULL PRIMARY KEY,
mid CHAR(6) NOT NULL REFERENCES Member(mid),
cid CHAR(6) NOT NULL REFERENCES Course(cid),
edate DATE NOT NULL
);
-- 上课表 (Class)
CREATE TABLE Class (
class_id CHAR(6) NOT NULL PRIMARY KEY,
cid CHAR(6) NOT NULL REFERENCES Course(cid),
tid CHAR(6) NOT NULL REFERENCES Trainer(tid) ON DELETE CASCADE,
class_date DATE NOT NULL,
start_time TIMESTAMP NOT NULL,
end_time TIMESTAMP NOT NULL
);
```
#### 重新创建视图
按照原始定义重新创建视图。
```sql
-- 1. 创建课程视图 (v_Course)
CREATE OR REPLACE VIEW v_Course AS
SELECT cid, cname, duration, capacity
FROM Course;
-- 2. 创建报名视图 (v_Enrollment)
CREATE OR REPLACE VIEW v_Enrollment AS
SELECT eid, mid, cid, edate
FROM Enrollment;
-- 3. 创建会员详细视图 (v_DetailedMember)
CREATE OR REPLACE VIEW v_DetailedMember AS
SELECT mid, mname, msex, mbirthdate, mphone
FROM Member;
-- 4. 创建教练详细视图 (v_DetailedTrainer)
CREATE OR REPLACE VIEW v_DetailedTrainer AS
SELECT tid, tname, tsex, tbirthdate, texpertise
FROM Trainer;
-- 5. 创建会员课程视图 (v_MemberCourses)
CREATE OR REPLACE VIEW v_MemberCourses AS
SELECT M.mid, M.mname, C.cid, C.cname, E.edate
FROM Member M
JOIN Enrollment E ON M.mid = E.mid
JOIN Course C ON E.cid = C.cid;
-- 6. 创建男性会员视图 (v_MaleMembers)
CREATE OR REPLACE VIEW v_MaleMembers AS
SELECT mid, mname, msex, mbirthdate, mphone
FROM Member
WHERE msex = '男';
-- 7. 创建会员视图 (v_Member)
CREATE OR REPLACE VIEW v_Member AS
SELECT mid, mname, msex, mbirthdate, mphone
FROM Member;
```
#### 重新创建触发器
按照原始定义重新创建触发器。
```sql
-- 事前触发器
CREATE OR REPLACE TRIGGER before_enroll
BEFORE INSERT ON Enrollment
FOR EACH ROW
DECLARE
v_capacity NUMBER;
v_count NUMBER;
BEGIN
-- 获取课程容量
SELECT capacity INTO v_capacity FROM Course WHERE cid = :NEW.cid;
-- 获取当前课程的报名人数
SELECT COUNT(*) INTO v_count FROM Enrollment WHERE cid = :NEW.cid;
-- 判断是否超过容量
IF v_count >= v_capacity THEN
RAISE_APPLICATION_ERROR(-20001, '课程已满员,无法报名');
END IF;
END;
-- 事后触发器
CREATE OR REPLACE TRIGGER after_delete_class
AFTER DELETE ON Class
FOR EACH ROW
BEGIN
DELETE FROM Enrollment WHERE cid = :OLD.cid;
END;
-- 替换触发器
CREATE OR REPLACE TRIGGER instead_of_enrollment
INSTEAD OF INSERT ON v_Enrollment
FOR EACH ROW
BEGIN
INSERT INTO Enrollment (eid, mid, cid, edate) VALUES (:NEW.eid, :NEW.mid, :NEW.cid, :NEW.edate);
END;
-- 级联触发器
CREATE OR REPLACE TRIGGER cascade_delete_course
AFTER DELETE ON Course
FOR EACH ROW
BEGIN
DELETE FROM Enrollment WHERE cid = :OLD.cid;
END;
-- 系统触发器
CREATE TABLE DropObjects (
object_name VARCHAR2(30),
object_type VARCHAR2(20),
drop_date DATE
);
CREATE OR REPLACE TRIGGER trig_drop_objects
BEFORE DROP ON SCHEMA
BEGIN
INSERT INTO DropObjects (object_name, object_type, drop_date) VALUES (ORA_DICT_OBJ_NAME, ORA_DICT_OBJ_TYPE, SYSDATE);
END;
```
### 3. 插入初始数据
如果有需要,可以重新插入初始数据。
```sql
-- 会员表 (Member)
INSERT INTO Member VALUES ('M00001', '张三', '男', TO_DATE('19900101', 'YYYYMMDD'), '1234567890');
INSERT INTO Member VALUES ('M00002', '李四', '女', TO_DATE('19920202', 'YYYYMMDD'), '0987654321');
-- 其他会员数据...
-- 教练表 (Trainer)
INSERT INTO Trainer VALUES ('T00001', '刘一', '男', TO_DATE('19850303', 'YYYYMMDD'), '健身教练');
INSERT INTO Trainer VALUES ('T00002', '孙二', '女', TO_DATE('19880404', 'YYYYMMDD'), '瑜伽教练');
-- 其他教练数据...
-- 课程表 (Course)
INSERT INTO Course VALUES ('C00001', '基础健身', 60, 20);
INSERT INTO Course VALUES ('C00002', '高级瑜伽', 90, 15);
-- 其他课程数据...
-- 报名表 (Enrollment)
INSERT INTO Enrollment VALUES ('E00001', 'M00001', 'C00001', SYSDATE);
INSERT INTO Enrollment VALUES ('E00002', 'M00002', 'C00002', SYSDATE);
-- 其他报名数据...
-- 上课表 (Class)
INSERT INTO Class VALUES ('CL0001', 'C00001', 'T00001', SYSDATE, TO_TIMESTAMP('2023-10-01 08:00:00', 'YYYY-MM-DD HH:MI:SS'), TO_TIMESTAMP('2023-10-01 09:00:00', 'YYYY-MM-DD HH:MI:SS'));
INSERT INTO Class VALUES ('CL0002', 'C00002', 'T00002', SYSDATE, TO_TIMESTAMP('2023-10-01 10:00:00', 'YYYY-MM-DD HH:MI:SS'), TO_TIMESTAMP('2023-10-01 11:30:00', 'YYYY-MM-DD HH:MI:SS'));
-- 其他上课数据...
```
通过以上步骤,您可以彻底清空并重新初始化数据库中的表、视图、触发器等对象。
阅读全文