《小布老师SQL学习笔记》由会员分享,可在线阅读,更多相关《小布老师SQL学习笔记(34页珍藏版)》请在金锄头文库上搜索。
1、Liao SQL 基础介绍 Oracle 11g SQL Fundamentals Training Introduction 1、primary key主键表中的唯一的标示 非空2 foreign key外键 在本表中引用的另外一张表的主键3、ER实体关系模型4、SQL的分类4.1、Data manipulation language(DML)数据操作语言Select查询数据Insert插入数据Update更新数据Delete删除数据Merge合并数据4.2、Data definition language(DDL)数据定义语言Create创建表Alter修改表Drop删除表Rename重命
2、名表Truncate 截取Comment4.3、Data control language(DCL)数据控制语言GrantRevoke取消4.4、Transaction control 事物控制Commit提交Rollback 回滚SavepointSQL show user -查看当前用户USER is SYSdesc dba_users -查看所有用户SQL set lines 200SQL select username,account_status from dba_users;USERNAMEACCOUNT_STATUSPERFSTATOPENHROPENOEOPENSHOPENOP
3、S$ORACLEUSER01DIPIXMDDATAPMBIOPENOPENEXPIRED & LOCKEDEXPIRED & LOCKEDEXPIRED & LOCKEDEXPIRED & LOCKEDEXPIRED & LOCKEDSQL select table_name from user_tables;-查看存在的表-字符串和日期型是左对齐,数字型是右对齐,表头是大写。SQL select * from departments;DEPARTMENT_ID DEPARTMENT_NAMEMANAGERLOCATIONIDID10 Administration20 Marketing30
4、Purchasing20017002011800114170001-Restricting Data Using the SQL SELECT Statement1、算数表达式+ - * /可以用于除FROM子句的其他子句SQL select last_namefsalary,salary + 300 from employees;LAST_NAMESALARY SALARY+300Walsh45331.6845631.68Feeney45190.5645490.56107 rows selected.-算数表达式中有NULL值结果为NULL-和字符串做运算时,结果为字符串。2、别名用双引号括
5、起来(不能用于WHERE子句)select last_name as Name,commission_pct as Comm from employees;NameCommOzer.25Bloom.2Fox.2Order by中可以使用别名和指定序号3、链接|将两个字段连起来显示-两个字段中插入空格select last_name | | job_id as Employees from employees;EmployeesOConnell SH_CLERKGrant SH_CLERKWhalen AD_ASSTselect last_name | is a |job_id as Emplo
6、yees from employees;EmployeesOConnell is a SH_CLERKGrant is a SH_CLERK;Whalen is a AD_ASSTq可以输出”单引号(实验证明应该是输出一段自定义字符)SQL select, ename | | qT * s j ob 7 | | j ob r om. emp;ENAME | |,SCTQB * | |SMITH1B j ob CLERK3QL select ename | |q T s j oh T | |j ob f匚口m emp;3NAMEI |QT T T SJOE T | I JOE3MITHr T s
7、 j ob CLERK4、distinct查不同的值(范围是整个SELECT后的所有字段)select distinct department_id from employees;02-Restricting and Sorting Data补充 where 子句:1)子句内的需要比较的字符需要用单引号引起来(比如where xxx=123),而且字符是区分 大小写的。2)子句如果比较的是日期字符(默认的格式是日-月-年),需要注意日期格式。3)运算符Comparison OperatorsOperatorMeaningEqual toGreater than=Greater than or
8、equal toLess than=Less than or qual toNot equal to (!=竺)BETWEEN.AND.Between Iwo valuer(indusiveTN(set)Match any of a list af valuesLIKE /Matth a character patternIS HULLIs a null value例子:Between and:select emp_name,sal where sal between 2500 adn 3000;Select emp_name,sal where emp_name where emp_name
9、 between tony adn suen字符也可以比较,A-Z,这样比较Like通配符%代表0或者多个字符代表1个字符Select emp_name from emp where empname like 张4) IS NULL / IS NOT NULL 字段是否为空WHERE子句里判断5) 逻辑运算符AND OR NOT1、Escape字符转义SQL select last_name,job_id from employees2 where job_id like %SA _% escape ; escape 来定义转义字符,也可以是|或者其他的-escape 是将斜线设置为转义字符,
10、把下划线转义成了一个普通的字符LAST_NAMERussellPartnersErrazurizSA_MANSA_MANSA_MAN2、SQL执行优先级Rules of PrecedenceOperatorMeaning1Arithmetic operators2Concatenation operator3Comparison conditions4IS NOT NULL, LIKE、NOT IN5IJOT EETWEEfJ6Not equal lo7not logical ccnditian8and logical condition9or logical conditionSELECT
11、last_nam& job_idF salary FROM employeesWHERE job_id = 1SA_REP1ORANDI= 1 ADORES 1-1L salary 15000; LAT_NAUE & JOBJDHi SAL4RY |1 KF1QD-PRES2+:OD2 MeiSA_REP1100D3 Taylor2A_REPOECD4 OflrtSA_REP7000SELECT last_nameF job_idj salaryFROM employeesWHER_ijob_id =r SA_REP rORj ob_i d =rAD_PRES r )ANDsalary 150
12、00; LAS7_NAME1 JOBJD |u)SALARY |1 Kng24COO补充:排序ORDERBY默认是升序,DESC是降序。ORDER BY子句里可以使用别名,WHERE ,group by , having子句不能使用Order by N后面可以自己加数字,代表SELECT第N个字段,但不值得推荐。Order by后面可以跟多个字段,表示先按照第一个字段先排序,然后再第一个字 段中相同的值中,第二例再排序。如果有NULL值,升序是放到最后,降序是放到开始。但也可以自己改变:SCJL SELECT last-narie, departiTient_id , salary2 FROM
13、 employees3 ORDER BY dEpartnent_i_d, salary DESC NULLS FIRST ;表的别名:另外:1、form子句的别名和国际标准不一样:不能加AS,其他子句可以。2、表的别名设置后,其他地方使用表就只能用别名。列的别名则不用这样SQL SELECT, lagt_H3tne f &, depaptm&Ht-Hsme2 FROM ernp loyees e, departrients 目、3 LJHZRE e.cepartnient_id = d.deparftnienr_id;Where子句不能包含多组函数,而且3、替换变量&和&简单的字符串的替换;几
14、乎可以适用所有子句 是客户端软件的功能SQL select last_name,salary from employees2 where employee_id=&employee_num;如是字符串和日期一定要加单引 号一Enter value for employee_num: 123 old 2: where employee_id=&employee_num new 2: where employee_id=123LAST_NAMESALARYVollman50161.44双&替换变量后,同一个变量,只用输入一次。4、Define定义变量(适合用在批处理里面)SQL define employee=200SQL select employee_id,las