【Oracle】玩转Oracle数据库:SQL语言
在Oracle数据库中,SQL语言是一种非常重要的技能。以下是一些基本的SQL语句和它们的用法:
- 创建表:
CREATE TABLE Employees (
ID NUMBER PRIMARY KEY,
FirstName VARCHAR2(50),
LastName VARCHAR2(50),
BirthDate DATE,
Email VARCHAR2(100),
Salary NUMBER(10, 2)
);
- 插入数据:
INSERT INTO Employees (ID, FirstName, LastName, BirthDate, Email, Salary)
VALUES (1, 'John', 'Doe', TO_DATE('1980-05-03', 'YYYY-MM-DD'), 'john.doe@example.com', 50000);
- 更新数据:
UPDATE Employees SET Salary = 60000 WHERE ID = 1;
- 删除数据:
DELETE FROM Employees WHERE ID = 1;
- 查询数据:
SELECT * FROM Employees;
- 创建索引:
CREATE INDEX idx_lastname ON Employees(LastName);
- 创建视图:
CREATE VIEW EmployeesView AS
SELECT ID, FirstName, LastName FROM Employees;
- 创建存储过程:
CREATE OR REPLACE PROCEDURE RaiseSalary(p_employee_id IN NUMBER, p_raise_amount IN NUMBER) AS
BEGIN
UPDATE Employees SET Salary = Salary + p_raise_amount WHERE ID = p_employee_id;
END;
/
- 创建触发器:
CREATE OR REPLACE TRIGGER Employees_Audit
AFTER INSERT OR UPDATE OR DELETE ON Employees
FOR EACH ROW
DECLARE
v_user VARCHAR2(100);
BEGIN
IF INSERTING THEN
v_user := 'INSERTED';
ELSIF UPDATING THEN
v_user := 'UPDATED';
ELSE
v_user := 'DELETED';
END IF;
INSERT INTO Employees_Audit (EmployeeID, Action, AuditDate) VALUES (:NEW.ID, v_user, SYSDATE);
END;
/
- 使用SQL进行数据库的备份和恢复:
备份:
BACKUP DATABASE;
恢复:
RESTORE DATABASE;
这些都是SQL语句的基本用法,在实际的数据库操作中,还会涉及到更复杂的查询,如连接查询、子查询、分组和排序等。
评论已关闭