Home Database Mysql Tutorial 【Oracle入门系列】第03章

【Oracle入门系列】第03章

Jun 07, 2016 pm 03:37 PM
c oracle getting Started create instruction operate Simple series

简单的表操作 指令 : 创建简单表 : create table student(name varchar2(20),age number(3)); 插入新记录 : insert into student values ('stone',24); 查看表结构 : desc student; 查询记录 : select * from student; 删除表 : drop table student;

简单的表操作
指令创建简单表createtable student(name varchar2(20), age number(3) );
             插入新记录insert into studentvalues('stone',24);
             查看表结构desc student;
             查询记录select * from student;
             删除表drop table student;
             删除表中的数据delete from studentwhere 条件;
说明SQLPlusWorksheet中用“--”作为单行注释标记。varchar2(20)标明可变长度的字符串,上限为20
             number(3)标明age为不包含小数点的3位的整数。如果只定义成number,则系统默认为10位的整数
             单引号引起来的字符是SQL语言中字符串常量的表示方式。SQL语言中的关键字大小写不敏感
             数据库开发人员喜欢使用大写关键字。应用程序开发人员多数使用小写的,以实现良好的可读性
             插入记录后,其实并没有在数据库中立即永久生效,这时可以使用commit;提交数据的更新操作

 

 

 

简单的Select语句
语法格式SELECT *|{字段名>,...} FROM 表名>;--字段间用逗号隔开。在逗号后面添加空格可以增强可读性
                     例如select * from emp;select empno, ename, sal from emp;
显示效果Sql*plus的默认显示:Date和character型数据左对齐,Numeric型数据右对齐,列标题默认显示为大写
                     iSql*plus的默认显示:列标题默认显示为大写,列标题缺省居中对齐

 

 

 

使用算术表达式
概述:在Select语句中,对NUMBER型数据可以使用算术运算符(+-*/)创建表达式
举例:select empno, ename, sal, sal*12 from emp;--查询emp中员工的编号姓名工资和年薪
             select empno, ename, sal, sal*12+1000 from emp;
             select empno, ename, sql, sal*(12+1000) from emp;--可以使用括号更改计算顺序

 

 

 

连接运算符
概述:即“||”可以把列与字符或其它表达式连接在一起,得到一个新的字符串,实现“合成”列的功能
举例:select ename || ' is a ' || job from emp;
             select empno, ename ||'''s annual salary is'|| sal*12 from emp;
说明:用一个单引号代表字符串常量的分隔标记。用两个单引号代表字符串常量中出现的一个单引号字符
             在查询的时候,也不需要转义符,直接用两个单引号代表字符串常量中出现的一个单引号即可

 

 

 

使用字段别名
概述:重命名查询结果中的字段,以增强可读性。如果别名中使用特殊字符或强制输出大小写时需使用双引号
格式SELECT 字段名>|表达式> [[AS]字段别名>],... FROM表名>;
举例:select empno, ename "Ename", sal*12 "年 薪" from emp;
             select empno as 员工编号, ename 员工姓名, sal*12 "年薪" from emp;
说明:as可以不加。别名可以用双引号引起来。如果别名中不包含特殊字符,双引号可以省略
             如果别名中包含了空格,如"年 薪",就必须加双引号,否则将被解析成两个成份,导致语法上出问题
             如果Ename不加双引号,那么执行后会显示为ENAME。只有给它加上双引号之后,才会遵重大小写来显示

 

 

 

空值
概述:空值是无效的,未指定的,未知的或不可预知的值。空值不等同于0空格
举例:select empno, ename, sal, comm from emp;--执行后某些记录的comm属性会显示一片空白,即空值
             insert into student(age) values(24);--插入数据时,如果没有指定name值,那么默认的name就是空值
补充:算术表达式中如果出现空值,则整个表达式结果为空
             连接表达式中出现的空值被当作一个空的(长度为零的)字符串处理
             如select ename, sal, comm, ename || ' - ' || comm, sal+comm from emp;
             在算术表达式中如果涉及到空值的话,可以使用相应的函数对可能存在空值的字段进行缺省值的设置
             经过判断之后,如果它是空值,则返回0,如果不是空值,则返回它真正的值,然后再进行算术运算

 

 

 

去除重复行
概述:缺省情况下,查询结果中显示所有符合条件的记录行,包括重复行。可以使用DISTINCT可以清除重复行
             DISTINCT的作用范围是后面所有字段的组合,即后面的多个字段的组合不出现重复就可以了
举例:select deptno from emp;--本意是查询部门编号的种类,显示的却是所有的编号,包括重复编号
             select distinct deptno from emp;--清除查询结果中重复的部门编号
             select distinct deptno, job from emp;--只要deptno和job组合后的结果不重复,那么就都显示输出
说明:通常把极为简单的数据处理,包括数据的查询、组合、过滤或转换类型等操作,均交给数据库进行处理
             比如分组统计、求和、求平均值、过滤空值、过滤重复值、数据开头和结尾的空格过滤、字符串转日期型等等
             而对于复杂些的业务逻辑,或者与商业逻辑相关的内容,建议在应用程序中处理。这样也就实现了松散的耦合

 

 

 

查询结果排序
概述:查询结果缺省按照记录的插入顺序进行排列。可以使用ORDER BY子句对查询结果进行排序
             排序方式包括升序(ASC缺省)和降序(DESC)两种。也可以使用字段别名排序或按多字段排序
举例:select empno, ename, sal from emp order by sal;--按sal进行升序排列,ASC可以省略不写
             select empno, ename, sal from emp order by sal desc;--按sal进行降序排列
             select empno, ename, sal*12 annsal from emp order by annsal;
             select deptno, empno, ename, sal from emp order by deptno, sal;
说明:多字段排序时,先按照deptno排序,如果在得到的排列结果中存在重复的deptno记录的话
             就将重复的deptno记录再按照sal排序。至于具体的排序方式,则需要对两个字段进行单独指定
             如deptno,sal表示二者均按升序排列。deptno,sal desc表示deptno按升序排列,sal按降序排列
             如deptno desc,sal表示deptno按降序排列,sal按升序排列。依此类推
             这种逻辑适合由数据库在查询阶段进行处理。在应用程序中通过代码来实现的话,就会困难一些

 

 

 

条件查询
说明:在查询语句中或查询条件中使用字符串或日期的时候,字符串和日期值要用单引号括起来
             字符串大小写敏感。日期值格式敏感,缺省的日期格式是DD-MON-RR
注意:凡涉及到字符串内容的比较,大小写都是敏感的
格式SELECT *|{[DISTINCT]字段名>|表达式>[别名>],...} FROM表名> [WHERE 查询条件>];
举例:select * from emp where ename='SMITH';--这里所要查询的SMITH严格区分大小写
             select * from emp where hiredate='02-4月-81';--系统会自动将符合约定格式的字符串转换成Date型
             select sysdata from dual;--以缺省的日期格式获取当前的系统时间
说明:表dual中只有一行一列,它本身并不保存系统时间,sysdate永远代表当前的系统时间
             通常在进行简单的查询或运算的时候,dual表起一个媒介的作用,使得形式上看起来像是查询
             dual的好处在于只有一行记录,它一般用在练习时测试某些指令。一般不在真实的开发使用它
             比如select 3+2 from dual;返回5。又如select 3+2 from emp;则会返回很多行的5
             它会对emp中每一行记录都查询,但查的不是真正保存的内容,而是逐行的计算一遍3+2
             再把得到的5作为一个唯一的结果,也是唯一的一个字段返回来,所以结果就是10多行的5
             对于Oracle数据库,即使是想让它做一个简单的常量表达式的运算,也要采用问答的形式
             即便此时所有的数据都在用户的手中,那也要装作是查询,给它一个指令,令其返回结果

 

 

 

关系运算符
符号BETWEEN...AND...:界于两值之间,包括边界,并且必须把小的数值放在前面
             IN(set):出现在集合中。判断是否存在某字段等于集合中的值的记录
             LIKE:模糊查询,也叫通配查询
             IS NULL:判断空值
示例:select * from emp where sal between 1600 and 2900;--查询工资界于1600和2900之间的员工的信息
             select * from emp where ename in('SMITH','KING','King');--返回姓名为set中的元素的值的记录
             select * from emp where comm is null;--返回所有comm字段的值为空的记录
             select * from emp where comm is not null;--返回非空的。注意0和空是不同的
注意:凡涉及到字符串内容的比较,大小写都是敏感的。即in()中的值需要与ename值完全匹配方可查询出记录
LIKE%代表零或多个字符。_代表一个字符。可以使用ESCAPE标识特殊符号
             select * from emp where ename like 'S%';--查询以S开头的姓名的记录。这里S仍然是大小写敏感的
             select * from emp where ename like '_A%';--查询第二个字母是A的姓名的记录
             select * from emp where ename like '%\_%' escape '\';--返回姓名存在下划线的记录
             这里的escape标明“\”起到转义符的作用。在真实的开发中,这种转义符用的并不多
             Oracle中的转义符可以随便定义,所以例句中的“\”也可以换成其它的符号,如“K
说明:用一个单引号代表字符串常量的分隔标记。用两个单引号代表字符串常量中出现的一个单引号字符
             在模糊查询的时候,也不需要转义符,直接用两个单引号代表字符串常量中出现的一个单引号即可

 

 

 

逻辑运算符
概述:优先级由NOTANDOR
举例:select * from emp where deptno=20 and sal>2000;--返回部门编号为20而且工资大于2000的记录
             select * from emp where deptno=20 or sal>2000;--返回部门编号为20的人或者工资大于2000的记录
             select * from emp where deptno not in(20,30,40);--返回部门编号没有出现在该集合中的人的信息

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Generate PPT with one click! Kimi: Let the 'PPT migrant workers' become popular first Generate PPT with one click! Kimi: Let the 'PPT migrant workers' become popular first Aug 01, 2024 pm 03:28 PM

Kimi: In just one sentence, in just ten seconds, a PPT will be ready. PPT is so annoying! To hold a meeting, you need to have a PPT; to write a weekly report, you need to have a PPT; to make an investment, you need to show a PPT; even when you accuse someone of cheating, you have to send a PPT. College is more like studying a PPT major. You watch PPT in class and do PPT after class. Perhaps, when Dennis Austin invented PPT 37 years ago, he did not expect that one day PPT would become so widespread. Talking about our hard experience of making PPT brings tears to our eyes. "It took three months to make a PPT of more than 20 pages, and I revised it dozens of times. I felt like vomiting when I saw the PPT." "At my peak, I did five PPTs a day, and even my breathing was PPT." If you have an impromptu meeting, you should do it

How long will Oracle database logs be kept? How long will Oracle database logs be kept? May 10, 2024 am 03:27 AM

The retention period of Oracle database logs depends on the log type and configuration, including: Redo logs: determined by the maximum size configured with the "LOG_ARCHIVE_DEST" parameter. Archived redo logs: Determined by the maximum size configured by the "DB_RECOVERY_FILE_DEST_SIZE" parameter. Online redo logs: not archived, lost when the database is restarted, and the retention period is consistent with the instance running time. Audit log: Configured by the "AUDIT_TRAIL" parameter, retained for 30 days by default.

All CVPR 2024 awards announced! Nearly 10,000 people attended the conference offline, and a Chinese researcher from Google won the best paper award All CVPR 2024 awards announced! Nearly 10,000 people attended the conference offline, and a Chinese researcher from Google won the best paper award Jun 20, 2024 pm 05:43 PM

In the early morning of June 20th, Beijing time, CVPR2024, the top international computer vision conference held in Seattle, officially announced the best paper and other awards. This year, a total of 10 papers won awards, including 2 best papers and 2 best student papers. In addition, there were 2 best paper nominations and 4 best student paper nominations. The top conference in the field of computer vision (CV) is CVPR, which attracts a large number of research institutions and universities every year. According to statistics, a total of 11,532 papers were submitted this year, and 2,719 were accepted, with an acceptance rate of 23.6%. According to Georgia Institute of Technology’s statistical analysis of CVPR2024 data, from the perspective of research topics, the largest number of papers is image and video synthesis and generation (Imageandvideosyn

From bare metal to a large model with 70 billion parameters, here is a tutorial and ready-to-use scripts From bare metal to a large model with 70 billion parameters, here is a tutorial and ready-to-use scripts Jul 24, 2024 pm 08:13 PM

We know that LLM is trained on large-scale computer clusters using massive data. This site has introduced many methods and technologies used to assist and improve the LLM training process. Today, what we want to share is an article that goes deep into the underlying technology and introduces how to turn a bunch of "bare metals" without even an operating system into a computer cluster for training LLM. This article comes from Imbue, an AI startup that strives to achieve general intelligence by understanding how machines think. Of course, turning a bunch of "bare metal" without an operating system into a computer cluster for training LLM is not an easy process, full of exploration and trial and error, but Imbue finally successfully trained an LLM with 70 billion parameters. and in the process accumulate

AI in use | AI created a life vlog of a girl living alone, which received tens of thousands of likes in 3 days AI in use | AI created a life vlog of a girl living alone, which received tens of thousands of likes in 3 days Aug 07, 2024 pm 10:53 PM

Editor of the Machine Power Report: Yang Wen The wave of artificial intelligence represented by large models and AIGC has been quietly changing the way we live and work, but most people still don’t know how to use it. Therefore, we have launched the "AI in Use" column to introduce in detail how to use AI through intuitive, interesting and concise artificial intelligence use cases and stimulate everyone's thinking. We also welcome readers to submit innovative, hands-on use cases. Video link: https://mp.weixin.qq.com/s/2hX_i7li3RqdE4u016yGhQ Recently, the life vlog of a girl living alone became popular on Xiaohongshu. An illustration-style animation, coupled with a few healing words, can be easily picked up in just a few days.

Counting down the 12 pain points of RAG, NVIDIA senior architect teaches solutions Counting down the 12 pain points of RAG, NVIDIA senior architect teaches solutions Jul 11, 2024 pm 01:53 PM

Retrieval-augmented generation (RAG) is a technique that uses retrieval to boost language models. Specifically, before a language model generates an answer, it retrieves relevant information from an extensive document database and then uses this information to guide the generation process. This technology can greatly improve the accuracy and relevance of content, effectively alleviate the problem of hallucinations, increase the speed of knowledge update, and enhance the traceability of content generation. RAG is undoubtedly one of the most exciting areas of artificial intelligence research. For more details about RAG, please refer to the column article on this site "What are the new developments in RAG, which specializes in making up for the shortcomings of large models?" This review explains it clearly." But RAG is not perfect, and users often encounter some "pain points" when using it. Recently, NVIDIA’s advanced generative AI solution

Oracle database server hardware configuration requirements Oracle database server hardware configuration requirements May 10, 2024 am 04:00 AM

Oracle database server hardware configuration requirements: Processor: multi-core, with a main frequency of at least 2.5 GHz. For large databases, 32 cores or more are recommended. Memory: At least 8GB for small databases, 16-64GB for medium sizes, up to 512GB or more for large databases or heavy workloads. Storage: SSD or NVMe disks, RAID arrays for redundancy and performance. Network: High-speed network (10GbE or higher), dedicated network card, low-latency network. Others: Stable power supply, redundant components, compatible operating system and software, heat dissipation and cooling system.

How much memory does oracle require? How much memory does oracle require? May 10, 2024 am 04:12 AM

The amount of memory required by Oracle depends on database size, activity level, and required performance level: for storing data buffers, index buffers, executing SQL statements, and managing the data dictionary cache. The exact amount is affected by database size, activity level, and required performance level. Best practices include setting the appropriate SGA size, sizing SGA components, using AMM, and monitoring memory usage.

See all articles