ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

自己这个整理的sql相关

2022-08-12 10:34:26  阅读:160  来源: 互联网

标签:表已 into sql number qjx SQL 整理 相关 table


创建用户:
SQL> create user Oracle identified by qjx
  2  default tablespace users
  3  temporary tablespace temp;

用户已创建。

SQL> connect system/qjx138
已连接。

SQL> grant connect, resource to Oracle;

授权成功。

SQL> spool off;

创建表代码:
 SQL> create table qjx_student(
  2  ID number(12) not null primary key,
  3  Name varchar2(12),
  4  Xbbh number(4),
  5  Bjbh number(4),
  6  Kcbh number(4),
  7  Xwbh number(4),
  8  Mzbh number(4),
  9  Zzmbbh number(4),
 10  Zcjbh number(4),
 11  Jxjbh number(4),
 12  Zxjbh number(4),
 13  Jcbh number(4),
 14  Zxjdjbh number(4),
 15  Jxjjcbh number(4)
 16  );

表已创建。

SQL> create table qjx_class(
  2  c_bjbh number(4) not null primary key,
  3  c_bjmc varchar(4)
  4  );

表已创建。

SQL> create table qjx_gender(
  2  g_xbbh number(4) primary key,
  3  g_gender varchar(2)
  4  );

表已创建。

SQL> create table qjx_nation(
  2  n_mzbh number(4) primary key,
  3  n_nation varchar(6)
  4  );

表已创建。

SQL> create table qjx_outlook(
  2  o_zzmmbh number(4) primary key,
  3  n_outlokk varchar(4)
  4  );

表已创建。

SQL> create table qjx_score(
  2  s_zcjbh number(4) primary key,
  3  s_score number(4)
  4  );

表已创建。

SQL> create table qjx_course(
  2  c_kcbh number(4) primary key,
  3  c_course varchar2(4)
  4  );

表已创建。

SQL> create table qjx_degree(
  2  d_xwbh number(4) primary key,
  3  d_degree varchar2(4)
  4  );

表已创建。

SQL> create table qjx_reward(
  2  r_jcbh number(4) primary key,
  3  r_reward varchar2(2)
  4  );

表已创建。

SQL> create table qjx_scholarship(
  2  s_jxjbh number(4) primary key,
  3  s_scholarship number(4)
  4  );

表已创建。

SQL> create table qjx_grant(
  2  g_jxjbh number(4) primary key,
  3  g_grant number(4)
  4  );

表已创建。

SQL> create table qjx_scholarlevel(
  2  l_jxjbh number(4) primary key,
  3  l_jxjmc varchar2(4),
  4  l_level varchar2(4)
  5  );

表已创建。

SQL> create table qjx_grantlevel(
  2  l_zxjbh number(4) primary key,
  3  l_zxjmc varchar2(4),
  4  l_level varchar2(4)
  5  );

表已创建。

SQL> create table qjx_college(
  2  c_xybh number(4) primary key,
  3  c_xymc varchar2(4)
  4  );

表已创建。

SQL> create table qjx_system(
  2  s_xbh number(4) primary key,
  3  s_xmc varchar2(4)
  4  );

表已创建。


SQL> alter table qjx_student add constraint fk_bjbh foreign key(Bjbh) references
  2  qjx_class(c_bjbh);

表已更改。

SQL> alter table qjx_student add constraint fk_xbbh foreign key(Xbbh) references
  2  qjx_gender(g_xbbh);

表已更改。

SQL> alter table qjx_student add constraint fk_mzbh foreign key(Mzbh) references
  2  qjx_nation(n_mzbh);

表已更改。

SQL> alter table qjx_student add constraint fk_zzmmbh foreign key(Zzmbbh) references
  2  qjx_outlook(o_zzmmbh);

表已更改。

SQL> alter table qjx_student add constraint fk_zcjbh foreign key(Zcjbh) references qjx_score(s_zcjbh);

表已更改。

SQL> alter table qjx_student add constraint fk_kcbh foreign key(Kcbh) references qjx_course(c_kcbh);

表已更改。

SQL> alter table qjx_student add constraint fk_xwbh foreign key(Xwbh) references qjx_degree(d_xwbh);

表已更改。

SQL> alter table qjx_student add constraint fk_jcbh foreign key(Jcbh) references qjx_reward(r_jcbh);

表已更改。

SQL> alter table qjx_student add constraint fk_jxjbh foreign key(Jxjbh) references qjx_scholarship(s_jxjbh);

表已更改。

SQL> alter table qjx_grant drop column g_jxjbh;

表已更改。

SQL> alter table qjx_grant add(g_zxjbh number(4) primary key);

表已更改。

SQL> alter table qjx_student add constraint fk_zxjbh foreign key(Zxjbh) references qjx_grant(g_zxjbh);

表已更改。

SQL> alter table qjx_scholarlevel drop column l_jxjbh;

表已更改。

SQL> alter table qjx_scholarlevel add(l_jxjdjbh number(4) primary key);

表已更改。

SQL> alter table qjx_student add(Xybh number(4));

表已更改。

SQL> alter table qjx_student add(Xbh number(4));

表已更改。

SQL> alter table qjx_grantlevel drop column l_zxjbh;

表已更改。

SQL> alter table qjx_grantlevel add(l_zxjdjbh number(4) primary key);

表已更改。

SQL> alter table qjx_student add constraint fk_zxjdjbh foreign key(Zxjdjbh) references qjx_grantlevel(l_zxjdjbh);

表已更改。

SQL> desc qjx_scholarlevel;
 名称                                      是否为空? 类型
 ----------------------------------------- -------- ----------------------------
 L_JXJMC                                            VARCHAR2(4)
 L_LEVEL                                            VARCHAR2(4)
 L_JXJDJBH                                 NOT NULL NUMBER(4)

SQL> alter table qjx_student add constraint fk_jxjdjbh foreign key(Zxjdjbh) references qjx_scholarlevel(l_jxjdjbh);

表已更改。

SQL> alter table qjx_student add constraint fk_xybh foreign key(Xybh) references qjx_college(c_xybh);

表已更改。

SQL> alter table qjx_student add constraint fk_xbh foreign key(Xbh) references qjx_system(s_xbh);

表已更改。

SQL> spool off;

SQL> drop table qjx_grantlevel cascade constraints;

表已删除。

SQL> alter table qjx_student drop column Zxjdjbh;

表已更改。

SQL> drop table qjx_scholarship cascade constraints;

表已删除。

SQL> alter table qjx_student drop column Zxjbh;

表已更改。

SQL> drop table qjx_grant cascade constraints;

表已删除。

SQL> alter table qjx_student drop column Jxjbh;

表已更改。
SQL> create table qjx_jxj(
  2  jxjbh number(4) primary key,
  3  jxjmc varchar(6)
  4  );

表已创建。

SQL> alter table qjx_student add(jxjbh number(4));

表已更改。

alter table qjx_student add constraint fk_jxjbh foreign key(jxjbh) references qjx_jxj(jxjbh);

表已更改。

SQL> create table qjx_zxj(
  2  zxjbh number(4) primary key,
  3  zxjmc varchar(6)
  4  );

表已创建。

SQL> alter table qjx_student add(zxjbh number(4));

表已更改。

SQL> alter table qjx_student add constraint fk_zxjbh foreign key(zxjbh) references qjx_zxj(zxjbh);

表已更改。

SQL> create table qjx_jxjdjbh(
  2  jxjdjbh number(4) primary key,
  3  jxjdjmc varchar(6)
  4  );

表已创建。

SQL> alter table qjx_student add(jxjdjbh number(4));

表已更改。
 alter table qjx_student add constraint fk_jxjdjbh foreign key(jxjdjbh) references qjx_jxjdjbh(jxjdjbh);

表已更改。
SQL> create table zxjdjbh(
  2  zxjdjbh number(4) primary key,
  3  zxjdjmc varchar(6)
  4  );

表已创建。

SQL> alter table qjx_student add(zxjdjbh number(4));

表已更改。

SQL> alter table qjx_student add constraint fk_zxjdjbh foreign key(zxjdjbh) references qjx_zxjdjbh(zxjdjbh);

表已更改。

表信息插入代码:

SQL> insert into qjx_class values(001,'1班');

已创建 1 行。

SQL> insert into qjx_class values(002,'2班');

已创建 1 行。

SQL> insert into qjx_class values(003,'3班');

已创建 1 行。

SQL> insert into qjx_gender values(01,'男');

已创建 1 行。

SQL> insert into qjx_gender values(02,'女');

已创建 1 行。

SQL> delete from qjx_class where c_bjbh=003;

已删除 1 行。

SQL> delete from qjx_class where c_bjmc='3班';

已删除0行。

SQL> insert into qjx_nation values(031,'汉族');

已创建 1 行。

SQL> insert into qjx_nation values(032,'回族');

已创建 1 行。

SQL> insert into qjx_outlook values(041,'党员');

已创建 1 行。

SQL> insert into qjx_outlook values(042,'党员');

已创建 1 行。

SQL> insert into qjx_score values(051,89);

已创建 1 行。

SQL> insert into qjx_score values(052,95);

已创建 1 行。

SQL> insert into qjx_course values(011,'高数');

已创建 1 行。

SQL> insert into qjx_course values(012,'线代');

已创建 1 行。

SQL> insert into qjx_degree values(021,'本科');

已创建 1 行。

SQL> insert into qjx_degree values(022,'硕士');

已创建 1 行。

SQL> insert into qjx_reward values(081,'奖');

已创建 1 行。

SQL> insert into qjx_reward values(082,'罚');

已创建 1 行。

SQL> insert into qjx_scholarship values(101,300);

已创建 1 行。

SQL> insert into qjx_scholarship values(102,800);

已创建 1 行。

SQL> insert into qjx_grant values(091,5000);

已创建 1 行。

SQL> insert into qjx_grant values(092,8000);

已创建 1 行。

SQL> insert into qjx_scholarlevel values(061,'慧宇',2);

已创建 1 行。

SQL> delete from qjx_scholarlevel where l_jxjdjbh=061;

已删除0行。

SQL> select * from qjx_scholarlevel;

L_JX L_LE  L_JXJDJBH                                                            
---- ---- ----------                                                            
61   慧宇          2                                                            

SQL> insert into qjx_scholarlevel values(062,'吴江',1);

已创建 1 行。

SQL> insert into qjx_grantlevel values(071,'奥东',1);

已创建 1 行。

SQL> insert into qjx_grantlevel values(072,'宸',2);

已创建 1 行。

SQL> insert into qjx_college values(111,'管院');

已创建 1 行。

SQL> insert into qjx_college values(112,'化院');

已创建 1 行。

SQL> insert into qjx_system values(121,'数学');

已创建 1 行。

SQL> insert into qjx_system values(122,'物理');

已创建 1 行。

SQL> desc qjx_student;
 名称                                      是否为空? 类型
 ----------------------------------------- -------- ----------------------------
 ID                                        NOT NULL NUMBER(12)
 NAME                                               VARCHAR2(12)
 XBBH                                               NUMBER(4)
 BJBH                                               NUMBER(4)
 KCBH                                               NUMBER(4)
 XWBH                                               NUMBER(4)
 MZBH                                               NUMBER(4)
 ZZMBBH                                             NUMBER(4)
 ZCJBH                                              NUMBER(4)
 JXJBH                                              NUMBER(4)
 ZXJBH                                              NUMBER(4)
 JCBH                                               NUMBER(4)
 ZXJDJBH                                            NUMBER(4)
 JXJJCBH                                            NUMBER(4)
 XYBH                                               NUMBER(4)
 XBH                                                NUMBER(4)

SQL> select * from qjx_scholarlevel;

L_JX L_LE  L_JXJDJBH                                                            
---- ---- ----------                                                            
61   慧宇          2                                                            
62   吴江          1                                                            

SQL> insert into qjx_student (id,name,xbbh) values(1,'张三',01);

已创建 1 行。

SQL> select * from qjx_gender;

    G_XBBH G_                                                                   
---------- --                                                                   
         1 男                                                                   
         2 女                                                                   

SQL> spool off;

SQL> insert into qjx_student (id,name,xbbh,bjbh,kcbh,xwbh,mzbh,zzmbbh,zcjbh,jcbh,xybh,xbh,jxjbh,zxjbh,zxjdjbh,jxjdjbh) values(1,'张三',01,001,011,021,031,041,051,081,111,121,061,071,091,101);

已创建 1 行。

SQL> insert into qjx_student (id,name,xbbh,bjbh,kcbh,xwbh,mzbh,zzmbbh,zcjbh,jcbh,xybh,xbh,jxjbh,zxjbh,zxjdjbh,jxjdjbh) values(2,'李四',02,002,012,022,032,042,052,082,112,122,062,072,092,102);

已创建 1 行。

触发器代码:

SQL> create or replace trigger l_cfq
  2  before insert or update or delete
  3  on qjx_class
  4  declare
  5  var_class varchar2(10);
  6  begin
  7  if inserting then
  8  var_class := '插入';
  9  elsif updating then
 10  var_class := '修改';
 11  elsif deleting then
 12  var_class := '删除';
 13  end if;
 14  insert into qjx_class
 15  values(var_class,sysdate);
 16  end l_cfq;
 17  /

触发器已创建

SQL> spool off;

存储过程代码:

SQL> create procedure pro_insertclasses is
  2  begin
  3  insert into qjx_class values(3,'3班');
  4  commit;
  5  dbms_output.put_line('插入新记录成功!');
  6  end pro_insertclasses;
  7  /

过程已创建。

SQL> create procedure pro_insertgenders is
  2  begin
  3  insert into qjx_gender values(3,'男');
  4  commit;
  5  dbms_output.put_line('插入新记录成功!');
  6  end pro_insertgenders;
  7  /

过程已创建。

SQL> create procedure pro_insertnations is
  2  begin
  3  insert into qjx_nation values(3,'蒙古族');
  4  commit;
  5  dbms_output.put_line('插入新记录成功!');
  6  end pro_insertnations;
  7  /

过程已创建。

SQL> create procedure pro_insertoutlooks is
  2  begin
  3  insert into qjx_outlook values(3,'党员');
  4  commit;
  5  dbms_output.put_line('插入新记录成功!');
  6  end pro_insertoutlooks;
  7  /

过程已创建。

SQL> spool off;

函数代码:

SQL> create or replace function get_avg_kc(c_kcbh number) return number is
  2  avg_kc number;
  3  begin
  4  select avg(c_kcbh) into avg_kc from qjx_course where c_kcbh=c_kcbh;
  5  return(round(avg_kc,2));
  6  exception
  7  when no_data_found then
  8  dbms_output.put_line('该课程不存在');
  9  return(0);
 10  end;
 11  /

函数已创建。

SQL> create or replace function get_avg_zcj(c_zcjbh number) return number is
  2  avg_zcj number;
  3  begin
  4  select avg(c_zcjbh) into avg_zcj from qjx_score where c_zcjbh=c_zcjbh;
  5  return(round(avg_zcj,2));
  6  exception
  7  when no_data_found then
  8  dbms_output.put_line('该成绩不存在');
  9  return(0);
 10  end;
 11  /

函数已创建。

视图代码:

SQL> conn/ as sysdba;
已连接。
SQL> grant connect, resource to Oracle;

授权成功。

SQL> grant create view to Oracle;

授权成功。


SQL> connect Oracle/qjx;
已连接。

SQL> create or replace view q_view as
  2  select c_bjbh,c_bjmc
  3  from qjx_class;

视图已创建。

SQL> spool off;

SQL> create or replace view j_view as
  2  select n_mzbh,n_nation
  3  from qjx_nation;

视图已创建。

SQL> create or replace view x_view as
  2  select s_zcjbh,s_score
  3  from qjx_score;

视图已创建。

SQL> create or replace view m_view as
  2  select c_kcbh,c_course
  3  from qjx_course;

视图已创建。

SQL> create or replace view h_view as
  2  select d_xwbh,d_degree
  3  from qjx_degree;

视图已创建。

索引代码:
SQL> create index qjx_gender_g_gender_index on qjx_gender(g_gender)
  2  pctfree 30
  3  tablespace users;

索引已创建。

SQL> create index qjx_nation_n_nation_index on qjx_nation(n_nation)
  2  pctfree 25
  3  tablespace users;

索引已创建。

序列代码:
SQL> create sequence class_xl
  2  maxvalue 4
  3  start with 3
  4  increment by 1
  5  cache 50;

序列已创建。

SQL> create sequence nation_xl
  2  maxvalue 2
  3  start with 3
  4  increment by 1
  5  cache 10;

序列已创建。

SQL> create sequence gender_xl
  2  maxvalue 4
  3  start with 3
  4  increment by 1
  5  cache 5;

序列已创建。

SQL> create sequence outlook_xl
  2  maxvalue 8
  3  start with 4
  4  increment by 2
  5  cache 20;

序列已创建。

游标代码:
SQL> declare
  2  cursor cur_nation (var_mz in varchar :='汉族')
  3  is select n_mzbh,n_nation
  4  from qjx_nation
  5  where n_mzbh=var_mz;
  6  type record_nation is record
  7  (
  8  var_n_mzbh qjx_nation.n_mzbh%type,
  9  var_n_nation qjx_nation.n_nation%type
 10  );
 11  nation_row  record_nation;
 12  begin
 13  open cur_nation('汉族');
 14  fetch cur_nation into nation_row;
 15  while cur_nation%found loop
 16  dbms_output.put_line(nation_row.var_n_mzbh||'民族编号'||nation_row.var_n_nation||'民族编号');
 17  fetch cur_nation into nation_row;
 18  end loop;
 19  close cur_nation;
 20  end;
 21  /

游标已创建。

SQL> declare
  2  cursor cur_class (var_bj in varchar :='1班')
  3  is select c_bjbh,c_bjmc
  4  from qjx_class
  5  where n_bjbh=var_bj;
  6  type record_class is record
  7  (
  8  var_c_bjbh qjx_class.c_bjbh%type,
  9  var_c_bjmc qjx_class.c_bjmc%type
 10  );
 11  class_row  record_class;
 12  begin
 13  open cur_class('1班');
 14  fetch cur_class into class_row;
 15  while cur_class%found loop
 16  dbms_output.put_line(class_row.var_c_bjbh||'班级编号'||class_row.var_c_bjmc||'班级编号');
 17  fetch cur_class into class_row;
 18  end loop;
 19  close cur_class;
 20  end;
 21  /

游标已创建。

 

标签:表已,into,sql,number,qjx,SQL,整理,相关,table
来源: https://www.cnblogs.com/sjzxs/p/16578849.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有