# Database
## 1.1 concept
Database: db, database, storing the data warehouse, data stored as files.
DBMS: Database Manager System (DBMS)
Common database:
1, ** MYSQL **: free small and medium sized databases, Oracle's acquisition
2, ** Oracle **: large databases charges, Oracle company
3, SQL Server: Microsoft's large database of charges
4, SQLite: Embedded mobile end of a small database
Database Category:
1, Relational:
* MYSQL
*Oracle
Advantages: General SQL statement, easy to understand
drawback: due to the complex table structure, a large amount of data read and write performance is poor
2 non-relational:
* MongoDb
* redis
Benefits: good read and write performance, easy to expand
disadvantages: high costs and the cost of learning
## 1.2 Structure
Class ---> table
Properties ---> fields, list
objects ---> Record
## 1.3 Syntax
1, a statement can be divided into single or multiple rows written
2 statement with a semicolon
3, not case-sensitive, but the keyword suggestion uppercase
4, comments:
Single-line comments: - Comment # contents or footnotes (MYSQL-specific)
multi-line comments: / * comment * content /
## 1.4 SQL language
SQL: Structured Query Language (Structured Query Language)
classification:
1, DDL: data definition language
library, operating table
Create, drop, ALTER
2, the DML: Data Manipulation Language
Recording to add, delete, change
insert into, update, delete, set , from
3, DQL: Data Query Language
The records query
the SELECT, the WHERE, in, ON, limit, like, the HAVING, by the Order,
Group by, desc, asc
4, DCL: database access language
Database set security levels and access to
revoke
CRUD: create、 retrieve、 update、delete、
# SQL
Operation ## First, the library
### 1.1 Creating a database
Directly create:
create database library name;
create and set the library code:
create database library name character set encoding;
Was created to determine whether the database already exists, there is no
create database if not exists 库名;
View ### 1.2 Database
1, see what database management system
show databases;
2, check the database creation statements
show create database 库名;
Delete ### 1.3 Database
Delete: drop database library name
First determine whether there exist, remove:
drop database if exists 库名;
### 1.4 modify the database
Modify the database encoding format:
alter database character set encoding library name
## 1.5 Other operations
Clearly want to use the database:
use the library name;
## Second, the operating table
### 2.1 Creating table
create table shows that (1 Field Type [bound], type 2 Field [bound] .....);
Types of:
. 1, int
2, Double
. 3, VARCHAR
. 4, DATE: the MM-dd-YYYY
. 5, datetime: yyyy-MM-dd HH: mm : ss
. 6, timestamp: timestamp yyyy-MM-dd HH: mm : ss
if timestamp value is not set, then the value is the current time record added
### 2.2 Delete table
Delete:
drop the Table table name;
First determine whether there is a table, there is deleted
drop table if exists 表名;
### 2.3 Table View
See all tables in the current library
show tables;
View table structure specified table
desc table name;
### 2.4 Modify Table Structure
Modify the table name:
alter table table name rename to new table name;
Adding a field:
alter table column name table type add [bound];
Modify the column name:
alter table change table column names old new Column name Type [bound];
Modify columns:
alter table modify table column names Type [bound]
Delete Column:
alter table drop table column name;
### 2.5 Other operating
Copy table structure:
create table table name like you want to copy the table name;
## Third, the recording operation
### 3.1 Add Record
Adding value to all fields in the record:
insert into table values (value 1, value 2 and value 3 ....)
Adding the value to the specified fields in the record:
insert into table (field1, field2 ...) values (Value 1, .... value);
### 3.2 modify records
Modify all of the specified field values of the record:
set update table field name field value 1 = 1, 2 field name = value 2 ,. . . . ;
Modifying the specified value of the specified field record:
set update table field name field value 1 = 1, 2 field name = value 2 ,. . . where conditions;
### 3.3 Delete Record
Delete the specified record:
delete from table where conditions;
Delete all records:
delete from table name; (delete one by one table all the records, poor performance)
truncate table table name; (a one-time delete the entire table, better performance, it is recommended to use,
when to use the table again, the table will be to rebuild a new table based on the original state)
### 3.4 Search record
Search all fields of information recording:
select * from 表名;
Query all records of the specified field information:
select field 1, field 2 .. . from table name;
Discover all the information specified record fields:
select * from where 条件;
Query information specified field of the specified record:
select from where the specified field conditions;