达梦产品手册
SELECT status$ as 状态 FROM v$instance;
SELECT banner as 版本信息 FROM v$version;
-- 使用 CREATE USER 语句创建 DM 用户,登录密码为 "dameng123"。
CREATE USER DM IDENTIFIED BY "dameng123";
-- 使用GRANT语句给DM用户授予RESOURCE角色
GRANT RESOURCE TO DM;
-- 给 DM 用户授予 dmhr 用户下 employee 表的 select 权限;
GRANT SELECT ON dmhr.employee TO DM;
-- 给 DM 用户授予 dmhr 用户下 department 表的 select 权限;
GRANT SELECT ON dmhr.department TO DM;
-- 给 DM 用户授予 创建表的权限
GRANT CREATE TABLE to DM;
-- 通过字典表 dba_users 查看基本信息
SELECT username,account_status,created FROM dba_users WHERE username='DM';
从输出结果看出,用户状态 account_status 为 open,输出的时间为用户的创建时间。
-- 使用 conn 命令切换用户。
conn DM/dameng123;
注意:windows系统在DM管理工具中使用该命令无法完成用户切换,直接断开登录后重新连接,更换登录用户即可。
-- 使用 user 关键字返回当前登录用户。
SELECT user FROM DUAL;
-- 创建employee表
CREATE TABLE employee
(
employee_id INTEGER,
employee_name VARCHAR2(20) NOT NULL,
hire_date DATE,
salary INTEGER,
department_id INTEGER NOT NULL
);
-- 使用 CREATE TABLE 语句创建 department 表
CREATE TABLE department
(
department_id INTEGER PRIMARY KEY,
department_name VARCHAR(30) NOT NULL
);
-- 非空约束
ALTER TABLE employee MODIFY( hire_date not null);
-- 主键约束
ALTER TABLE employee ADD constraint pk_empid PRIMARY KEY(employee_id);
-- 外键约束
ALTER TABLE employee ADD constraint fk_dept FOREIGN KEY (department_id) REFERENCES department (department_id);
SELECT table_name, constraint_name, constraint_type FROM all_constraints WHERE owner='DM' AND table_name='EMPLOYEE';
ALTER TABLE "DM"."EMPLOYEE" add "IS_REPEAT" VARCHAR(255);
COMMENT ON COLUMN "DM"."EMPLOYEE"."IS_REPEAT" IS '是否重复,1、是,0、否';
ALTER TABLE "DM"."EMPLOYEE" DROP COLUMN IS_REPEAT;
-- 插入数据
INSERT INTO department VALUES(666, '数据库产品中心');
INSERT INTO employee VALUES (9999, '王达梦','2008-05-30 00:00:00', 30000, 666);
commit;
-- 因为 employee 员工表和 department 部门表存在主外键约束,所以此示例中须按顺序执行插入语句,即先在 department 表中插入数据。
-- 更新数据
UPDATE employee SET salary='35000' WHERE employee_id=9999;
commit;
-- 验证数据
SELECT salary,employee_id FROM employee;
-- 删除数据
DELETE FROM employee;
DELETE FROM department WHERE department_id=666;
commit;
-- 验证数据
SELECT * FROM employee;
-- 在 t1 表中批量插入 100000 条数据记录。
CREATE TABLE t1 AS
SELECT rownum AS id,
trunc(dbms_random.value(0, 100)) AS random_id,
dbms_random.string('x', 20) AS random_string
FROM dual
connect BY level <= 100000;
-- 使用 COUNT(*) 聚集函数统计 t1 表中的总数据记录。
SELECT COUNT(*) FROM t1;
-- 使用 ORDER BY 语句实现选择排序。
SELECT * FROM t1 where rownum<5 ORDER BY id DESC;
-- 插入准备数据
INSERT INTO department (department_id, department_name)
SELECT department_id, department_name FROM dmhr.department;
INSERT INTO employee
(employee_id, employee_name, hire_date, salary, department_id)
SELECT employee_id, employee_name, hire_date, salary,
department_id FROM dmhr.employee;
commit;
SELECT dept.department_name as 部门, count(*) as 人数
FROM employee emp, department dept
where emp.department_id=dept.department_id
GROUP BY dept.department_name
HAVING count(*) > 20;
-- 定义视图
CREATE OR replace VIEW v1 AS
SELECT dept.department_name, emp.employee_name,
emp.salary,emp.hire_date
FROM employee emp, department dept
WHERE salary > 10000
AND hire_date >= '2013-08-01'
AND emp.department_id = dept.department_id;
-- 通过视图简化查询
SELECT * FROM v1 WHERE hire_date > '2014-09-01';
-- 创建普通索引
CREATE INDEX ind_emp_salary ON employee(salary);
SELECT table_name, index_name, index_type
from user_indexes WHERE index_name='IND_EMP_SALARY';
-- 删除索引
DROP INDEX IND_EMP_SALARY;
-- 若该表存在则删除
DROP TABLE IF EXISTS TEST_TEMP;
-- 创建对应的表:
-- 主键自增配置 IDENTITY(1, 1),CLUSTER PRIMARY KEY("ID")
-- 自动创建时间 DEFAULT (CURDATE)
CREATE TABLE "TEST_TEMP"
(
"ID" INT IDENTITY(1, 1) NOT NULL,
"PROJECT_KEY" VARCHAR(100),
"CREAT_TIME" DATE DEFAULT (CURDATE),
CLUSTER PRIMARY KEY("ID"))
STORAGE(ON "TEST", CLUSTERBTR) ;
-- 语法
SET IDENTITY_INSERT [<模式名>.]<表名> ON | OFF;
-- 初始化数据,自增id赋值。需要指定列且IDENTITY_INSERT设置ON
SET IDENTITY_INSERT SYSDBA.TEST_TEMP ON;
INSERT INTO TEST_TEMP(ID,PROJECT_KEY,CREAT_TIME) VALUES (1, '测试项目', '2022-12-15 14:15:30');
-- 记得提交,否则无法真正的落库。
commit;
-- 自动生成日期,不带时分秒
"CREAT_TIME" DATE DEFAULT (CURDATE),
-- 自动生成日期,带时分秒
"CREAT_TIME" TIMESTAMP DEFAULT (SYSDATE),
通过触发器脚本实现
create trigger UPDATE_TIME_CUSTOM_NAME
before update on "DEV"."CUSTOM_NAME" for each row
begin
new.UPDATE_TIME:=sysdate;
end;
解析
UPDATE_TIME_CUSTOM_NAME:触发名称
DEV: 模式名
CUSTOM_NAME:表名称
UPDATE_TIME:字段名称
– 字段配置,自动生成日期,带时分秒
“UPDATE_TIME” TIMESTAMP DEFAULT (SYSDATE),
-- SYSDBA为模式,TEST_TEMP 为表名称
-- 查询表字段情况,但不包含注释信息
select * from all_tab_columns where owner='SYSDBA' and Table_Name='TEST_TEMP'
-- 查询表字段的注释信息
select * from dba_col_comments where owner='SYSDBA' and Table_Name='TEST_TEMP'
-- 将SYSDBA模式的TEST_TEMP表中的UPDATE_TIME字段名称修改为CREATE_TIME
ALTER TABLE "SYSDBA"."TEST_TEMP" RENAME COLUMN "UPDATE_TIME" TO "CREATE_TIME";
COMMENT ON COLUMN "SYSDBA"."TEST_TEMP"."CREATE_TIME" IS '数据创建时间';
-- 将SYSDBA模式的TEST_TEMP表中的CREATE_TIME 字段类型修改为TIMESTAMP DEFAULT SYSDATE
ALTER TABLE "SYSDBA"."TEST_TEMP" MODIFY CREATE_TIME TIMESTAMP DEFAULT SYSDATE