Day 01
Oracle
SQL = Structured Query Language = 结构化查询语言
DDL = Data Difinition Language = 数据定义语言
create 创建 alter 修改 drop 删除 truncate 截断
DML = Data Manipulation Language = 数据操纵语言
insert 新增 delete 删除 update 更新
DQL = Data Query Language = 数据查询语言
select 查询
DCL = Data Control Language = 数据控制语言
grant 授权 revoke 取消授权
TCL = Transaction Control Language = 事务控制语言
commit 提交 rollback 回滚 savepoint 保存点
sqlplus = 让我们和数据库进行沟通的工具
regedit = 注册表
control = 控制面板
services.msc = 服务
用户:system scott
用户操作语句: | |
---|---|
切换用户: | conn 用户名/密码; conn system/etoak; |
给用户解锁: | alter user scott account unlock; |
给用户加锁: | alter user scott account lock; |
给用户授权: | grant dba to scott; |
给用户取消授权: | revoke dba from scott; |
创建新用户: | create user ET2404 identified by Etoak; |
给用户修改密码: | alter user scott identified by etoak; |
删除用户: | drop user ET2404; |
查看当前用户: | show user; |
练习:
在scott用户下新建用户ET,在ET用户下新建用户ET2404,连接ET2404,删除ET2404
注意:上述代码在Oracle头脑里除了密码保存大小写,其他字符(包括用户名'et2404')统统转化为大写。
Oracle的数据类型:
字符型:
varchar2(20) 可变类型 0-4000字节
char(6) 固定类型 0-2000字节
数值型:
number(5)
number(5,1)
日期型:
date
timestamp 包含毫秒数
show user; 查看数据库中所有用户;
edit; 快速对上行代码在.txt文档中编辑;
创建表:
create table student(
id number(5),
name varchar2(20),
birthday date,
sal number(5,1)
);
练习:
新建teacher表,字段有老师工号,老师姓名,老师入职时间,老师年薪(整数位五位以上,小数位两位)
表结构操作语句: | |
---|---|
查看表结构: | desc student; |
建表以后,修改表名: | alter table student rename to stu; |
建表以后,新增字段: | alter table student add email varchar2(20); |
建表以后,修改列名: | alter table student rename column email to youxiang; |
建表以后,删除字段: | alter table student drop column youxiang; |
建表以后,修改数据类型: | alter table student modify name varchar2(25); |
建表以后,删除表: | drop table student2; |
建表以后,截断表: | truncate table student; |
查看当前用户有哪些表: | select table_name from user_tables; |
练习:
给teacher表改名为tea,新增字段hobby,将hobby修改为aihao,删除aihao,截断表,删除表
增删改查:
增删改查: | |
---|---|
新增数据:insert | insert into student values(1,'张三',sysdate,5000); insert into student(id,name,sal) values(2,'李四',6000); |
删除数据:delete | delete from student; delete from student where id = 1; |
更新数据:update | update student set sal = sal + 500; update student set sal = sal + 500,name = 'zsf' where id = 1; |
查询数据:select | select * from student; select name,sal from student; select name,sal from student where name = '张三'; select name,sal from student where name = '张三' and id = 1; |
条件:
and 并且,两个条件必须都成立
or 或者,有一个条件成立即可
between and 闭合区间
select name,sal from student where sal between 5000 and 5500;
select name,sal from student where sal >= 5000 and sal <= 5500;
运算:
+ - * /
比较:
> < >= <= !=
<> 不等于
like:模糊查询
%:代表任意位的任意字符
_:代表一位上的任意字符
select name from student where name like 'ET%';
select name from student where name like '%s%';
select name from student where name like 'ET_%';
not like:
select name from student where name not like '%s%';
escape:逃离符
通过指定一个字符位进行逃离,来保证like之后的特殊字符看作是普通字符
select name from student where name like 'ET,_' escape ',';
select name from student where name like 'ET._.%' escape '.';
别名:
select name,sal * 1.3 as salary from student;
select s.name,s.sal * 1.3 as salary from student s;
分组:group by
将一张表中某个列或者多个列上相同的值划分为一个组,那么这张表就被分为多个组
*:根据字段A分组,那么只能查询字段A,其他字段需要以组函数的形式出现
select deptno from emp group by deptno;
select deptno,job from emp group by deptno,job;
select deptno,count(ename) num from emp group by deptno;
having:条件
select deptno,count(ename) num
from emp
group by deptno
having count(ename) >= 5;
distinct:去重
支持单列,多列的去重
select distinct deptno from emp;
select distinct deptno,job from emp;
排序:order by
升序:asc 降序:desc标签:name,sal,数据库,day15,student,m2,deptno,where,select From: https://www.cnblogs.com/zhaodenghui33/p/18259625
select deptno,sal from emp order by deptno;
select deptno,sal from emp order by deptno asc;
select deptno,sal from emp order by deptno asc,sal desc;