CREATE TABLE TB_CLAZZ(
ID INT PRIMARY KEY ,
CODE VARCHAR2(20),
TC VARCHAR2(10)
);
CREATE TABLE TB_STUDENT(
ID INT PRIMARY KEY,
NAME VARCHAR2(15) NOT NULL,
SEX VARCHAR2(2) NOT NULL,
AGE INT NOT NULL,
ADDRESS VARCHAR2(150) ,
PHNNE VARCHAR2(20) ,
EMAIL VARCHAR2(20) ,
CLAZZ_ID INT,
CONSTRAINTS CLAZZ_ID_FK FOREIGN KEY (CLAZZ_ID) REFERENCES TB_CLAZZ(ID)
);
DROP TABLE TB_STUDENT;
CREATE SEQUENCE SEQ_TB_CLAZZ;
CREATE SEQUENCE SEQ_TB_STUDENT;
INSERT INTO TB_CLAZZ VALUES (SEQ_TB_CLAZZ.Nextval,'软件工程1班','aa');
INSERT INTO TB_CLAZZ VALUES (SEQ_TB_CLAZZ.Nextval,'软件工程2班','bb');
INSERT INTO TB_CLAZZ VALUES (SEQ_TB_CLAZZ.nextval,'软件工程3班','cc');
SELECT * FROM tb_clazz;
INSERT INTO tb_Student
VALUES
(SEQ_TB_STUDENT.NEXTVAL,'小强','男',20,'湖南省长沙市伍家岭江南苑9栋203号','0731-4230123','xq@sina.com',1);
INSERT INTO tb_Student
VALUES
(SEQ_TB_STUDENT.NEXTVAL,'李云','女',19,'湖南省长沙市东风路东风新村21栋502号','0731-4145268','ly@163.com',1);
INSERT INTO tb_Student
VALUES
(SEQ_TB_STUDENT.NEXTVAL,'鲁智深','男',30,'湖南省株洲市601厂宿舍15栋308号','0732-8342567','',2);
INSERT INTO tb_Student
VALUES
(SEQ_TB_STUDENT.NEXTVAL,'张飞','男',28,'湖南省郴洲市人民医院20栋301号','0735-2245214',NULL,2);
INSERT INTO tb_Student
VALUES
(SEQ_TB_STUDENT.NEXTVAL,'翠花','女',21,'湖南省长沙市望月湖12栋403号','0731-8325124','ch@sina.com',3);
SELECT *
FROM TB_STUDENT S,TB_CLAZZ C
WHERE S.CLAZZ_ID = C.ID
AND S.NAME = '张飞';
--orcle10之前的单表查询
DECLARE
--1.定义游标Cursor
CURSOR c_tb_clazz IS SELECT * FROM tb_clazz;
--定义一个rowtyoe
rt_tb_clazz tb_clazz%ROWTYPE;
BEGIN
--2.打开游标
OPEN c_tb_clazz;
--3.fecth游标,如果已经到了游标的最后一行会重复提取数据
LOOP
FETCH c_tb_clazz INTO rt_tb_clazz;
--当c_tb_clazz%NOTFOUND 返回true的时候停止
EXIT WHEN c_tb_clazz%NOTFOUND;
dbms_output.put_line('clazz_id:'||rt_tb_clazz.id||' code:'||rt_tb_clazz.code||' tc:'||rt_tb_clazz.tc);
END LOOP;
--4.关闭游标
CLOSE c_tb_clazz;
END;
/**
clazz_id:1 code:软件工程1班 tc:aa
clazz_id:2 code:软件工程2班 tc:bb
clazz_id:3 code:软件工程3班 tc:cc
*/
--orcle10之前的多表查询
DECLARE
CURSOR c_tb_clazz IS SELECT * FROM tb_clazz;
rt_tb_clazz tb_clazz%ROWTYPE;
--定义带参数的查询,参数可以作为条件
CURSOR c_tb_student(v_student_id tb_student.id%TYPE) IS SELECT * FROM tb_student WHERE ID = v_student_id;
rt_tb_student tb_student%ROWTYPE;
BEGIN
OPEN c_tb_clazz;
LOOP
FETCH c_tb_clazz INTO rt_tb_clazz;
EXIT WHEN c_tb_clazz%NOTFOUND;
dbms_output.put_line('clazz_id:'||rt_tb_clazz.id||' code:'||rt_tb_clazz.code||' tc:'||rt_tb_clazz.tc);
--调用带一个参数的游标
OPEN c_tb_student(rt_tb_clazz.id);
LOOP
FETCH c_tb_student INTO rt_tb_student;
EXIT WHEN c_tb_student%NOTFOUND;
dbms_output.put_line(rt_tb_student.id||' '||rt_tb_student.name||' '||rt_tb_student.sex||' '||rt_tb_student.phnne||' '||rt_tb_student.address||' '||rt_tb_student.email);
END LOOP c_tb_student;
CLOSE c_tb_student;
dbms_output.put_line(' ');
END LOOP;
CLOSE c_tb_clazz;
END;
/**
clazz_id:1 code:软件工程1班 tc:aa
clazz_id:2 code:软件工程2班 tc:bb
2 小强 男 0731-4230123 湖南省长沙市伍家岭江南苑9栋203号 xq@sina.com
clazz_id:3 code:软件工程3班 tc:cc
3 李云 女 0731-4145268 湖南省长沙市东风路东风新村21栋502号 ly@163.com
*/
--orcle10之后的单表查询
DECLARE
--定义游标
CURSOR c_tb_clazz IS SELECT * FROM tb_clazz ;
BEGIN
--循环取出游标的数据
FOR rt_tb_clazz IN c_tb_clazz
LOOP
dbms_output.put_line('clazz_id:'||rt_tb_clazz.id||' code:'||rt_tb_clazz.code||' tc:'||rt_tb_clazz.tc);
END LOOP;
END;
/**
clazz_id:1 code:软件工程1班 tc:aa
clazz_id:2 code:软件工程2班 tc:bb
clazz_id:3 code:软件工程3班 tc:cc
*/
--orcle10之后多表格查询
DECLARE
CURSOR c_tb_clazz IS SELECT * FROM tb_clazz;
CURSOR c_tb_student(v_clazz_id tb_student.id%TYPE) IS SELECT * FROM tb_student WHERE ID = v_clazz_id;
BEGIN
FOR rt_tb_clazz IN c_tb_clazz
LOOP
dbms_output.put_line('clazz_id:'||rt_tb_clazz.id||' '||rt_tb_clazz.code||' '||rt_tb_clazz.tc);
FOR rt_tb_student IN c_tb_student(rt_tb_clazz.id)
LOOP
dbms_output.put_line(rt_tb_student.id||' '||rt_tb_student.name||' '||rt_tb_student.sex||' '||rt_tb_student.phnne||' '||rt_tb_student.address||' '||rt_tb_student.email);
END LOOP;
dbms_output.put_line(' ');
END LOOP;
END;
/**
clazz_id:1 软件工程1班 aa
clazz_id:2 软件工程2班 bb
2 小强 男 0731-4230123 湖南省长沙市伍家岭江南苑9栋203号 xq@sina.com
clazz_id:3 软件工程3班 cc
3 李云 女 0731-4145268 湖南省长沙市东风路东风新村21栋502号 ly@163.com
ID INT PRIMARY KEY ,
CODE VARCHAR2(20),
TC VARCHAR2(10)
);
CREATE TABLE TB_STUDENT(
ID INT PRIMARY KEY,
NAME VARCHAR2(15) NOT NULL,
SEX VARCHAR2(2) NOT NULL,
AGE INT NOT NULL,
ADDRESS VARCHAR2(150) ,
PHNNE VARCHAR2(20) ,
EMAIL VARCHAR2(20) ,
CLAZZ_ID INT,
CONSTRAINTS CLAZZ_ID_FK FOREIGN KEY (CLAZZ_ID) REFERENCES TB_CLAZZ(ID)
);
DROP TABLE TB_STUDENT;
CREATE SEQUENCE SEQ_TB_CLAZZ;
CREATE SEQUENCE SEQ_TB_STUDENT;
INSERT INTO TB_CLAZZ VALUES (SEQ_TB_CLAZZ.Nextval,'软件工程1班','aa');
INSERT INTO TB_CLAZZ VALUES (SEQ_TB_CLAZZ.Nextval,'软件工程2班','bb');
INSERT INTO TB_CLAZZ VALUES (SEQ_TB_CLAZZ.nextval,'软件工程3班','cc');
SELECT * FROM tb_clazz;
INSERT INTO tb_Student
VALUES
(SEQ_TB_STUDENT.NEXTVAL,'小强','男',20,'湖南省长沙市伍家岭江南苑9栋203号','0731-4230123','xq@sina.com',1);
INSERT INTO tb_Student
VALUES
(SEQ_TB_STUDENT.NEXTVAL,'李云','女',19,'湖南省长沙市东风路东风新村21栋502号','0731-4145268','ly@163.com',1);
INSERT INTO tb_Student
VALUES
(SEQ_TB_STUDENT.NEXTVAL,'鲁智深','男',30,'湖南省株洲市601厂宿舍15栋308号','0732-8342567','',2);
INSERT INTO tb_Student
VALUES
(SEQ_TB_STUDENT.NEXTVAL,'张飞','男',28,'湖南省郴洲市人民医院20栋301号','0735-2245214',NULL,2);
INSERT INTO tb_Student
VALUES
(SEQ_TB_STUDENT.NEXTVAL,'翠花','女',21,'湖南省长沙市望月湖12栋403号','0731-8325124','ch@sina.com',3);
SELECT *
FROM TB_STUDENT S,TB_CLAZZ C
WHERE S.CLAZZ_ID = C.ID
AND S.NAME = '张飞';
--orcle10之前的单表查询
DECLARE
--1.定义游标Cursor
CURSOR c_tb_clazz IS SELECT * FROM tb_clazz;
--定义一个rowtyoe
rt_tb_clazz tb_clazz%ROWTYPE;
BEGIN
--2.打开游标
OPEN c_tb_clazz;
--3.fecth游标,如果已经到了游标的最后一行会重复提取数据
LOOP
FETCH c_tb_clazz INTO rt_tb_clazz;
--当c_tb_clazz%NOTFOUND 返回true的时候停止
EXIT WHEN c_tb_clazz%NOTFOUND;
dbms_output.put_line('clazz_id:'||rt_tb_clazz.id||' code:'||rt_tb_clazz.code||' tc:'||rt_tb_clazz.tc);
END LOOP;
--4.关闭游标
CLOSE c_tb_clazz;
END;
/**
clazz_id:1 code:软件工程1班 tc:aa
clazz_id:2 code:软件工程2班 tc:bb
clazz_id:3 code:软件工程3班 tc:cc
*/
--orcle10之前的多表查询
DECLARE
CURSOR c_tb_clazz IS SELECT * FROM tb_clazz;
rt_tb_clazz tb_clazz%ROWTYPE;
--定义带参数的查询,参数可以作为条件
CURSOR c_tb_student(v_student_id tb_student.id%TYPE) IS SELECT * FROM tb_student WHERE ID = v_student_id;
rt_tb_student tb_student%ROWTYPE;
BEGIN
OPEN c_tb_clazz;
LOOP
FETCH c_tb_clazz INTO rt_tb_clazz;
EXIT WHEN c_tb_clazz%NOTFOUND;
dbms_output.put_line('clazz_id:'||rt_tb_clazz.id||' code:'||rt_tb_clazz.code||' tc:'||rt_tb_clazz.tc);
--调用带一个参数的游标
OPEN c_tb_student(rt_tb_clazz.id);
LOOP
FETCH c_tb_student INTO rt_tb_student;
EXIT WHEN c_tb_student%NOTFOUND;
dbms_output.put_line(rt_tb_student.id||' '||rt_tb_student.name||' '||rt_tb_student.sex||' '||rt_tb_student.phnne||' '||rt_tb_student.address||' '||rt_tb_student.email);
END LOOP c_tb_student;
CLOSE c_tb_student;
dbms_output.put_line(' ');
END LOOP;
CLOSE c_tb_clazz;
END;
/**
clazz_id:1 code:软件工程1班 tc:aa
clazz_id:2 code:软件工程2班 tc:bb
2 小强 男 0731-4230123 湖南省长沙市伍家岭江南苑9栋203号 xq@sina.com
clazz_id:3 code:软件工程3班 tc:cc
3 李云 女 0731-4145268 湖南省长沙市东风路东风新村21栋502号 ly@163.com
*/
--orcle10之后的单表查询
DECLARE
--定义游标
CURSOR c_tb_clazz IS SELECT * FROM tb_clazz ;
BEGIN
--循环取出游标的数据
FOR rt_tb_clazz IN c_tb_clazz
LOOP
dbms_output.put_line('clazz_id:'||rt_tb_clazz.id||' code:'||rt_tb_clazz.code||' tc:'||rt_tb_clazz.tc);
END LOOP;
END;
/**
clazz_id:1 code:软件工程1班 tc:aa
clazz_id:2 code:软件工程2班 tc:bb
clazz_id:3 code:软件工程3班 tc:cc
*/
--orcle10之后多表格查询
DECLARE
CURSOR c_tb_clazz IS SELECT * FROM tb_clazz;
CURSOR c_tb_student(v_clazz_id tb_student.id%TYPE) IS SELECT * FROM tb_student WHERE ID = v_clazz_id;
BEGIN
FOR rt_tb_clazz IN c_tb_clazz
LOOP
dbms_output.put_line('clazz_id:'||rt_tb_clazz.id||' '||rt_tb_clazz.code||' '||rt_tb_clazz.tc);
FOR rt_tb_student IN c_tb_student(rt_tb_clazz.id)
LOOP
dbms_output.put_line(rt_tb_student.id||' '||rt_tb_student.name||' '||rt_tb_student.sex||' '||rt_tb_student.phnne||' '||rt_tb_student.address||' '||rt_tb_student.email);
END LOOP;
dbms_output.put_line(' ');
END LOOP;
END;
/**
clazz_id:1 软件工程1班 aa
clazz_id:2 软件工程2班 bb
2 小强 男 0731-4230123 湖南省长沙市伍家岭江南苑9栋203号 xq@sina.com
clazz_id:3 软件工程3班 cc
3 李云 女 0731-4145268 湖南省长沙市东风路东风新村21栋502号 ly@163.com
*/
暂时没有注释,有时间再补上