YashanDB练习SQL

  1. 查询当前实例的信息:
SELECT * FROM v$instance;
  1. 查询数据库的信息:
SELECT * FROM v$database;
  1. 查询数据库名称:
SELECT database_name FROM v$database;
  1. 创建用户sales并授予DBA角色:
CREATE USER sales IDENTIFIED BY "Sxc1%sd27";
GRANT DBA TO sales;
  1. 创建表area,并插入数据:
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;
  1. 查询area表的数据:
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;
  1. 创建表branches,并插入数据:
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;
  1. 查询branches表的数据,并进行JOIN操作:
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;
  1. 描述area表的结构:
DESC area;
  1. 修改area表的数据:
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;
  1. 创建和操作area_01表:
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; --删除表
  1. 创建和操作索引:
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; --删除索引
  1. 创建视图v_area_03:
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;
  1. 创建视图v_area_03并查询:
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;
  1. 创建用户ycauser并授权:
CREATE USER ycauser IDENTIFIED BY yca123456;
GRANT CONNECT TO ycauser;
REVOKE CONNECT FROM ycauser;
GRANT CONNECT TO ycauser;
GRANT RESOURCE TO ycauser;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别晃我的可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值