- 查询当前实例的信息:
sql
复制代码
SELECT * FROM v$instance;
- 查询数据库的信息:
sql
复制代码
SELECT * FROM v$database;
- 查询数据库名称:
sql
复制代码
SELECT database_name FROM v$database;
- 创建用户sales并授予DBA角色:
sql
复制代码
CREATE USER sales IDENTIFIED BY "Sxc1%sd27";
GRANT DBA TO sales;
- 创建表area,并插入数据:
sql
复制代码
CREATE TABLE area (
area_no CHAR(2) NOT NULL PRIMARY KEY,
area_name VARCHAR2(60),
DHQ VARCHAR2(20) DEFAULT 'ShenZhen' NOT NULL
);
INSERT INTO area VALUES ('01', '华东', 'Shanghai');
INSERT INTO area VALUES ('02', '华西', 'Chengdu');
INSERT INTO area VALUES ('03', '华南', 'Guangzhou');
INSERT INTO area VALUES ('04', '华北', 'Beijing');
INSERT INTO area VALUES ('05', '华中', 'Wuhan');
COMMIT;
- 查询area表的数据:
sql
复制代码
SELECT area_name FROM area;
SELECT area_no, area_name FROM area;
SELECT * FROM area;
SELECT * FROM area WHERE area_name = '华东';
SELECT * FROM area ORDER BY dhq;
SELECT * FROM area ORDER BY dhq DESC;
- 创建表branches,并插入数据:
sql
复制代码
CREATE TABLE branches (
branch_no CHAR(4) PRIMARY KEY,
branch_name VARCHAR2(200) NOT NULL,
area_no CHAR(2),
address VARCHAR2(200)
);
INSERT INTO branches VALUES ('0001', '深圳', '', '');
INSERT INTO branches VALUES ('0101', '上海', '01', '上海市静安区');
INSERT INTO branches VALUES ('0102', '南京', '01', 'City of Nanjing');
INSERT INTO branches VALUES ('0103', '福州', '01', '');
INSERT INTO branches VALUES ('0104', '厦门', '01', 'Xiamen');
INSERT INTO branches VALUES ('0401', '北京', '04', '');
INSERT INTO branches VALUES ('0402', '天津', '04', '');
INSERT INTO branches VALUES ('0403', '大连', '04', '大连市');
INSERT INTO branches VALUES ('0404', '沈阳', '04', '');
INSERT INTO branches VALUES ('0201', '成都', '02', '');
INSERT INTO branches VALUES ('0501', '武汉', '', '');
INSERT INTO branches VALUES ('0502', '长沙', '05', '');
COMMIT;
- 查询branches表的数据,并进行JOIN操作:
sql
复制代码
SELECT * FROM branches;
SELECT branches.branch_name, branches.address FROM branches INNER JOIN area ON branches.area_no = area.area_no;
SELECT branches.branch_name, branches.address FROM branches RIGHT JOIN area ON branches.area_no = area.area_no;
SELECT branches.branch_name, branches.address FROM branches FULL JOIN area ON branches.area_no = area.area_no;
- 描述area表的结构:
sql
复制代码
DESC area;
- 修改area表的数据:
sql
复制代码
INSERT INTO area(area_no, area_name, dhq) VALUES ('06', '东北', 'Dalian');
COMMIT;
UPDATE area SET dhq='Haerbin' WHERE area_no='06';
COMMIT;
UPDATE area SET dhq='Haerbin';
COMMIT;
DELETE FROM area WHERE area_no='06';
COMMIT;
DELETE FROM area;
COMMIT;
- 创建和操作area_01表:
sql
复制代码
CREATE TABLE area_01 (area_no CHAR(2) NOT NULL PRIMARY KEY, area_name VARCHAR2(60), DHQ VARCHAR2(20) NOT NULL);
ALTER TABLE area_01 ADD NUM NUMBER(20); --添加NUM列
ALTER TABLE area_01 MODIFY DHQ VARCHAR(80); --修改列的长度
ALTER TABLE area_01 DROP COLUMN num; --删除列
DROP TABLE area_01; --删除表
- 创建和操作索引:
sql
复制代码
CREATE TABLE area_02(area_no CHAR(2) NOT NULL PRIMARY KEY, area_name VARCHAR2(60), DHQ VARCHAR2(20) NOT NULL);
CREATE INDEX idx_area_02_area_name ON area_02(area_name);
ALTER INDEX idx_area_02_area_name REBUILD; --重建索引
DROP INDEX idx_area_02_area_name; --删除索引
- 创建视图v_area_03:
sql
复制代码
CREATE TABLE area_03(area_no CHAR(2) NOT NULL PRIMARY KEY, area_name VARCHAR2(60), DHQ VARCHAR2(20) NOT NULL);
INSERT INTO area_03 VALUES ('01', '华东', 'Shanghai');
INSERT INTO area_03 VALUES ('02', '华西', 'Chengdu');
INSERT INTO area_03 VALUES ('03', '华南', 'Guangzhou');
INSERT INTO area_03 VALUES ('04', '华北', 'Beijing');
INSERT INTO area_03 VALUES ('05', '华中', 'Wuhan');
COMMIT;
CREATE OR REPLACE VIEW v_area_03 AS SELECT area_no, area_name, dhq FROM area_03;
- 创建视图v_area_03并查询:
sql
复制代码
CREATE OR REPLACE VIEW v_area_03 AS SELECT area_no,area_name,dhq FROM area_03;
SELECT * FROM v_area_03;
SELECT area_no,area_name,dhq FROM area_03;
DROP VIEW v_area_03;
- 创建用户ycauser并授权:
sql
复制代码
CREATE USER ycauser IDENTIFIED BY yca123456;
GRANT CONNECT TO ycauser;
REVOKE CONNECT FROM ycauser;
GRANT CONNECT TO ycauser;
GRANT RESOURCE TO ycauser;