1. SQL basic operations
1.1 library operations
1.1.1 New
Create database 数据库名字 [库选项];
Library Option : used to constrain the database is divided into two options
- Char Set : charset / character set specific character set (stored data encoding format): common character set: GBK and UTF8
- Setting calibration set : collate specific calibration set (rule data comparison)
Among them, the database name can not use the keyword (characters have been used) or a reserved word (might use in the future). If you have to use keywords or reserved words, you must use anti-quotation marks (output esc key following key in the English state: `)
1.1.2 View
- View all databases:
show databases
; - Examine a specific portion of the database: Fuzzy query
Show databases like ‘pattern’;
pattern matching mode
- %: Indicates match multiple characters
- _: That matches a single character
- View the database creation statement:
show create database 数据库名字
1.1.3 Modify
Database name can not be modified.
Modify the library only option for the database: character sets and proof sets (proofreading set depends character set)
Alter database 数据库名字 [库选项]
Charset/ character set [=] 字符集
Collate 校对集
1.1.4 Delete
Drop database 数据库名字
Table 1.2 Operation
1.2.1 New
Create table [if not exists] 表名(
字段名字 数据类型,
字段名字 数据类型 -- 最后一行不需要逗号
)[表选项];
Not EXISTS IF : If the table name does not exist, it is created, otherwise it does create the code: check
menu option : control performance table
- Charset: charset / character set specific character sets; - ensure that the table data stored in the character set
- Proofing Set: collate proofreading particular set;
- Storage Engine: engine specific storage engine (innodb and myisam)
A list of any design must specify the database.
Scheme 1 : belongs to the specified database table displayed
Create table 数据库名.表名()
; - the current data table to create a specified database under
scheme 2 : Implicit specified table belongs to the database: the first environment into a database, and then the table thus created automatically attributed to a . a specified database
into the database environment:use 数据库名字
1.2.2 Queries
How the database can be viewed in the table can be viewed.
- View all the tables:
show tables
; - View section Table: fuzzy matching:
show tables like ‘pattern’
; - View table creation statement:
show create table 表名
; - See table structure: see the field information table
Desc/describe/(show columns from) 表名
;
1.2.3 Modify
Modify the table is divided into two parts: Modify the table itself and modify the field
(1) modify the table itself
table itself can be modified: the table name and table options
to modify the table name: rename table 老表名 to 新表名
;
modify table options: Character Set, proof sets and storage engines: Alter table 表名 表选项 [=] 值
;
( 2) modify the field
field Action lot: add, modify, duplicate names, delete
new fields
Alter table 表名 add [column] 字段名 数据类型 [列属性] [位置]
;
location: field names can be stored anywhere in the table
- First: the first position
- After: After which field: after the field name; the default is the last in a field after
Modify Field :
Modify usually modify the properties or type of data
Alter table 表名 modify 字段名 数据类型 [属性] [位置]
;
rename fields
Alter table 表名 change 旧字段 新字段名 数据类型 [属性] [位置]
;
remove a field
Alter table 表名 drop 字段名
;
1.2.4 Delete
Drop table 表名1,表名2...
; - You can erase multiple tables
1.3 Data Manipulation
1.3.1 New
There are two options
Scheme 1 : a Full Field inserted data need not specify the list of fields: consistent with the order request data value appears to be problems with the design fields in the table: all non-numerical data, require the use of quotation marks (recommended single quotes) package
Insert into 表名
values(值列表)[,(值列表)];
Scheme 2 : a part of the field data is inserted, a list of selected fields required: the field regardless of the order of occurrence of the field list; a list of the order of the selected value of the field must be consistent with the order .
Insert into 表名 (字段列表)
values (值列表)[,(值列表)];
1.3.2 Queries
Select */字段列表 from 表名 [where条件]
;
1.3.3 Modify
Update 表名 set 字段 = 值 [where条件]
1.3.4 Delete
Delete from 表名 [where条件]
;
2. garbage problem
Chinese data essential problem is the character set problem
to see which character set of the server in the end recognize:
Show character set
Check the server's default character set with the client deal:
Show variables like ‘character_set%’
;
Solution (defined client is GBK coding) :
(1) changing the server, the default receive character is set GBK;
Set character_set_client = gbk
;
(2) modify the server to the client data GBK character set
Set character_set_results = GBK
;
Set variable = value; modified only if the session level (the current client, when the sub connection is valid: Close Failure)
Setting server understanding of the client's character set: You can use a shortcut: set names character set
Set names gbk
;
equivalent to the character_set_client,character_set_results,character_set_connection gbk;
Connection connection layer: is the intermediary character set change, the more efficient if unified, not uniform, no problem.
3. Set proofreading
Proof set: way of comparison data
Proof set has three formats
- _bin: binary, binary comparison, remove the bit, bit by bit comparison is case sensitive
- _cs: case sensitive (case sensitive): Case
- _ci: case insensitice (case insensitive): case-insensitive
View proofreading set database supported: show collation
;
proof sets Application: only when comparing the data generated, proofreading set to take effect
[ important ] proof set: must be declared in the absence of good data, if they have data, then proofread again set Review: so modify invalid.
-- 新增 --
[库]
Create database 数据库名字 [库选项]
[表]
Create table [if not exists] 数据库名字.表名(
字段名字 数据类型,
字段名字 数据类型 -- 最后一行不需要逗号
)[表选项]
use 数据库名字;
Create table [if not exists] 表名(
字段名字 数据类型,
字段名字 数据类型 -- 最后一行不需要逗号
)[表选项]
[数据]
Insert into 表名 values(值列表)[,(值列表)]
Insert into 表名 (字段列表) values (值列表)[,(值列表)]
--查询--
[库]
show databases
Show databases like ‘pattern
show create database 数据库名字
[表]
show tables
show tables like ‘pattern’
show create table 表名`
Desc/describe/(show columns from) 表名`
[数据]
Select */字段列表 from 表名 [where条件]
-- 修改--
[库]
Alter database 数据库名字 [库选项] Charset/ character set [=] 字符集 Collate 校对集
[表]
rename table 老表名 to 新表名
Alter table 表名 表选项 [=] 值
Alter table 表名 drop 字段名
[数据]
Update 表名 set 字段 = 值 [where条件]
-- 删除--
[库]
Drop database 数据库名字
[表]
Drop table 表名1,表名2...
[数据]
Delete from 表名 [where条件]