Home Database Mysql Tutorial Oracle数据库对表的基本操作和一些常用函数

Oracle数据库对表的基本操作和一些常用函数

Jun 07, 2016 pm 04:42 PM
oracle function

首先,创建一个员工表,sql语句如下: CREATE TABLE employee( id NUMBER(4), name VARCHAR2(20) NOT NULL, gender CHAR(1) D

首先,创建一个员工表,sql语句如下:

CREATE TABLE employee(
  id NUMBER(4),
  name VARCHAR2(20) NOT NULL,
  gender CHAR(1) DEFAULT 'M',
  birth DATE,
  salary NUMBER(6,2),
  comn NUMBER(6,2),
  job VARCHAR2(30),
  manager NUMBER(4),
  deptno NUMBER(2)
)

下面,是对这个表的一些基本操作:


RENAME employee TO emp  //将表名改成emp

ALTER TABLE employee ADD hiredate DATE DEFAULT sysdate  //为employee增加列,默认值为当前日期

ALTER TABLE employee MODIFY (job VARCHAR2(40) DEFAULT 'CLERK')  //修改job列的长度为40,并设置默认值

ALTER TABLE employee DROP (manager)  //删除manager列

INSERT INTO employee (id,name,job,salary) VALUES(1001,'jack','PROGRAMMER',5500)  //向表中插入数据

INSERT INTO employee (id,name,job,birth) VALUES(1002,'LARRY','ANALYST',TO_DATE('1994-10-22','YYYY-MM-DD'))  //使用自定义日期格式插入记录,默认格式是'22-SEP-94'

UPDATE employee SET salary=10 WHERE  //更新LARRY的薪水,注意如果插入数据的时候字母是大写,,那么修改的时候也要大写

DELETE FROM employee WHERE birth is null  //删除生日为null的记录

CHAR最大取值为2000字节,它可以不指定长度,默认为1,VARCHAR2最大取值为4000字节,它必须指定长度。

字符串函数:

CONCAT和||:返回两个字符串连接后的结果。select CONCAT('a','b') from dual  result:ab    select 'lo'||'ve' from dual  result:love

LENGTH:返回字符串的长度,如果是VARCHAR2返回字符串的实际长度,如果字符类型是CHAR,长度还要包括后补的空格。

UPPER:把字符转换为大写形式。

LOWER:把字符转换为小写形式。

INTCAP:把每个单词的首字母大写,单词之间用空格和非字母字符分隔。

TRIM:SELECT TRIM('e' FROM 'elite') FROM dual  //从elite的前后截去'e',如果不写e,就去掉两端的空格。

LTRIM:SELECT LTRIM('elite','e') FROM dual  //从elite的左面截去'e',如果不写e,就去掉左端的空格。

RTRIM:SELECT LTRIM('elite','e') FROM dual  //从elite的右面截去'e',如果不写e,就去掉右端的空格。

LPAD:SELECT LPAD('12',5,'$#') from dual  //把'12'用'$#'在左面补满5位。

RPAD:SELECT LPAD('12',5,'$#') from dual  //把'12'用'$#'在右面补满5位。

SUBSTR:SELECT SUBSTR('abcdefg',-2,2) from dual  //第二个参数等于0或1都是从首字符开始取,如果第二个参数为负数,那么'g'为-1,'f'为-2,依此类推,然后向右取第三个参数长度的字符。如果没有设置第三个参数或者长度超过了字符的长度,则取到字符串末尾为止。

SELECT INSTR('thinking in java','i',4,2) from dual  //返回'i'在第一个参数中的位置,第三个参数的含义是从第四个字符('n')开始检索,2表示的含义是'i'第二次出现的位置。

空值函数:

NVL(expr1, expr2):如果expr1为NULL,则取值expr2。

NVL2(expr1, expr2, expr3):NVL2用来判断expr1是否为NULL,如果不是NULL,返回expr2,如果是NULL,返回expr3。

 


日期转换函数:

 


TO_DATE:将字符串按照定制格式转换为日期类型。for example:TO_DATE('2002-01-01','YYYY-MM-DD')

TO_CHAR:将其它类型(日期,数值)的数据转换为字符类型,主要应用在日期类型上。for example:TO_CHAR(hiredate,'YYYY"年"MM"月"DD"日"')

常用的日期格式如下图:

Oracle数据库对表的基本操作和一些常用函数

Oracle数据库对表的基本操作和一些常用函数

日期常用函数:

LAST——DAY(date):返回给定date所在月的最后一天。

ADD_MONTHS(date,i):返回给定date加上i个月后的日期值,如果i是负数,则获得减去i个月后的日期值。

MONTH_BETWEEN(date1, date2):计算date1-date2两个日期值之间间隔了多少个月,如果date2时间比date1晚,会得到负值。除非两个日期间隔是整数月,否则会得到带小数位的结果,比如计算2009年9月1日到2009年10月10日之间间隔多少个月,会得到1.29个月。

NEXT_DAY(date, char):返回给定date日期数据的下一个周几,周几是由参数char来决定的。在中文环境下,直接使用”星期三”这种形式,英文环境下,需要使用”WEDNESDAY”这种英文的周几。为避免麻烦,可以直接用数字1-7表示周日-周六。

GREATEST(expr1[, expr2[, expr3]]…)    LEAST(expr1[, expr2[, expr3]]…)  两个函数都可以有多个参数值,但参数类型必须一致,返回结果是参数列表中最大或最小的值,在比较之前,在参数列表中第二个以后的参数会被隐含的转换为第一个参数的数据类型,所以如果可以转换,则继续比较,如果不能转换将会报错。

在CentOS 6.4下安装Oracle 11gR2(x64)

Oracle 11gR2 在VMWare虚拟机中安装步骤

Debian 下 安装 Oracle 11g XE R2

Oracle Linux 6.5安装Oracle 11.2.0.4 x64

本文永久更新链接地址:

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

See all articles