Overview of SQL statements
SQL language classification
Data definition language (Data Definition Language) is mainly used to modify, create and delete database objects, including CREATE ALTER DROP statement.
Data Query Language (Data Query Language) is mainly used for data query the database, which is mainly a SELECT statement, the SELECT statement includes five clauses, namely FROM WHERE HAVING GROUP BY and WITH statement.
Data manipulation language (Data Manipulation Language) is mainly used to update data in a database in the data table, including INSERT UODATE DELETE statement.
Data Control Language (Data Control Language) is mainly used to grant access to the database and the recovery of certain privileges. Including GRANT REVOKE and other statements.
Things that control language, is mainly used for database control of things, ensure the consistency of data in the database, including the COMMIT ROLLBACK statement.
Common data types
MYSQL:
Standards of writing SQL statements
SQL statements are not case-sensitive keywords
SQL statement and column names are not case-sensitive object names
SQL statement is case sensitive data in the database
NOTE, - - SQL statement using the back there is at least one space, multi-line comments with / * * /
Database creation and deletion
(1) create a database:
CREATE DATABASE database_name;
When writing SQL statements, the SQL statement should be added after the semicolon
(2) deleting the database
DROP DATABASE database_name;
Creating and updating data tables
Database table
(1) Data record: Each row in the data table is referred to as a data record
(2) field: each column of the data table is called a field
(3) the primary key (PRIMARY KEY): the only as a data table, he said each day to ensure the uniqueness of the number of records. Gradually constraints entity integrity in a relational database. The so-called entity integrity, is a constraint that the data table row.
(4) a foreign key (FOREIGN KEY): used to define the foreign key relationships between tables and tables. In the data table, column F if the attribute is an attribute of the relationship B (B is not the primary key relationships), and F is the primary key attribute column A of the relationship, the foreign key B, F is. Foreign key constraints in a relational database referential integrity. Referential integrity refers to the so-called constraint between the table and the table.
(5) index: index is a pointer to the data in the table, an index into the physical location of the fields in the data table. Generally can be indexed in the following cases
Create an index on the primary key column
Multi-table joins, create indexes on columns that are frequently used in connection
Creating indexes on columns are often used in the WHERE clause of a query
Create an index on columns that are frequently grouped GROUP BY and ORDER BY sort of
(6) constraint: In order to ensure data integrity constraints need to use
No unique constraint (UNIQUE) using a unique constraint on a column or a group of the same value, i.e. to ensure the uniqueness values. However, the only constraint can insert a NULL value.
Primary key constraint (PRIMARY KEY) using the primary key constraint to ensure that the column can have a unique value, and can not contain a NULL value, for each column in the data table can define a PRIMARY KEY
Foreign key constraint (FOREIGN KEY) to ensure referential integrity table, to ensure operating on a data table without causing adverse effects on a table associated with
Check constraints (CHECK) limit or value range column conditions, CHECK constraints can be defined as a plurality of columns
Non-empty constraint (NOT NULL) for restraining the column
Create a data table:
CREATE TABLE table_name(
column_name1 datatype [constraint_condition1]
[,column name2 datatype [constraint_condition2]
...)
Using constraints
(1) a unique constraint (UNIQUE) to ensure that a column value is not the same column or row in a group is not the same value, typically creates a constraint for the column, the database will automatically create a unique index for the column, the same name and its index constraint name
For example: CREATE TABLE T_dept (
dept_ID VARCHAR(15) UNIQUE,
dept_Name VARCHAR(10)
(2) primary key constraint
CREATE TABLE T_result(
stuID VARCHAR(15),
cruID VARCHAR(15),
result DOUBLE,
PRIMARY KEY(stuID,curID)
)
(3) foreign key constraints
FOREIGN KEY [table 1] (column name 1) the REFERENCE table 2 (column name 2)
[ON UPDATE [CASCADE]|[SET NULL]|[RESTRICT]]
[ON DELETE [CASCADE]|[SET NULL]|[RESTRICT]]
Wherein, FOREIGN KEY is a keyword, is an optional table 1, a column name table is a foreign key designation data for foreign key constraints, the name of the table 2 represents the main table name, column name from the primary table 2 table names corresponding to the column name of the column 1, the latter oN UPDATE oN dELETE representation of the data table modified or deleted, what kind of operation taken from between the master table is optional
CASCADE: Cascade delete, if a data record in the primary table is deleted, the data from the table will also be deleted
SET NULL: blanking deleted, if a data record in the main table to delete the north and south, the data from the table will also be connected thereto blank
RESTRICE: Restricted deleted, if a primary data table is deleted, the system reports an error in the implementation of the DELETE statement, notify the user and the corresponding data in the main appearances from the table still exists, but the data corresponding to the main appearances without being removed from the table, which is the default mode
CREATE TABLE T_result(
stuID VARCHAR(15),
curID VARCHAR(15),
result DOUBLE,
FOREIGN KEY (stuID) REFERCES T_student (stuID) ON DELETE CASCADE,
PRIMARY KEY (stuID,curID)
)
(4) check constraint CHECK
CREATE TABLE T_curriculum(
curID VARCHAR(15) PRIMARY KEY
curName VARCHAR(10),
credit INT,
CHECK(credit BETWEEN 3 AND 8)
)
(5) a non-null constraint NOT NULL
Use Index
(1) unique index: UNIQUE data table in a column can define a unique index to a data, each unique index value in the index corresponds to only one record in the data table, to ensure the uniqueness of the recorded data in the column
(2) primary index: PRIMARY KEY use in a data table can be a primary index column definition data, called the primary index is the index in the primary key column
(3) separate index: an index defined in the data table is an index on a single column of data, usually in the data query, if the WHERE clause is often used in a column in the data table as a query, we can put the create a separate index for the column
(4) Composite Index: The index can be defined on a plurality of data columns of the data table, this is referred to as the index composite index
(5) clustered index: create and delete index
CREATE [UNIQUE]|[CLUSTER] INDEX index_name
ON table_name (column_name row sequential method ... S) A
Sort: ASC keyword indicates ascending, descending order keyword represents DESC
Query data
Query data records of all the columns
SELECT *FROM table_name or view_name[,table_name1 or view_name1]
Lookup table specified in column
SELECT target column [, target column ...]
FROM table name or view [, table or view name ...]
When the query data table or a view in a SELECT statement to specify a column, designated column name in the SELECT statement must be a column of the query tables specified data table or view is not present in the duplicate records
SELECT DISTINCT target column [, target column ...]
FROM table name or view [, table or view name ...]
Use column aliases inquiry
SELECT target column [AS] column alias [, target column [AS] column aliases ...]
FROM table name or view [, table or view name ...]
Do not use forms a column name in the SELECT statement to query data table or view, the space key may be used in a later query column name instead of the AS, the space behind the above talk alias name. If a column alias for case-sensitive letters, or use the column alias contains spaces or special characters, you must use single or double quotes will cause it to be, otherwise the system will report an error.
Performing operations on records check
SELECT statements can also be used for the column specified arithmetic operator calculates, wherein the arithmetic operator includes addition, subtraction and the result of addition to the mold, when performing arithmetic operators operate on columns specified in the SELECT statement, only display change, not the results will change the display does not change the data in the table columns in the original value
Use connector (||) connected field
In ORACLE, || connectors may be used to connect multiple field connectors in use, data should be the same type of connection, if the system will be given a different, or if necessary to add a character in a field of the date type value of the connected It requires the character value or date value type single quotation marks. Note that does not support the connector || MYSQL and MYSQL SERVER can be used in the MYSQL CONCAT function, can be used in MS SQL SERVER connected to the +
About NULL value
In the database, if not for the class assignment, and there is no default value for this column, this time the result of the query is empty, that is NULL
Using the WHERE clause of a query table to meet the conditions of recording
Compare inquiry
Three types of comparison operators, arithmetic comparison operator, BETWEEN AND, IN
The arithmetic comparison operators
SQL statement including arithmetic comparison operators => = <=> <! = <> (Not equal to)!> (Not greater than)! <(Not less than), arithmetic comparisons may be used in the WHERE clause of a SELECT statement operator on the designated column are compared, it should be noted that, in the MS SQL SERVER, the <> is not meant not equal
In the SQL statement, if the comparison integer data type in the WHERE clause, you can not use single quotes, if the comparison of other data types such as strings, date, etc., must be enclosed in single quotes, additionally, comparison operator in the WHERE clause of the left and right data types must be compatible with the type of