Common SQL syntax and Data Objects in ORACLE

Source: Internet
Author: User

1. INSERT (INSERT the record statement into the data table)

Insert into Table Name (field name 1, field name 2 ,......) VALUES (value 1, value 2 ,......);
Insert into Table Name (field name 1, field name 2 ,......) SELECT (field name 1, field name 2 ,......) FROM another table name;

Field Values of the string type must be enclosed in single quotes, for example, 'good Day'
If the field value contains single quotes and requires String Conversion, we replace it with two single quotes ''. an error occurs when the value of a string type field exceeds the defined length. It is best to verify the length before insertion.

The field value of the date field can be converted to the date function TO_DATE ('2017-08-01 ', 'yyyy-MM-DD') with the system time SYSDATE of the current database or with a string ') TO_DATE () has many date formats. For more information, see oracle doc. -Month-day hour: minute: seconds format YYYY-MM-DD HH24: MI: SS

The maximum length of a string that can be operated during INSERT is less than or equal to 4000 single bytes. If you want to INSERT a longer string, use the CLOB type for the field to use the DBMS_LOB package that comes with ORACLE.

If you want to use the serial number that automatically increases from 1 during INSERT, you should first CREATE a name WITH the serial number create sequence serial number (preferably table name + Serial number mark) increment by 1 start with 1
MAXVALUE 99999 cycle nocache;
The maximum value is determined by the length of the field. If the defined auto-increment serial NUMBER (6), the maximum value is 999999.
INSERT statement INSERT this field value: name of the serial number. NEXTVAL

2. DELETE (DELETE the statements recorded in the data table)

Delete from table name WHERE condition;

Note: deleting a record does not release the occupied data block tablespace in ORACLE. It only marks the deleted data blocks as unused.

To delete all records in a large table, run the TRUNCATE command to release the occupied data block tablespace.
Truncate table name;
This operation cannot be rolled back.

3. UPDATE (modify the statements recorded in the data table)

UPDATE table name SET field name 1 = value 1, field name 2 = value 2 ,...... WHERE condition;

If the modified value N is not assigned a value or defined, the original record content will be cleared to NULL. It is best to perform non-NULL verification before modification. If the value N exceeds the defined length, an error will occur, it is best to verify the length before inserting ..

Note:
A. the preceding SQL statement adds row-level locks to the table. After confirmation, the transaction processing command COMMIT must be added to take effect. Otherwise, changes may not be written to the database. to recall these operations, run the ROLLBACK command to restore them.

B. before Running INSERT, DELETE, and UPDATE statements, it is best to estimate the range of records that may be operated. It should be limited to a small (10 thousand records) range ,. otherwise, ORACLE will use a large rollback segment to process this transaction. slow program response or even loss of response. if the number of records is more than 100,000, you can complete these SQL statements in multiple parts,
Add COMMIT to confirm transaction processing. II. Data Definition (DDL) section.

1. CREATE (CREATE a table, index, view, synonym, process, function, database link, etc)

Common ORACLE field types include:
CHAR fixed-length string
VARCHAR2 variable-length string
NUMBER (M, N) numeric type M is the total length of digits, N is the length of decimal places
DATE type

When creating a table, you need to put a small field that is not empty in front of the table. If it is possible to put a blank field in the back, you can use a Chinese field name when creating the table, but it is best to use an English field name.

When creating a table, you can add a DEFAULT value to the field, for example, default sysdate. During each insertion or modification, you do not need to perform any program operation on this field to get the operation time.

When creating a table, you can add constraints to fields.
For example, duplicate UNIQUE is not allowed, and the KEY is primary key.

2. ALTER (change tables, indexes, views, etc)

Change table name
Alter table name 1 to table name 2;

Add a field to the end of the table.
Alter table name ADD field Name field name description;

Modify the definition description of fields in the table
Alter table name MODIFY Field Name field name description;

Add constraints to fields in the table
Alter table name add constraint name primary key (field name );
Alter table name add constraint name UNIQUE (field name );

Place the table in or out the memory area of the database
Alter table name CACHE;
Alter table name NOCACHE;

3. DROP (Delete tables, indexes, views, synonyms, processes, functions, database links, etc)

Delete a table and all its constraints
Drop table name cascade constraints;

4. TRUNCATE (clear all records in the table and keep the table structure)

TRUNCATE table name; 3. query statement (SELECT)

SELECT field name 1, field name 2 ,...... FROM table name 1, [Table name 2,...] WHERE condition;

Field names can be imported into functions.
Example: COUNT (*), MIN (field name), MAX (field name), AVG (field name), DISTINCT (field name), TO_CHAR (DATE Field name, 'yyyy-MM-DD HH24: MI: ss ')

NVL (EXPR1, EXPR2) Function
Explanation:
IF EXPR1 = NULL
RETURN EXPR2
ELSE
RETURN EXPR1

DECODE (AA, V1, R1, V2, R2....) Function
Explanation:
If aa = V1 then return R1
If aa = V2 then return R2
.....
ELSE
RETURN NULL

LPAD (char1, n, char2) Functions
Explanation:
Character char1 is displayed based on the specified number of digits n. The reserved number of digits is replaced by the reserved number on the left using the char2 string.

Arithmetic Operations can be performed between Field Names
Example: (field name 1 * field name 1)/3

The query statement can be nested.
Example: SELECT ...... FROM
(SELECT ...... FROM table name 1, [Table name 2,...] WHERE condition) WHERE condition 2;

The results of two query statements can be set.
Example: UNION (remove duplicate records), union all (do not remove duplicate records), difference set MINUS, intersection INTERSECT

Group Query
SELECT field name 1, field name 2 ,...... FROM table name 1, [Table name 2,...] Group by field name 1
[HAVING condition];

Query connections between two or more tables

SELECT field name 1, field name 2 ,...... FROM table name 1, [Table name 2,...] WHERE
Table Name 1. Field name = table name 2. Field name [AND…] ;

SELECT field name 1, field name 2 ,...... FROM table name 1, [Table name 2,...] WHERE
Table Name 1. Field name = table name 2. Field name (+) [AND…] ;

The position of a field with a (+) number is automatically null.

Sort the query result set. The default sorting is ASC in ascending order and DESC in descending order.

SELECT field name 1, field name 2 ,...... FROM table name 1, [Table name 2,...]
Order by field name 1, field name 2 DESC;

Fuzzy string comparison

INSTR (field name, 'string')> 0
Field name LIKE 'string % '[' % string % ']

Each table has an implicit field ROWID, which indicates the uniqueness of the record.

Common Data Objects (SCHEMA) in ORACLE)

1. INDEX)

Create index name ON table name (Field 1, [Field 2,…] );
Alter index name REBUILD;

It is recommended that a table have no more than three indexes (except for a special large table). It is recommended that you use a single-field index to analyze the execution of SQL statements, you can also create a multi-field composite index and a function-based index.

ORACLE8.1.7 the maximum length of a string that can be indexed is 1578 bytes.
The maximum length of an ORACLE8.0.6 string that can be indexed is 758 bytes.

2. VIEW)

Create view name as select .... FROM .....;
Alter view name COMPILE;

A view is only an SQL query statement that simplifies the complex relationships between tables.

3. SYNONMY)
Create synonym name FOR table name;
Create synonym name FOR table name @ Database Link name;

4. DATABASE LINK)
Create database link database connection name connect to user name identified by password USING 'database connection string'; DATABASE connection string can be NET8 easy config or directly modified in TNSNAMES. ORA.

When the database parameter global_name = true, the Database Link name must be the same as the remote database name.

You can use the following command to check the global name of the database:
SELECT * FROM GLOBAL_NAME;

Query tables in a remote database
SELECT ...... FROM table name @ Database Link name;

Permission management (DCL) Statements

1. GRANT Permissions
Common system permission sets include the following:
CONNECT (basic connection), RESOURCE (Program Development), DBA (Database Management)
Common Data Objects have the following permissions:
All on Data Object Name, select on Data Object Name, update on Data Object Name delete on Data Object Name, insert on Data Object Name, alter on Data Object Name

Grant connect, resource to user name;
Grant select on table name TO user name;
Grant select, INSERT, delete on table name TO username 1, username 2;

2. REVOKE permissions from REVOKE

Revoke connect, resource from username;
Revoke select on table name FROM user name;
Revoke select, INSERT, delete on table name FROM username 1, username 2;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.