oracle单表查询简单语句

来源:互联网 发布:快手特效软件 编辑:程序博客网 时间:2024/05/17 03:21
常用操作:


1)远程连接
>sqlplus scott/tiger@ip[:port]/sid [as sysdba]
port:1521
sid:orcl或者oracle


2)系统管理员sys登录:
>sqlplus / as sysdba
>sqlplus sys/wepull as sysdba


>select * from scott.emp;(访问另一个用户的表)
> select table_name from all_tables;
(表或视图的名字叫all_tables,记录了所有用户的所有的表
(tablename叫做字段)
> select table_name from all_tables where owner='SCOTT';
(用单引号引起来的是常量区,大小写敏感!!!!!!!!)
> select table_name,tablespace_name from all_tables where owner='SCOTT';
(查看在哪个表空间里面)
> desc all_tables;
(查看一下alltables所有的表结构或字段)


>select username from dba_users;
>alter user scott identified by ww;(修改密码)
>alter user scott account unlock;(为用户解锁)
>alter user scott account locked;(为用户加锁)


> select tablespace_name from user_tablespaces;(查看表空间)
TABLESPACE_NAME
----------------
SYSTEM
UNDOTBS1
SYSAUX
TEMP
USERS
EXAMPLE


>create tablespace wpctest_ts datafile 'E:\wpce1106\wp.dbf' size 10M;(创建表空间)
>create temporary tablespace wpctest_temp tempfile 'E:\wpce1106\wp_temp.dbf' size 5M;(创建临时表空间)
(创建新用户并关联到已创建的表空间和临时表空间)
表空间和临时表空间的区别:
正规的数据都放在表空间里;
临时表空间只是一个临时周转的场所。一些操作产生的垃圾数据就放在临时表空间里。
>create user wpce1106 identified by wpce1106 default tablespace wpctest_ts temporary tablespace wpctest_temp;
>create user wpce1106 identified by wpce1106;
(不指定则默认存储在USERS,临时的存储在TEMP)
>grant CREATE SESSION to wpce1106;(为新用户授权,新用户才可用)
>revoke create session from wpce1106;(回滚,撤销权限)


>exit


3)在非管理员用户下(scott或者wpce1106新用户下)
>sqlplus wpce1106/wpce1106;
>sqlplus scott/tiger;


>set line 100 ;设置每行多少字节
>set pagesize 50 ;设置每页多少条记录
>set line 100 pagesize 50;
>set heading/feedback/echo/time


>connect sys/wepull as sysdba(切换账户,系统管理员)
>connect scott/tiger;(切换账户,scott)


>show user;显示身份


>select table_name from user_tables;(查看有多少张表)
>select * from dept;(查表的内容)
>select * from emp;
>desc 查看表的结构


> create table test1(test varchar2(20));
> insert into test1 values('wepull');
> drop table test1;(删除表)
(不能完全删除,默认进回收站了)


4)在scott下作的一些小练习
找出员工编号是偶数的员工((没有%) %用mod(a,b)代替):
> select * from emp where mod(empno,2)=0;


找出所有没有津贴的员工的姓名:
> select ename from emp where comm is null;
找出所有有津贴的员工的姓名:
> select ename from emp where comm is not null;




*****************   like   ******************
找出名字中含有J的员工信息:
> select * from emp where ename like '%J%';
找出第二个字母为A的职工:
> select * from emp where ename like '_A%';
(_代表匹配单个字符,%代表匹配任意字符)


*****************   嵌套查询   ******************
找出比SCOTT薪水更高的员工的姓名:
> select ename from emp where sal>(select sal from emp where ename='SCOTT');


*****************   distinct   ******************
distinct的作用范围是后面查询的所有字段!!!!!!!!!
找出不是重复的薪水:
> select distinct sal from emp;(12条记录)
> select distinct sal,ename from emp;(14条记录)
 
*****************   group by   ******************
groop by 前只能与集合函数以及后面带的字段连用!!!!!!!!!
集合函数:
max min avg sum count


查看每个部门有多少人:
> select count(*),deptno from emp group by deptno;


*****************   having   ******************
找出部门员工人数大于4人的部门以及员工人数:
> select count(*),deptno from emp group by deptno having count(*)>4;




找出薪水>2000所在的部门,并且部门人数超过1个的部门:
> select deptno,count(*)  from emp where sal>2000 group by deptno having count(*)>1;


*****************  where,goup by,having的区别   ******************
顺序:where—>group by—>having!!!!!!!!
goup by 只能和集合函数以及后面所带的字段连用!!!!!
having后面可以带集合函数!!!!!!




*****************   where,group by,having,order by   ******************
排序:
order by columnName [asc/desc] 默认是asc


找出没有有津贴的部门以及对应的人数,且人数超过两个的部门,并且按部门人数从大到小的顺序排序
> select deptno,count(*) from emp where comm is null group by deptno having count(*)>2 order by deptno desc;




where vs having??
1.两个都是过滤,但是where是独立的,having必须与group by连用!
2,如果分组过滤同时存在的时候,过滤在分组的前面,组跟组之间的过滤用having
即wehere用在分组前,having用在分组后

















原创粉丝点击