1.oracle数据库启动的服务(OracleOraDb10g_home1TNSListener,OracleServiceORCL)
2.用客户端sqldeveloper.exe连接指定数据库的方式
cmd中输入sqlplus进行本机数据库的方式
3.数据库中表和字段的概念
4. sql语句的分类
a) DDL,数据定义语言 create table drop table等
b) *DML,数据操纵语言 insert update delete
c) *DQL,数据查询语言 select
d) DCL,数据控制语言 dba(数据库管理员)
5.ddl语句:建或者删除表,索引等数据库对象
createtable tablename (column1 type,column2 type,column3 type....)
droptable tablename;
6.DML,数据操纵语言增删改(insert ,delete ,update)
insertinto tablename(column1,column2....) values()
deletefrom tablename where 条件
updatetablename set column1=value1,column2=value2 where 条件
DML语句要执行commit的操作才能生效
7.DQL 数据查询语句
select要查询的字段列表 fromtablename
要查询的字段列表:* 代表查询该表中所有字段或者是要查询的字段的列表格式为(column1,column2,column3....)
1.虚表:dual
dual表是系统提供的一张虚表,这张表中只有一条记录,查询此表时也只会返回一条记录如果我们需要测试某个表达式或函数,测试的结果又与具体的表无关就可以使用dual这张 表。
1.1.sysdate在oracle中用来表示当前的系统时间, Mysql中为NOW()
Select now() from dual;
select sysdate from dual;
1.2.select 12*13 from dual;(表达式)
2.空值:null:函数nvl(val1,val2)
用来处理空值,这个函数有两个参数,第一个参数为字段名或表达式,如果这个参数的值为null,那么就返回第二个参数的值,否则返回第一个参数的值
nvl(字段名,0):将空值的字段设值为0;尽量避免空置的出现
3.where语句
3.1.条件比较
=,!=,<>,<,>,<=,>=,any,some,all
is null,is not null
between x and y
exsits(sub-query)
in(list),not in(list)
like _ ,%,escape ‘\‘ _\% escape ‘\’ not like
3.2.逻辑复合条件
not,and,or
判断字符串是否相等时需要将字符串用单引号引起来
单引号中的字符串内容是要区分大小写的
between .. and .. 的时候,包含了最大和最小值(>= and <=)
4.别名
4.1.字段名 as “别名”
4.2.字段名“别名”
4.3.字段名别名
当别名中间有空格时必须用双引号引起来
5.Like
‘%’表示零个或多个字符;
‘_’表示一个字符;
查询名字中含有‘%’使用转义字符 escape ‘\’;
SELECT sal, ename from emp where ename like'%a\%%' ESCAPE'\';
6.order by 是出现在select语句之后的,所以可以用别名,where和其他语句中不能用别名
7.character:
lower 小写
upper 大写
concat 连接两个字符串(而||可连接多个字段)
select concat(concat(empno, ename), sal)from emp;
select empno||ename||sal from emp;
length 字符串长度
substr 提取字符串的一部分substr(ename,1,2)
Chr与某个ASCII码数值所对应的字符
Ascii求一个字符的ASCII码数值
round(number,n)
组合函数不能出现在where里面,只能在select里
Rownum支持<=不支持>=
R排序,定义rownum别名,在子查询
sqlplus sys/o123 as sysdba;
Grant create table,create view to scott;
视图:
1.查询变得简单2,使数据库更加安全3,给系统维护带来困难
Create view viewName as (select a,b fromtableName);
Drop view viewName;
索引:查询远远大于增删改的次数时
1.加快对数据的查询速度2,索引太多,会降低对插入、删除、修改数据的速度
Create index indexName ontableName(colName1,colName2,...)
Drop index indexName;
序列:
Create sequence seq_tableName_Id(建议每张表建一个序列) increateBy n start withn
Insert into tableName (id,name) values(seq_tableName_Id.nextval,'name');
seq_tableName_Id.nextval和seq_tableName_Id.currval(下个序列和当前序列)
Drop sequence seqName;
表结构:
Create table tableName (id number(10,2))
Drop table tableName
Alter table tableName add(colNamecolType,...)/modify (colName colType,...)
Drop (colName,colName,...)/rename (colume colName1 to colName2)
Rename oldName to newName;
表中数据:
Delete from tableName where ...
Insert into tableName (id,name,...) values(1,'limin',...)
Insert into tableName (id,name,...) select * from tableName2
Insett into tableName select * fromtableName2
Update tableName set colsName = 'value'
Update tableName set (cols1,cols2,...) =(select cols1,cols2...from table where ...)
Sql查询语句:
Distinct :用distinct关键字来消除结果集中的重复信息,当distinct用来修饰多个字段的时候,指的是需要消除后面所有字段的组合重复结果。除去重复的组(select distinct name,passwd from tableName)将name,passwd同时重复的组除去
转换函数
to_char
to_number
to_date
to_char(date|number[,’fmt’])
To_char(sysdate,'YYYY-MM-DD')
to_number(char)
to_date(char[,’fmt’])
To_date('1982-12-12','YYYY-MM-DD')
to_char数字格式
select to_char(sal,'999,999') fromemp;------------1600--->1,600.000
select to_char(sal,'000,000') fromemp;------------1600--->001,600.000
select to_char(sal,'$999,999') fromemp;------------1600--->$1,600.000
select to_char(sal,'L999,999') fromemp;------------1600--->¥1,600.000
to_char日期格式
Group by
在使用group by 时,有一个规则需要遵守,即出现在select列表中的字段,如果没有在组函数中,那么必须出现在group by 子句中。
sql查询总结
首先执行where语句将原有记录过滤;
第二执行group by 进行分组;
第三执行having过滤分组;
然后将select 中的字段值选出来;
最后执行order by 进行排序;
(所以在select中定义字段别名时,select前面的不可用,select后面的可以用)
小结:sql语句select的语法
selectcolumn1,column2,..... from tablename where 过滤条件(and or) group by column having 分组的限制条件
orderby column(最后进行排序)
注意:where过滤条件中只允许使用普通函数 不可以使用组函数 但是having分组限制条件中可以使用组函数
Rownum:
一个伪字段存在,这个字段的名字叫做rownum,
用来标识每条记录的行号,行号从1开始,每次递增1,
当rownum和order by 一起使用时,会首先选出符合rownum条件的记录,然后再排序
数据库的分页:
select* from (select rownum r,a.* from (?) a) where r between ? and ?
第一个问号:指的是要查询的sql语句(包含排序)
第二个问号:指的是分页查询的起始行号
第三个问号:指的是分页查询的结束行号
oracle分页利用的rownum伪字段
mysql的分页利用的limit关键字
sqlserver的分页利用的top
Rowid:
删除掉student表中重复的记录
思路:(1)先查询出表中重复的记录
(2)将查询的重复记录的条件作为删除数据的条件之一(删除的就是这些重复的记录 但是要保留一个)
(3)如何去区分出这些重复的记录中保留的唯一的一条记录(利用rowid[取出rowid的最小值或者最大值])
(4)删除的另外一个条件是取出的rowid最小值的那条重复的记录不能删除
delete from student
where (username,passwd) in(
select username,passwd from student
group by username,passwd
having count(*)>1)
and rowid not in(
select min(rowid) from student
group by username,passwd
having count(*)>1)