SQLdesc表名SQLdescs_emp脚本sql.txtsql.sql1.上传脚本2.运行脚本@绝对路径/文件名@文件名(关注语言环境)selectuserenv('lang')fromdual;USERENV('LANG')----------------USUSERENV('LANG')----------------ZHSdesc一张表NameNull?Type------------------------------------------------------------------ID员工idNOTNULLNUMBER(7)LAST_NAME姓NOTNULLVARCHAR2(25)FIRST_NAME名VARCHAR2(25)USERID员工描述VARCHAR2(8)START_DATE入职日期DATECOMMENTS备注VARCHAR2(255)MANAGER_ID领导的员工idNUMBER(7)TITLE职位VARCHAR2(25)DEPT_ID部门idNUMBER(7)SALARY月薪NUMBER(11,2)COMMISSION_PCT提成NUMBER(4,2)sql语句的分类:数据检索select数据操作insertdeleteupdate数据定义createdropalter事务控制commitrollbacksavepoint数据控制grantrevoke选择投影连接查询语句:Afrom子句1.查询出任意一个字段select字段名from表名;selectsalaryfroms_emp;2.查询多个字段(用逗号分割)selectfirst_name,salaryfroms_emp;3.*号可以替代所有的字段select*froms_emp;4.表头的原样显示----使用双引号selectfirst_name,salarysalfroms_emp;selectfirst_name,salarysalfroms_emp;可以让别名中包含空格selectfirst_name,salaryemPsalfroms_emp;5.字段的数学运算求每个人的年薪selectsalary*12yearsalfroms_emp;换一种年薪计算方式考虑提成selectsalary*12*(1+commission_pct/100)yearsalfroms_emp;selectsalary*12*(1+commission_pct/100)yearsalfroms_emp;nvl空值处理函数nvl(值/字段,如果是空要得到的值)第一个参数是NULL就返回第二个参数如果第一个参数不是NULL返回第一个参数selectnvl(salary*12*(1+commission_pct/100),0)yearsalfroms_emp;//logicerror空值要尽早处理空值和任何值做运算都是NULLselectsalary*12*(1+nvl(commission_pct,0)/100)yearsalfroms_emp;6.想把姓名显示出来selectfirst_name,last_namefroms_emp;字符串连接||selectfirst_name||last_namefroms_emp;字符串的表达'hello''_'''''selectfirst_name||'_'||last_namefroms_emp;this'sselectfirst_name||''''||last_namefroms_emp;7.排重显示---distinctselectsalaryfroms_emp;selectdistinctsalaryfroms_emp;补充:联合排重selectdistinctsalary,idfroms_emp;clearscreen!clearB条件子句wherewhere字段表达式表达式找出工资大于1400的first_name,salaryselectfirst_name,salaryfroms_empwhere1=1;//全部显示selectfirst_name,salaryfroms_empwhere1=2;//无显示selectfirst_name,salaryfroms_empwheresalary1400;where条件限制行的返回符合条件返回不符合过滤掉字段表达式中可以使用的比较运算符=一个等号判断相等找出first_name,manager_id是Carmen的工资selectfirst_name,salary,manager_idfroms_empwherefirst_name='Carmen';数字类型可以直接用等号判断相等字符串不要忘记单引号oraclesql大小写不敏感单字符串的值大小写敏感==!=^=betweenandnotbetweenandinnotinlikenotlikeisnullisnotnulloracle(sql)也提供了一些运算符字段betweenaandb表达一个闭区间[a,b]工资salary在[800,1400]selectfirst_name,salaryfroms_empwheresalarybetween800and1400;字段in(list)list用逗号隔开的一组值查询一下部门号在414250人first_name,salaryselectfirst_name,salary,dept_idfroms_empwheredept_idin(41,42,50);顺序对最终结果没有影响但对效率可能产生影响字段isnull判断一个字段是不是NULLid是1的人manager_id是不是NULLselectid,first_namefroms_empwheremanager_idisnull;like-----模糊查询成龙配套成龙李小龙龙飞凤舞first_name中带a字符的统配符:%代表n个任意字符_一个任意字符'%a%''%a''a%''_a%''%a_'selectfirst_namefroms_empwherefirst_namelike'%a%';找出第二个字符是aselectfirst_namefroms_empwherefirst_namelike'_a%';user_tables----数据字典表,存储了数据库中所有表的信息descuser_tablesTABLE_NAME表名selecttable_namefromuser_tables;s_emps_depts找出所有s_开头的表名selecttable_namefromuser_tableswheretable_namelike's_%';user_tables数据字典表----默认处理成大写selecttable_namefromuser_tableswheretable_namelike'S_%';selecttable_namefromuser_tableswheretable_namelike'S__%';//logicerror转义selecttable_namefromuser_tableswheretable_namelike'S\_%'escape'\';selecttable_namefromuser_tableswheretable_namelike'S'||'_'||'%';//logic逻辑连接符号andor!工资salary在[800,1400]selectfirst_name,salaryfroms_empwheresalarybetween800and1400;selectfirst_name,salaryfroms_empwheresalary=800andsalary=1400;工资salary在(800,1400)selectfirst_name,salaryfroms_empwheresalary800andsalary1400;字段in(list)list用逗号隔开的一组值查询一下部门号在414250人first_name,salaryselectfirst_name,salary,dept_idfroms_empwheredept_idin(41,42,50);selectfirst_name,salary,dept_idfroms_empwheredept_id=41ordept_id=42ordept_id=50;非关系===!=^=betweenandnotbetweenandinnotinlikenotlikeisnullisnotnullmanager_id不是NULL的first_name,salary,manager_idselectfirst_name,salary,manager_idfroms_empwheremanager_idisnotnull;xml----domsaxui条件可以用小括号改变优先级别selectfirst_namefroms_empwheredept_id=41ordept_id=42andsalary1000;selectfirst_namefroms_empwhere(dept_id=41ordept_id=42)andsalary1000;如果不确定你的逻辑可以通过小括号改变逻辑优先selectsalary*12+100froms_emp;select(salary+100)*12froms_emp;C排序排序的种类:升序字典顺序自然顺序asc默认顺序降序desc按照工资排序显示first_name,salaryorderby排序字段一定出现在sql语句最后selectfirst_name,salaryfroms_emporderbysalary;selectfirst_name,salaryfroms_emporderbysalaryasc;降序selectfirst_name,salaryfroms_emporderbysalarydesc;补充:第一排序字段第二排序排序按照工资排序显示first_name,salary工资相同按first_name降序selectfirst_name,salaryfroms_empwhere1=1orderbysalarydesc,first_namedesc;D单行函数单行函数:对一行操作之后得到一个结果组函数:对一组数据数据处理之后得到一个结果upperselectfirst_name,upper(first_name)froms_emp;countselectcount(id)froms_emp;s_emp表中数据特别多对单行函数的测试不是很方便测试表dual单行单列descdualselect*fromdual;测试字符串的函数upperlowerselectlower('ONEDREAMONEWORLD')fromdual;initcapselectinitcap('ONEDREAMONEWORLD')fromdual;lengthselectlength('ONE')fromdual;edit-----进入vi编辑界面x删除字符dd删除一行aio命令下ZZ保存退出nvl()selectnvl(NULL,'isnull')fromdual;selectnvl('','isnull')fromdual;selectnvl('','isnull')fromdual;nvl要求可以处理任何类型但两个参数的类型必须一致substr(字段/值,开始的位置,截取多长)注意开始的位置是从1开始也可以负数负数从后往前编号从-1开始编号selectsubstr('hello',1,3)fromdual;selectsubstr('hello',0,3)fromdual;selectsubstr('hello',2,4)fromdual;从后编号selectsubstr('hello',-2,2)fromdu