Oracle database study notes (day4)

Structured query language SQL (Structured Query Language) is 

divided into 5 categories
1> query statement: retrieve data in the database
SELECT
2> data manipulation statement: modify and delete data in the database
INSERT/UPDATE/DELETE
3> data definition statement: define the database Structure
CREATE/ALTER/DROP/RENAME/TRUNCATE
4> Transaction control statement: used to revoke or commit some operations
COMMIT/ROLLBACK/SAVEPOINT
5> Data control statement: used to control access to data
GRANT/REVOKE

*Create table
CREATE [ GLOBAL TEMPORARY] TABLE tableName(
  colName colType [DEFAULT defaultExp CONSTRAINT constraintDef]
  [,colName colType...]
) [ON COMMIT {DELETE/PRESERVE} ROWS] 
  TABLESPACE tabSpace;

GLOBAL TEMPORARY indicates that the rows in this table are temporary, which This kind of table is also called 'global temporary table', the rows in the temporary table are specific to a user session, and the validity period of the row is specified by the ON COMMIT clause.

ON COMMIT DELETE ROWS means that all data in the temporary table is automatically deleted after the transaction is committed
ON COMMIT PRESERVE ROWS means that the data is retained until the end of the session after the transaction is committed.

DEFAULT is used to specify the default value of the column.
CONSTRAINT is followed by the constraints on the column

* naming convention of the oracle database table. The length of the
table name is 1 to 128 characters, including letters, numbers and #, $ sign, and _;
the first character must be a letter;
if the name contains spaces, double quotes must be used. (Normally speaking, spaces in table names are generally not allowed)

*Char(length), a data type commonly used in databases
: fixed-length character strings. If the actual length is insufficient, spaces will be added at the back. The length is between 1 and 2000. The unit is byte
VARCHAR2(length): variable-length string, length represents the maximum length, between 1 and 4000, the unit is byte
NCHAR(length): fixed-length UNICODE string, which stores two characters for any character. Byte UNICODE code, length indicates the number of characters, between 1 and 1000
NVARCHAR2(length): variable-length UNICODE string, length indicates the number of characters, between 1 and 2000
BLOB: (Binary Large Object) binary large data, can store up to 4G data
CLOB: single-byte character data, can store up to 4G data
NCLOB: UNICODE character data, can store up to 4G data
DATE: date type, store date and time values
NUMBER(precision,scale): numeric type, which can store floating-point numbers or integers, precision represents the number of significant digits, and scale represents the number of decimal digits. The maximum precision supported by oracle is 38 bits, and the excess digits will be truncated
. BINARY_FLOAT: 32-bit floating-point number, single-precision floating-point number data type, can support at least 6-bit precision, each value of BINARY_FLOAT requires 5 bytes, including the length Byte
BINARY_DOUBLE: 64-bit floating-point number, double-precision floating-point number data type, each value of BINARY_DOUBLE requires 9 bytes, including length bytes

**insert record
× single row insert
INSERT INTO tableName(columnNameList) values(valueList)- -Insert a row of records into the table
columnNameList represents a list of column names, you can only list part of the table, valueList is a list of values, the number and data type must match the columnNameList, and
the unlisted will be set to null or default, but The defined primary key columns and non-null columns must specify values

. The columnNameList part can be omitted, but at this time, the values ​​of all columns in the table need to be provided in the valueList, and the order should be the same
as that of the columns listed in the DESC command (not recommended). To store single quotation marks in the data, you need to use two consecutive single quotation marks

× multiple rows to insert
INSERT INTO tableName(columnNameList)
  SELECT...FROM...WHERE... Insert the query result into the table
Note :
1. When inserting a new record, the value of the primary key column cannot be repeated. If the value of the primary key column is repeated in the inserted row, an error will occur ('violate the unique constraint condition')
2. If there is a foreign key constraint, when inserting data, The given column value is either a null value, or the value must exist in the parent table, otherwise an error occurs ('violate the complete constraint condition')
3. The '' in the string represents a null value, and ' ' contains a space The string

** update record
UPDATE tableName SET colName=value,... WHERE...--Update the restricted row and column in the table
Note :
1. If you want to update the value of the primary key column in the table, and the value is The record reference in the table, an error will occur ('Complete constraint violated - child record found')
2. If there is a foreign key constraint column in the current table, then when updating the data of this column, the given column value is either is empty, or the value must exist in the parent table, otherwise an error occurs ('full constraint violated - parent key not found')

**delete records
DELETE FROM tableName WHERE...--deletes records from table based on conditions

Note:
If the value of the primary key column in the table to be deleted, if the value is referenced by the child table, an error will occur ('violate the complete constraint condition')
(if you want to implement the function of cascade deletion, you need to specify ON when creating the foreign key constraint column DELETE CASCADE, if ON DELETE SET NULL is specified for the column of the foreign key constraint, then when the parent record is deleted, the column corresponding to the child record is automatically set to NULL)

**Constraint
1. Types of constraints:
 1> Primary key constraints: any in the table The value of a row in the primary key column cannot be empty, and the value of the primary key column can uniquely determine a record
 2>Foreign key constraint: The value of the foreign key column is either empty or equal to a value of the column it refers to, and the foreign key column can only refer to a column with unique constraints (including the primary key) in another table
 3> Unique Constraint: The column is either null or non-repeatable
 4> Check Constraint: The value of the column is either null or conforms to the restriction of an expression
 5> Not null constraint: No row in the table can have a null value in this column

2.
 1> Column-level constraints: follow the definition of each column, column-level constraints are part of the column definition
 2> Table-level constraints: all columns are defined, and subsequent statements to define constraints independently, table-level constraints can be specific to a column or multiple columns

× column-level constraints
CONSTRAINT constraintName[]
  primary key constraint: PRIMARY KEY
  foreign key constraint: REFERENCES otherTableName(columnName)
  unique constraint: UNICODE
  check constraint: CHECK(exp)
  not-null constraint: NOT NULL
(1) Constraint naming convention
pk_table_column
fk_table_column
uq_table_column
ch_table_column
nn_table_column
(2) You can not specify the constraint name, the database will automatically generate the constraint name
(3) A column can have multiple constraints

× table-level constraints
Primary Key Constraint CONSTRAINTS pk_tbName_colName PRIMARY KEY(colName)
Foreign Key Constraint CONSTRAINTS fk_tbName_colName FOREIGN KEY (colName) REFERENCES tbName(colName)
Check Constraint CONSTRAINTS ck_tbName_colName CHECK(exp)
Unique Constraint CONSTRAINTS uq_tbName_colName UNIQUE(colName)

Not Null Constraint MODIFY (colName colType not null) --belongs to modification, not table-level constraints

Note :
  non-null constraints are generally defined in column-level
  union primary keys and only table-level constraints can be used

. 3. Column default value: DEFAULT exp
        The order of DEFAULT phrases and CONSTRAINT phrases cannot be reversed





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325732225&siteId=291194637