码迷,mamicode.com
首页 > 数据库 > 详细

SQI学习---数据库的增删改查等

时间:2018-07-21 11:43:22      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:open   ice   服务器   unique   学习   服务   roo   trigger   into   

创建表[增删改查]

技术分享图片
  1 CREATE TABLE female_teachers
  2 (
  3  tid  number(5) not null,
  4  tname varchar2(11),
  5  title   char(2) 
  6  );
  7 
  8 CREATE TABLE T_SCHOOL
  9 (
 10  sid VARCHAR(10),
 11  hid VARCHAR(10),
 12  sname VARCHAR(10)
 13  );
 14 
 15 
 16 CREATE TABLE T_HEADER
 17 (
 18  hid VARCHAR(10),
 19  hname VARCHAR(10)
 20  );
 21 
 22 
 23 CREATE TABLE T_BANJI
 24 (
 25  bid VARCHAR(10),
 26  room VARCHAR(10)
 27  );
 28 
 29 
 30 CREATE TABLE T_STUDENT
 31 (
 32  sid VARCHAR(10),
 33  bid VARCHAR(10),
 34  sname VARCHAR(10)
 35  );
 36 
 37  
 38  CREATE TABLE emp
 39 (
 40  empno  number(5) not null,
 41   ename      VARCHAR(10),
 42    job        VARCHAR(9),
 43    hiredate     DATE,
 44     sal  number(5)
 45  );
 46 
 47 
 48 INSERT INTO emp (empno,ename,job,hiredate,sal) VALUES (6060,‘李兴华‘,‘经理‘,sysdate,2000.30) ;
 49 INSERT INTO emp (empno,ename,job,hiredate,sal) VALUES (7369,‘董鸣楠‘,‘销售‘,sysdate,1500.90) ;
 50 INSERT INTO emp (empno,ename,job,hiredate,sal) VALUES (8964,‘李祺‘,‘分析员‘,sysdate,3000) ;
 51 INSERT INTO emp (empno,ename,job,hiredate,sal) VALUES (7698,‘张惠‘,‘销售‘,sysdate,800) ;
 52 INSERT INTO emp (empno,ename,job,hiredate,sal) VALUES (7782,‘杨军‘,‘分析员‘,sysdate,2500) ;
 53 INSERT INTO emp (empno,ename,job,hiredate,sal) VALUES (7762,‘刘明‘,‘销售‘,sysdate,1000) ;
 54 INSERT INTO emp (empno,ename,job,hiredate,sal) VALUES (7839,‘王月‘,‘经理‘,sysdate,2500) ;
 55 
 56 
 57 
 58  CREATE TABLE teachers
 59 (
 60  tid  number(5) not null,
 61  tname varchar2(11),
 62  sex   char(2),
 63  score  number(5)
 64  );
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73  insert into teachers(tid,tname,sex,score) values(2,‘wer‘,‘M‘,101);
 74  insert into teachers(tid,tname,sex,score) values(3,‘qw‘,‘W‘,103);
 75  insert into teachers(tid,tname,sex,score) values(4,‘yuw‘,‘W‘,104);
 76  insert into teachers(tid,tname,sex,score) values(5,‘table‘,‘M‘,105);
 77  insert into teachers(tid,tname,sex,score) values(6,‘jkh‘,‘W‘,203);
 78  insert into teachers(tid,tname,sex,score) values(7,‘tr‘,‘W‘,204);
 79  
 80  
 81 create table student
 82 (
 83 sno char(7) primary key,
 84 sname varchar2(9),
 85 sex char(2),
 86 age int,
 87 dept varchar2(8)
 88 )
 89 
 90 
 91 
 92 declare
 93     cursor tea is
 94     select * from teachers;
 95     begin
 96     for i in tea
 97     loop
 98     if i.sex=‘M‘ then
 99     insert into male_teachers values(i.tid, i.tname,i.sex) ;
100     else
101    insert into female_teachers values(i.tid,i.tname,i.sex);
102    end if;
103    end loop;
104    end ;
105    /
106 
107 
108 
109 SQL>
110     set serveroutput on;
111     create or replace procedure tea_count
112     (in_sex in Teachers.sex%type)
113     as
114     out_num number;
115     begin
116     if in_sex=‘M‘ then
117      select count(sex) into out_num from teachers where sex=‘M‘;
118      dbms_output.put_line(‘Number of Male: ‘ || out_num);
119     else
120     select count(sex) into out_num from teachers where sex=‘W‘;
121     dbms_output.put_line(‘Number of FeMale: ‘ || out_num);
122    end if;
123    end tea_count;
124    /
125  
126   create index in_teachers on teachers(tid);      select tid from teachers;
127   create view v_teachers as select tid, tname, score from teachers;
128   select * from v_teachers;
129 
130   declare
131   create or replace trigger t_teachers
132     before insert or update of tid, tname on teachers;
133     for each row
134     when(new.tname=‘Jack‘)
135     begin
136     if sex=‘M‘ then
137     insert into male_teachers values(tid,tname, sex);
138     else
139     insert into female_teachers values(tid, tname, sex);
140    end if;
141    end t_teachers;
142    /
143   
144   
145   
146   
147   
148   
149   
150   
151   
152   
153   
154   
155   
156   
157   
158   
159   
160   
161   
162 //外键的设置需要父表中设置主键或者unique约束,父表中没有的数据子表不能插入数据
163 
164 
165 alter table null_emp add constraint nullemp_fk foreign  key(sno) references dep on delete casc
166 ade;
167 存储过程是流控制和SQL语句书写过程,经过便宜存储后在数据库服务器,调用即可。
168 触发器是一种特殊的存储过程
169 视图还有一个好处就是重命名
170 角色(集合了多种权限)是相关权限的命名集合,简化用户权限的管理。
171 create view v_stu(学号,姓名,性别,年龄)
172 as
173 select sno,sname,sex,age from sys.student;
174 
175 create public synonym stu for student; 创建同义词
View Code

 

创建表[含外键]

技术分享图片
 1 create table customers (
 2     customersId int identity(1,1)  primary key ,
 3     cname varchar(8) not null ,
 4     address varchar(50) ,
 5     city varchar(10) ,
 6     tel varchar(10) unique ,
 7     company varchar(50) ,
 8     birthday datetime , 
 9     type tinyint default 1 
10 );
11 
12 
13 
14 create table goods (
15     goodsid int constraint c1 primary key , 
16     goodsname varchar(20) not null , 
17     price money , 
18     description varchar(200) ,
19     storage  int ,
20     provider varchar(50) ,
21     status tinyint default(0) 
22 );
23 
24 
25 create table orders (
26     orderid int identity(1,1) constraint c2 primary key ,
27     goodsid int not null references goods(goodsid) on delete cascade ,
28     customerid int not null foreign key(customerid)
29        references customers (customersId) on delete no action ,
30     quantity int not null constraint c3 check(quantity >0) ,
31     ordersum money not null ,
32     orderdate datetime default(getdate())
33 
34 ) ;
View Code

 

SQI学习---数据库的增删改查等

标签:open   ice   服务器   unique   学习   服务   roo   trigger   into   

原文地址:https://www.cnblogs.com/ftl1012/p/sql.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!