SQL language notes

Common library operations:

- Create a database CREATE DATABASE database name;
- delete the database DROP DATABASE database name;
- displays all databases: SHOW DATABASES;

Common Table Operations:

- Create table: CREATE TABLE table name (attribute name Data type [integrity constraints],
property name data type [integrity constraint],
.
.
Attribute name data table [integrity constraints]
);

Restrictions Explanation
PRIMARY KEY Attribute that identifies the primary key of the table, the unique identifier corresponding to the record 
FOREIGN KEY Attribute that identifies the foreign key table, associated with the primary key of a table 
NOT NULL It identifies the property can not be empty
UNIQUE Identification value of this property is unique
AUTO_INCREMENT It identifies the attribute value is automatically increased
DEFAULT The default value for this property is set

 

- Table View

operating Statement
View basic table structure DESCRIBE (or DESC) table name; // return table fields and their details table
View detailed structure of the table SHOW CREATE TABLE table name; // return the result is to create a table of sql 

 

- Delete table

 

Statement meaning
 drop table 表名; Completely remove the table, delete the field data, free up memory space 
truncate 表名; Delete table data, do not delete the field, free up memory space
delete from table name; or delete * from table name; Remove content does not delete the definition, not free up space, delete the system line by line, lower efficiency than truncate

 

** truncate and delete contrast: **
 TRUNCATE will reset the line and all the high level of the index. When the entire table and index complete browse through the table after the operation than truncate table after the Delete operation much faster. When the table is emptied of the index table and the table is reset to the initial size, but can not delete. 

truncate is implicit submission, you can not trigger any Delete triggers rollback command can not be withdrawn, and can delete, delete statement line because each deletion, are recorded one for each row deleted in the transaction log; 
truncate function in the Table on a delete statement with no WHERE clause is the same: both remove all rows in the table. But truncate delete faster than the speed, and low system resource usage and transaction logs, because delete is to delete the line by line. 
Note: truncate can not empty the parent table 

 

 


- Modify table

 

operating Statement
Modify the table name ALTER TABLE table name RENMAE new table the old name;
Modify the field ALTER TABLE table name CHANGE old name for the new property attribute names new data types
Increase the field ALTER TABLE table name ADD attribute name Data type [integrity constraints] 
Delete field ALTER TABLE table DROP attribute name

 

- insert data

 

operating Statement
All fields of the table to insert data  INSERT INTO table name VALUES (value 1, value 2 and value 3, ..., n-value);
Inserted to the specified fields of the table data INSERT INTO table name (attribute 1, property 2, ..., attributes n) VALUES (value 1, value 2 and value 3, ..., n-value);
Simultaneously insert multiple records  INSERT INTO table name [(attribute list)] the VALUES (argument list 1), (2 argument list) ..., (n-value list);



- update data

SET UPDATE table name attribute value 1 = 1, attribute name = value 2 2, ..., n = attribute name value n WHERE conditional expression;

- delete data

DELETE FROM table [WHERE conditional expression]


Left a large piece of inquiry, to be updated! ! !

 

Guess you like

Origin www.cnblogs.com/CodeHunter-qcy/p/11614926.html