实训日记Day2(数据库篇)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CNZSWYMP/article/details/86018305

实训第二天

/*2019.1.7上午
1.删除表语句
2.删除数据语句
3.使用语句创建表
4.使用加入约束条件
  非空约束,唯一约束,主键约束,外键约束,检查约束
5.提交,回退,存点
*/

使用的编辑工具是 SQLDevelopment(Oracle数据库)

相关操作代码

建立一个teacher表,设tno(主键),tname,sex,age四列;又创建表student,创建学生表student,表中包含学生编号sno(主键),学生姓名sname,学生年龄age ,学生性别sex,默认为女,手机号tel,地址address。对以上两表进行相关操作。

--删除表
drop table teacher;
--删除数据语句
DELETE from TEACHER where tno=103;
DELETE from TEACHER where age=29;
--提交
commit;
delete from teacher;
--回退
rollback;
--存点
DELETE from TEACHER where tno=104;
SAVEPOINT s1;
DELETE from TEACHER where tno=105;
SAVEPOINT s2;
DELETE from teacher;
roolback;
rollback to s2;
--建表语句
CREATE table teacher(
  tno number(10),
  tname VARCHAR2(20),
  sex VARCHAR2(3) DEFAULT '男',
  age NUMBER(3)
);
--创建学生表student,表中包含学生编号sno,学生姓名sname,学生年龄age ,学生性别sex,默认为女,手机号tel,地址address。
create table student(
  sno NUMBER(10),
  sname VARCHAR2(20),
  age NUMBER(3),
  sex VARCHAR2(3) DEFAULT '女',
  tel NUMBER(11),
  address VARCHAR2(40)
);
drop table student;
--约束
--非空约束
create table student(
  sno NUMBER(10) not null,
  sname VARCHAR2(20) not null,
  age NUMBER(3),
  sex VARCHAR2(3) DEFAULT '女',
  tel NUMBER(11),
  address VARCHAR2(40)
);
--修改表
ALTER table student MODIFY(sno varchar2(10));
ALTER table student MODIFY(sex default('男'));
ALTER table student MODIFY(age default('18'));
ALTER table student MODIFY(classes not null);
--新增字段
alter table student add(classes varchar2(8));
alter table student add(IDcard varchar2(18) not null);
--删除字段
alter table student drop(IDcard);
--唯一约束 unique
CREATE table student(
  sno varchar2(10) UNIQUE not null,
  sname varchar2(20) constraint uq_sname unique,
  tel number(11) constraint uq_tel unique,
  address varchar2(50) constraint uq_address unique,
  --constraint 加了一个约束,名字叫做uq_address
  IDcard varchar2(18),
  sex varchar2(4),
  CONSTRAINT uq_card unique(IDcard)
);--推荐使用
drop table student;
alter table student MODIFY(sex CONSTRAINT uq_sex unique);
alter table student add(age NUMBER(3)CONSTRAINT uq_age unique);--不推荐使用
--主键约束 创建主键时自动唯一非空,oracle主键不一定自增 primary key
drop table student;
create table student(
  sno VARCHAR2(10) CONSTRAINT pk_sno PRIMARY KEY,
  sname varchar2(20)
);
--检查约束
create table student(
  sno VARCHAR2(20),
  sname varchar2(20),
  age NUMBER(3) CONSTRAINT ckk_age check(age between 18 and 70)
);
--外键约束
drop table student;
create table employee(
  eno number(4),
  ename VARCHAR2(20),
  deptno number(2) CONSTRAINT fk_deptno1 REFERENCES dept(deptno)
  --创建一个外键 关联到dept表中的deptno
);

/*
2019.1.7 下午
1.索引
2.查询语句
3.练习1
*/

--索引
--创建索引
--单列索引
create index idx_sname on student(sname);
--复合索引
create index idx_name_tel on student(sname,tel);
create index idx_name_tel_address on student(sname,tel,address);
/*
索引1.create index idx_sname on student(sname);
索引2.create index idx_name_tel on student(sname,tel);
索引3.create index idx_name_tel_address on student(sname,tel,address);
索引4.create index idx_tel on student(tel);
索引5.create index idx_tel_address_name on student(tel,address,sname);
当只查询sname时,触发索引1,索引2,索引3
当只查询tel是,触发索引4,索引5
当查询tel,sname时 触发索引2
当查询tel,sname,address,或查询sname,address,tel,或tel,address,sname时,索引3,索引5会被触发
当查询tel,address时,触发索引5
当查询sname,tel触发索引2,索引3
当查询address时,不触发索引
当查询sname,addrss时,不触发索引
*/
--简单的查询语句
select * from emp;
select empno,ename,sal,deptno from emp;
--条件筛选
select empno,ename,sal,deptno from emp where deptno != 10;
select empno,ename,sal,deptno from emp where deptno <> 20;  
select empno,ename,sal,deptno from emp where deptno != 10 and sal>1000;
--算术运算
select empno,ename,sal,sal*12 from emp;
select empno,ename,sal as 员工工资,sal*12+500+800+200*3+12*100 as 年薪 from emp;
--查询员工编号,姓名,工资,年薪。将sal更名为工资,将sal*12+500+800+200*3+12*100更名为年薪。

--联合查询
select ename from emp union select sex from teacher; 
--排序
select empno,ename,sal from emp order by sal;
select empno,ename,sal from emp order by sal asc;--asc表示升序排列,默认升序;desc表示降序排列
select empno,ename,sal from emp order by sal desc;
select empno,ename,sal from emp order by sal desc ,empno asc;
--模糊查询 _:可以替换一个字符;%可以代表任意长度字符(可为0)
select empno,ename,sal from emp where ename like '%A%';--从表中查询名字中有字母A的名字
select empno,ename,sal from emp where ename like 'A%';--查询以字母A开头的名字
select empno,ename,sal from emp where ename like '%S';--查询以字母S开头的名字
select empno,ename,sal from emp where ename like '__A__';--查询名字长度为五,A位于第三位的名字
select empno,ename,sal from emp where ename like '_L%';--查询第二位是L的名字
select empno,ename,sal from emp where ename like '%R_';--插叙倒数第二位是L的名字
--NULL值查询
select * from emp where comm is null;
insert into teacher(tno,sex) values('21','男');
select * from emp where comm is not null;

猜你喜欢

转载自blog.csdn.net/CNZSWYMP/article/details/86018305