Common Oracle SQL syntax and Data Object Conversion

Source: Internet
Author: User

I. Data Control Statement (DML)

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

Insertinto table name (field name 1, field name 2 ,......) Values (value 1, value 2 ,......);
Insertinto 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, 'goodday'
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 use the system time sysdate of the current database, accurate to seconds
Or use a string to convert to the date type function to_date ('2017-08-01 ', 'yyyy-MM-DD ')
To_date () has many date formats. For more information, see oracledoc.
Year-month-day hour: minute: Second format YYYY-MM-DDHH24: MI: SS

The maximum length of a string that can be operated during insert is less than or equal to 4000 single bytes. To insert a longer string, use the clob type for the field,
Use dbms_lob in OracleProgramPackage.

If you want to use a serial number that automatically increases from 1 during insert, you should first create a serial number
Createsequence serial number name (preferably table name + Serial number mark) incrementby1startwith1
Maxvalue99999cyclenocache;
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)

Where efrom 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.
Truncatetable 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;
An error occurs when the value of N exceeds the defined length. It is best to verify the length before insertion ..

Note:
A. The preceding SQL statements have row-level locks applied to tables,
After confirmation, you must add the Transaction Processing Command commit to take effect,
Otherwise, the changes may not be written into the database.
To recall these operations, run the rollback command to restore them.

B. Estimate the range of records that may be operated before running insert, delete, and update statements,
It should be limited to a small range (10 thousand records). Otherwise, Oracle will use a large rollback segment to process this transaction.
Slow program response or even loss of response. If more than 100,000 of these operations are recorded, you can complete these SQL statements in multiple parts,
In the meantime, commit is added to confirm transaction processing.

Ii. Data Definition (DDL)

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 field that may be empty in the back

You can use a Chinese field name when creating a table, but it is best to use an English field name.

When creating a table, you can add the default value to the field, such as defasyssysdate.
In this way, the operation time can be obtained every time you insert or modify this field without any program operation.

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

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

Change table name
Altertable table name 1to table name 2;

Add a field to the end of the table.
Altertable table name add field Name field name description;

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

Add constraints to fields in the table
Altertable table name addconstraint constraint name primarykey (field name );
Altertable table name addconstraint constraint name unique (field name );

Place the table in or out the memory area of the database
Altertable table name cache;
Altertable table name nocache;

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

Delete a table and all its constraints
Droptable table name cascadeconstraints;

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

Truncate table name;

Iii. Select

Select field name 1, field name 2 ,...... From table name 1, [Table name 2,...> Where condition;

Field names can be imported into functions.
For example, count (*), min (field name), max (field name), AVG (field name), distinct (field name ),
To_char (Date Field name, 'yyyy-MM-DDHH24: MI: ss ')

Nvl (expr1, expr2) Function
Explanation:
Ifexpr1 = NULL
Returnexpr2
Else
Returnexpr1

Decode (AA, V1, R1, V2, R2....) Function
Explanation:
IFAA = v1thenreturnr1
IFAA = v2thenreturnr2
.....
Else
Returnnull

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 (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,...> Groupby 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,...>
Orderby 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.

4. Common Data Objects (schema) in Oracle)

1. Index)

Createindex index name on table name (Field 1, [Field 2,...> );
Alterindex 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)

Createview view name asselect .... From .....;
Alterview name: compile;

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

3. synonmy)
Createsynonym synonym name for table name;
Createsynonym synonym name for table name @ Database Link name;

4. Database Connection (databaselink)
Createdatabaselink database connection name connectto username identifiedby password using 'database connection string ';

You can use net8easyconfig to change the database connection string or directly modify the definition 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 * fromglobal_name;

Query tables in a remote database
Select ...... From table name @ Database Link name;

V. 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, SELECTON Data Object Name, updateon Data Object Name,
Delete on Data Object Name, inserton Data Object Name, alteron Data Object Name

Grantconnect, resourceto user name;
Grantselecton table name to user name;
Grantselect, insert, deleteon table name to username 1, username 2;

2. revoke permissions from revoke

Revokeconnect, resourcefrom user name;
Revokeselecton table name from user name;
Revokeselect, insert, deleteon 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.