2024-09-12
‌SYSTEM表空间‌:存放数据库的字典文件信息,用户不能在这个表空间下创建表和索引。


‌ROLL表空间‌:回滚表空间,由达梦数据库自动维护,用于存放事务运行过程中执行DML操作之前的值,从而为访问该表的其他用户提供数据的一致性视图。


‌MAIN表空间‌:默认表空间,是达梦数据库系统默认的表空间,如果没有指定默认表空间,则系统自动指定MAIN表空间为用户默认的表空间。


‌TEMP表空间‌:临时表空间,当用户的SQL语句需要磁盘空间来完成某个操作时,达梦数据库会从TEMP表空间分配临时段。




UPDATE 模式名.表名 SET 列名1 = 值1, 列名2 = 值2, ... [WHERE 条件];


DELETE FROM 模式名.表名 [WHERE 条件];


SELECT column1, column2, ... 
FROM 模式名.表名
[WHERE 条件]
[GROUP BY 列名]
[ORDER BY 列名 [ASC|DESC]]
[HAVING 条件];






-- 创建学生表
CREATE TABLE STUDENTS(
    STUDENT_ID INTEGER PRIMARY KEY IDENTITY(1,1),
    NAME VARCHAR(50) NOT NULL,
    BIRTH_DATE DATE NOT NULL,
    GENDER CHAR(1) CHECK (GENDER IN ('M','F')) NOT NULL,
    EMAIL VARCHAR(100) UNIQUE NOT NULL,
    PHONE_NUMBER VARCHAR(15)
);
 
-- 创建教师表
CREATE TABLE TEACHERS(
    TEACHER_ID INTEGER PRIMARY KEY IDENTITY(1,1),
    NAME VARCHAR(50) NOT NULL,
    EMAIL VARCHAR(100) UNIQUE NOT NULL,
    PHONE_NUMBER VARCHAR(15),
    TITLE VARCHAR(30)
);
 
-- 创建课程表
CREATE TABLE COURSES(
    COURSE_ID INTEGER PRIMARY KEY IDENTITY(1,1),
    COURSE_NAME VARCHAR(100) NOT NULL,
    DESCRIPTION TEXT,
    CREDITS INTEGER NOT NULL
);
 
-- 创建注册表
CREATE TABLE REGISTRATIONS(
    STUDENT_ID INTEGER,
    COURSE_ID INTEGER,
    REGISTRATION_DATE DATE NOT NULL,
    GRADE CHAR(1) CHECK (GRADE IN ('A','B','C','D','F')),
    PRIMARY KEY (STUDENT_ID, COURSE_ID),
    FOREIGN KEY (STUDENT_ID) REFERENCES STUDENTS (STUDENT_ID),
    FOREIGN KEY (COURSE_ID) REFERENCES COURSES (COURSE_ID)
);
 
-- 创建授课表
CREATE TABLE TEACHING_ASSIGNMENTS(
    TEACHER_ID INTEGER,
    COURSE_ID INTEGER,
    SEMESTER VARCHAR(10) NOT NULL,
    YEAR INTEGER NOT NULL,
    PRIMARY KEY (TEACHER_ID, COURSE_ID, SEMESTER, YEAR),
    FOREIGN KEY (TEACHER_ID) REFERENCES TEACHERS (TEACHER_ID),
    FOREIGN KEY (COURSE_ID) REFERENCES COURSES (COURSE_ID)
);
 
-- 创建索引以优化查询性能(根据需要来创建)
CREATE INDEX idx_students_email ON STUDENTS (EMAIL);
CREATE INDEX idx_teachers_email ON TEACHERS (EMAIL);
CREATE INDEX idx_courses_name ON COURSES (COURSE_NAME);
CREATE INDEX idx_registrations_student ON REGISTRATIONS (STUDENT_ID);
CREATE INDEX idx_registrations_course ON REGISTRATIONS (COURSE_ID);
CREATE INDEX idx_teaching_assignments_teacher ON TEACHING_ASSIGNMENTS (TEACHER_ID);
CREATE INDEX idx_teaching_assignments_course ON TEACHING_ASSIGNMENTS (COURSE_ID);








create or replace procedure mytest_proc(
parm_OPType in int:=null,
parm_cName in VARCHAR:='',
parm_info OUT int:=null -- 输出参数:用户信息
)
as 
declare
 inparm_sql VARCHAR2(100);  -- 声明局部变量
 inparm_value VARCHAR2(100);  -- 声明局部变量
--declare
 --OPType int:=0;
 --cName varchar(50):='';
 --counter int:=0;
begin

if parm_OPType=101
begin
--select 1 as iResult,'查询到' as cResult;
 
SELECT  top 10 * from PRODUCTION."PRODUCT_CATEGORY" order by PRODUCT_CATEGORYID DESC;

--select count(*) into inparm_value from PRODUCTION."PRODUCT_CATEGORY"; --INTO 
-- parm_info := inparm_value; -- 将查询结果赋值给输出参数
return;
end
ELSE
BEGIN
select -1 as iResult,'没有查询到' as cResult;
return;
--print('22222222');
end;

end








--正确用法1
--call mytest_proc(101);
--正确用法2
--call mytest_proc(parm_OPType=>101);


--declare ssresult int:=0;
--call mytest_proc(parm_OPType=>101,parm_info=>ssresult);




--call mytest_proc(101,'ee',@result);
--select sysdate;
--SELECT * from PRODUCTION."PRODUCT_CATEGORY" order by PRODUCT_CATEGORYID DESC