面试必知的SQL数据库命令

 

 

1、登录数据库

普通用户登陆sqlplus 用户名/密码sqlplus scot/ti以管理员身份登陆sqlplus  /  as  sysdbasqlplus sys/sys as sysdba修改用户密码alter user scot identified by 密码

2、select语句注意事项 

select col1, col2…from table_name where condition group by col…having condtion order by col…、SELECT  *|{[DISTINCT] column|expression [alias],...}  from table;

例子:

查询所有员工的所有记录select * from emp;select empno, ename, job,sal, comm, deptno from emp;注:尽量使用列名,用列名代替

别名的使用:

查询员工号,姓名,月薪,年薪select empno as "员工号", ename "姓名", sal 月薪, sal*12 年薪 from emp;

 1、SQL 语言大小写不敏感; 2、 SQL可以写在一行或者多行

3、关键字不能被缩写也不能分行;4、各子句一般要分行写。

3、NULL值

NULL不等于NULLselect * from emp where NULL=NULL; 查不到任何记录滤空函数:nvl(a, b)  如果a为NULL,返回b;

在SQL中, 判断一值是否等于NULL不用“=” 和“!=”而使用is和is not

查询奖金为NULL的员工信息select * from emp where comm is NULL;查询奖金不为NULL的员工信息select * from emp where comm is not NULL;

4、过滤和排序数据

查询薪水不等于10000的员工信息select * from emp where sal != 10000;between…and:介于两值之间,包含两边的值查询工资在10-15之间的员工select * from emp where sal >=10 and sal<=15;select * from emp where sal between 10 and 15;  in:在集合中, not in 不在集合中例子:
查询部门号为10和20的员工信息select * from emp where deptno=10 or deptno=20; select * from emp where deptno in (10, 20);  查询部门号不是10和20的员工select * from emp where deptno not in (10, 20);

模糊查询

like:模糊查询%匹配任意多个字符,  _匹配一个字符
例子:查询名字以S开头的员工select * from emp where ename like 'S% '; 

注意:SQL在解析where的时候,是从右至左解析的。故,and应该将易假的值放在右侧; or时应该将易真的值放在右侧。

order by 排序与分组函数

order by 子句排序ASC(ascend): 升序DESC(descend): 降序例:查询员工信息, 按月薪排序select * from emp order by sal;      从小到大排序,默认select * from emp order by sal desc; 从大到小排序
按照工资进行排序--使用序号进行排序的情况select ename, sal, sal*12, from emp order by 3 desc;按照select后面列名出现的先后顺序, ename→1, sal→2, sal*12→3
按照部门和工资进行排序select * from emp order by deptno, sal;desc只作用于最近的一列, 两列都要降序排, 则需要两个descselect * from emp order by deptno desc, sal desc;

分组函数作用于一组数据,并对一组数据返回一个值。分组数据使用group by关键字,按照group by 后给定的表达式,将from后面的table进行分组。

查询“部门”的平均工资select deptno, avg(sal) from emp group by deptno;
查询部门内部不同职位的平均工资select deptno, job, avg(sal) from emp group by deptno, job order by 1;在select列表中所有没有包含在组函数中的列, 都必须在group by的后面出现。

  Having 过滤分组:1、行已经被过滤;2、使用了组函数

语法:SELECT column, group_function FROM table [WHERE  condition] [GROUP BY      group_by_expression][HAVINGgroup_condition] [ORDER BY column];
查询平均薪水大于1000的部门select deptno, avg(sal) from emp group by deptno having avg(sal)>1000;

注:where和having都是将满足条件的结果进行过滤。但是差别是where子句中不能使用组函数。

求1号部门的平均工资select deptno, avg(sal) from emp group by deptno having deptno=1;、或者使用whereselect deptno, avg(sal) from emp where deptno=1 group by deptno;  笛卡尔集:笛卡尔集的行数 = table1的行数 x table2的行数;笛卡尔集的列数 = table1的列数 + table2的列数。查询员工信息:员工号 姓名 月薪和部门名称select e.empno, e.ename, e.sal, d.dname  from emp e, dept d  where e.deptno=d.deptno;按部门统计员工人数,显示:部门号 部门名称 人数select d.deptno 部门号, d.dname 部门名称, count(e.empno) 人数 from emp e, dept d where e.deptno=d.deptno group by d.deptno, d.dname;

5、CURD

使用 INSERT 语句向表中插入数据。其语法为:INSERT INTO  table [(column [, column...])]VALUES (value [, value...]);注:values的值覆盖了所有列,则table的列可以省略如果:插入的时候没有插入所有的列, 就必须显式的写出这些列的名字(注意:字符串和日期都应该使用 ' '号引用起来)insert into emp(empno, ename, sal, deptno) values(10, 'chales', 55000, 20);一次性将emp表中的指定列插入到表emp10中 insert into emp10(empno, ename, sal, deptno) select empno, ename, sal, deptno from emp where deptno=10;
创建表create table test1 (tid number, tname varchar2(20), hiredate date default sysdate);
create table emp1 as select * from emp where 1=2;“where 1=2”一定为假。所以是不能select到结果的,但是将这条子查询放到Create语句中,可以完成拷贝表结构的效果。

     约束:

1.  Not Null    非空约束2.  Unique    唯一性约束3.  Primary Key  主键约束 主键约束隐含Not null + Unique4.  Foreign Key  外键约束 

     update更新数据

格式: update 表名 set col=值 where condtionupdate emp set sal=5000, comm=500 where ename = 'charles';

   delete删除数据

格式: delete from 表名 where condtiondelete from emp where empno=07;
delete 和 truncate的区别1.  delete 逐条删除表“内容”,truncate 先摧毁表再重建。  2.  delete 是DML语句,truncate 是DDL语句。DML语句可以闪回(flashback),DDL语句不可以闪回。3.  由于delete是逐条操作数据,所以delete会产生碎片,truncate不会产生碎片。4.  delete不会释放空间,truncate 会释放空间用delete删除一张10M的表,空间不会释放。而truncate会。所以当确定表不再使用,应truncate5.  delete可以回滚rollback, truncate不可以回滚rollback。

另外,经常使用的数据库对象有表、视图、索引、序列、同义词等,博主后续也将对其进行详细解释,特别索引这一功能。如果觉得文章还不错的话,请多多关注。

 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值