oracle 基本查询语句及实例

Posted 猫九丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle 基本查询语句及实例相关的知识,希望对你有一定的参考价值。

1、查询所有列

select * from 表名;

2、查询表结构

     desc 表名;

3、查询指定列

select ename,sal,job from 表名;

4、racle中查看所有表和字段

获取表:

select table_name from user_tables; //当前用户的表       

select table_name from all_tables; //所有用户的表   

select table_name from dba_tables; //包括系统表

select table_name from dba_tables where owner=‘用户名‘

user_tables:

table_name,tablespace_name,last_analyzed等

dba_tables:

ower,table_name,tablespace_name,last_analyzed等

all_tables:

ower,table_name,tablespace_name,last_analyzed等

all_objects:

ower,object_name,subobject_name,object_id,created,last_ddl_time,timestamp,status等

获取表字段:

select * from user_tab_columns where Table_Name=‘用户表‘;

select * from all_tab_columns where Table_Name=‘用户表‘;

select * from dba_tab_columns where Table_Name=‘用户表‘;

user_tab_columns:

table_name,column_name,data_type,data_length,data_precision,data_scale,nullable,column_id等

all_tab_columns :

ower,table_name,column_name,data_type,data_length,data_precision,data_scale,nullable,column_id等

dba_tab_columns:

ower,table_name,column_name,data_type,data_length,data_precision,data_scale,nullable,column_id等

获取表注释:

select * from user_tab_comments

user_tab_comments:table_name,table_type,comments

相应的还有dba_tab_comments,all_tab_comments,

这两个比user_tab_comments多了ower列。

获取字段注释:

select * from user_col_comments

user_col_comments:table_name,column_name,comments

相应的还有dba_col_comments,all_col_comments,

这两个比user_col_comments多了ower列。

5、锁定oracle用户及解除锁定

     alter user scott account lock

     alter user scott account unlock

6、where字句

    select * from 表名 where 字段>数值;

    select * from 表明 where to_char(字段,‘yyyy-mm-dd‘)>‘1982-1-1‘;    to_char转换函数

    select * from 表明 where to_char(字段,‘yyyy‘)=‘1980‘;

    select * from 表明 where to_char(字段,‘mm‘)=‘4‘;

    显示工资在2000到2500工资

 select * from 表名 where 字段>=2000 and 字段<=2500;

 select * from 表明 where 字段 between 2000 and 2500;

7、模糊查询 like

      %:表示任意0到多个字符  ;  _ : 表示任意单个字符

如何显示首字母为S的员工姓名及工资

      select eaname, sal from 表名 where eaname like ‘S%‘ ;

如何显示第三个字母为O的所有员工姓名及工资

      select eaname, sal from 表名 where eaname like ‘__O%‘;

8、where语句使用 in

      如何显示empno 为 123,345,678的雇员情况

     1、select * from  表明 where empno=123 or empno=345 or empno=678;

           select * from 表明 where empno in (123,345,678);

      2、is null 空值查询

          select * from 表明 where 字段名 is null ;

       3、oracle逻辑运算符

           查询工资高于500或是岗位为MSN的雇员,同时还要满足他们的姓名首字母大学J

          select * from 表明 where (sal>500 or job=‘MSN‘) and (enname like ‘J%‘ );

drop table student;drop table course;drop table score;drop table teacher;
CREATE TABLE STUDENT(SNO VARCHAR(3) NOT NULL, SNAME VARCHAR(4) NOT NULL,SSEX VARCHAR(2) NOT NULL, SBIRTHDAY DATE,CLASS NUMBER NOT NULL);
CREATE TABLE COURSE(CNO VARCHAR(5) NOT NULL, CNAME VARCHAR(10) NOT NULL, TNO VARCHAR(10) NOT NULL);
CREATE TABLE SCORE (SNO VARCHAR(3) NOT NULL, CNO VARCHAR(5) NOT NULL, DEGREE NUMBER NOT NULL);
CREATE TABLE TEACHER (TNO VARCHAR(3) NOT NULL, TNAME VARCHAR(4) NOT NULL, TSEX VARCHAR(2) NOT NULL, TBIRTHDAY DATE NOT NULL, PROF VARCHAR(6), DEPART VARCHAR(10) NOT NULL);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,‘曾华‘ ,‘男‘ ,to_date(‘1977-09-01‘,‘yyyy-mm-dd‘),95033);INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,‘匡明‘ ,‘男‘ ,to_date(‘1975-10-02‘,‘yyyy-mm-dd‘),95031);INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,‘王丽‘ ,‘女‘ ,to_date(‘1976-01-23‘,‘yyyy-mm-dd‘),95033);INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,‘李军‘ ,‘男‘ ,to_date(‘1976-02-20‘,‘yyyy-mm-dd‘),95033);INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,‘王芳‘ ,‘女‘ ,to_date(‘1975-02-10‘,‘yyyy-mm-dd‘),95031);INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,‘陆君‘ ,‘男‘ ,to_date(‘1974-06-03‘,‘yyyy-mm-dd‘),95031);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘3-105‘ ,‘计算机导论‘,825);INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘3-245‘ ,‘操作系统‘ ,804);INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘6-166‘ ,‘数据电路‘ ,856);INSERT INTO COURSE(CNO,CNAME,TNO)VALUES (‘9-888‘ ,‘高等数学‘ ,100);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,‘3-245‘,86);INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,‘3-245‘,75);INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,‘3-245‘,68);INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,‘3-105‘,92);INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,‘3-105‘,88);INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,‘3-105‘,76);INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,‘3-105‘,64);INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,‘3-105‘,91);INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,‘3-105‘,78);INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,‘6-166‘,85);INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,‘6-106‘,79);INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,‘6-166‘,81);
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,‘李诚‘,‘男‘,to_date(‘1958-12-02‘,‘yyyy-mm-dd‘),‘副教授‘,‘计算机系‘);INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,‘张旭‘,‘男‘,to_date(‘1969-03-12‘,‘yyyy-mm-dd‘),‘讲师‘,‘电子工程系‘);INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,‘王萍‘,‘女‘,to_date(‘1972-05-05‘,‘yyyy-mm-dd‘),‘助教‘,‘计算机系‘);INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,‘刘冰‘,‘女‘,to_date(‘1977-08-14‘,‘yyyy-mm-dd‘),‘助教‘,‘电子工程系‘);

 









以上是关于oracle 基本查询语句及实例的主要内容,如果未能解决你的问题,请参考以下文章

oracle查询日期字段大于某个日期值时sql语句怎么写

查询Oracle正在执行的sql语句及执行该语句的用户

oracle查询语句中in后面数据较多怎么优化

oracle基本查询

Oracle数据库基本查询语句

oracle——单表查询