您的位置:首页 > 数据库

39,数据库(01)

2013-10-24 23:50 155 查看
/*

达内学习 Oracle day38 2013-10-24

*/

oracle -sql (structed query language)

数据的存储

file

dbms(数据库管理系统) 管理的文件就是db

主流的数据库:FDB 关系型数据库(基于二维表的数据库) 有行有列组成的表

数据库管理的基本单位是表

RDBMS 关系型数据库管理系统

Oracle甲骨文 50%

版本9i,10g,11g

DB2 IBM (国际商务机器)20%

sqlserver microsoft 15%

开源数据库:

mysql sun(斯坦福大学网络实验室)甲骨文

嵌入式数据库

sqllite 文本型数据库

//==================================================

SQL 语言的分类

1 查询语句

select

2 数据定义语言 DDL

create table

drop table

alter table

3 数据操作语言 DML

insert

delete

update

4 事务控制语言 TCL

commit

rollback

savepoint

5 grant revoke

//---------------------------------------------------------------

远程登陆

telnet 192.168.0.23/26

openlab open123

window: telnet IP

ubuntu: 相同

登陆数据库 sqlplus openlab/open123 sqlplus openlab open123

//-----------

察看 一张表结构

SQL>desc 表名;

SQL> desc s_emp;

Name Null? Type

----------------------------------------- -------- ----------------------------

ID 编号 NOT NULL NUMBER(7)

LAST_NAME 姓氏 NOT NULL VARCHAR2(25)

FIRST_NAME 名 VARCHAR2(25)

USERID 员工编号 VARCHAR2(8)

START_DATE 入职日期 DATE

COMMENTS 备注 VARCHAR2(255)

MANAGER_ID 领导编号 NUMBER(7)

TITLE 职位 VARCHAR2(25)

DEPT_ID NUMBER(7)

SALARY 工资 NUMBER(11,2)

COMMISSION_PCT 提成 NUMBER(4,2)

name 就是表头中的字段

null? 字段能不能不填写

type 字段的类型

数字:number

字符串:varchar2

日期类型:date

//----------

基本概念:

选择:所有列被选择部分行被选中

投影:所有行被选中,部分列被选择

表连接:把所有表组织起来

select 语句

A,from

数据从哪里来

1,怎么从一张表查询一个字段

select 字段名 from 表;

查询 s_emp 表中的 salary;

select salary from s_emp;

2,怎么从一张表中查询多个字段

select 字段名,字段名…… from 表; //同级别的东西用逗号隔开

查询 s_emp 表中 名 和对应的 工资

select first_name,salary from s_emp;

3,查询一个表中所有字段

select * from 表;

4,sql中的数学运算 + - * /

要求把每个人的工资 加100以后显示

select salary,salary+100 from s_emp;

5,字段和表达式别名

select first_name name,salary*12 'yearsal' from s_emp;

字段 和表达式的别名。 字段或者表达式后面 起另一个名字(别名只能有一个并且别名处理成大写) 原样显示用单引号

6,sql中的字符串

在一串字符的两端 加上单引号

例如:'' 'a' 'hello'

字符串的拼接 ||

select last_name||'_'||first_name name ,salary*12 yearsal from s_emp;

col 字段名 for a+数字; 设置字段名最多显示多少个字符,超了折行

col name for a25

姓名之间加入单引号

select last_name||''''||first_name name from s_emp;

7,空值处理

重新计算年薪,要求年薪是 工资*12*(1+提成/100)

select salary*12,salary*12*(1+nvl(commission_pct,0)/100) byearsa from s_emp

任何值 和 NULL 做运算结果都是NULL

nvl(par1,par2)

par1 为NULL时返回par2的值,当par1的值不为NULL就返回par1的值

8,数据的排重显示

select distinct salary from s_emp ;

只管做什么,不管在怎么做。

select distinct salary,title from s_emp; //工资和职位都相同

B,where

限定表中数据返回,符合where条件的数据被选中,不符合where条件的数据被过滤掉

1,特殊的where条件

select id,salary from s_emp where 1=1;

2,数字型的数据的限制

列出工资大于1400的员工的id, first_name salary

select id,first_name,salary from s_emp where salary>1400;

select id,first_name,salary from s_emp where salary!=1400;

3,字符串类型的数据的判断

找出first_name 叫Mai的员工的id

select id from s_emp where first_name = 'Mai';

字符串值区分大小写,sql语句不区分大小写

4,常见的运算符

= != < > >= <=

5,sql提供的元算符

a,表达一个闭区间

[a,b]

where 字段 between a and b;

查询工资在 1500 到 2000 之间的id

select id from s_emp where salary between 1500 and 2000;

b,表达一个值 出现在一个列表中

where 字段 in(值1,值2,……);

查询部门编号是 50,31,32 的员工ID

select id, first_name,dept_id from s_emp where dept_id in(50,31,32);

改变括号内顺序对查询结果没有影响。但是可能对查询效率有影响。应该把概率高的放在前面。

c,模糊查询关键字。 关键字 like

where 字段 like '通配串'

数据库中代表0-n个任意字符的是 %,代表一个任意字符的是 _

查询s_emp中first_name 带a的

select id,first_name from s_emp where first_name like '%a%';

查询所有S_开头的表明

select table_name from user_tables where table_name like 'S\_%' escape '\';

'\' '后面的字符转义 ,只有下划线和%需要转义

d,如何判断一个字段的值是否是NULL值

where 字段 is NULL;

找出manager_id是null的人的id

select id from s_emp where manager_id is NULL;

6,逻辑条件运算符

and or not

对立面

between a and b -> not between a and b

in -> not in(注意NULL)

like -> not like

is null -> is not null

7,条件组合顺序

为了清楚加()

C,order by

数据排序

order by 位置在语句最后面

order by 排序标准 排序方式

升序(自然排序,字典排序) asc 降序(反自然,反字典) desc

按照工资排序 显示 id first_name salary

select id,first_name,salary form s_emp order by salary desc;

按照manmager_id 排序

显示id first_name,manager_id

select id,first_name,manager_id from s_emp order by manager_id;

NULL 在排序中当最大值处理

当按照第一个排序时,如果第一个字一样,按照第二排序字段。

select id,first_name,salary from s_emp order by salary asc,first_name desc;

D,单行函数

1 概念

单行函数:针对sql影响的每一行数据都做处理,每一行都会返回一个结果

sql语句影响多少行 就返回多少个结果

组函数:针对一组数据做处理,返回一个结果。无论sql语句影响多少行都返回一个结果。

2 举例 组函数 count

select count(id) from s_emp

where id>1;

select upper(first_name) from s_emp where id = 1;

3 为了测试单行函数 引入了一张表 dual

select * from dual;

4 处理字符串的单行函数

upper(par1) 转化大写

lower(par2) 转化小写

select lower('HELLO')from dual;

initcap(par1) 把每个单词的首字母变大写

select initcap('one world one dream') from dual;

length(par1) 求字符串的长度

select length('hello') from dual;

把s_emp表中所有first_name 和first_name的长度列出来

select first_name,length(first_name) from s_emp;

E,多表查询

F,组函数和分组

G,子查询
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐