table of Contents
First, the basic operation of the database
Second, the type of database column
Third, database type
Fourth, the operating table
Five, CRUD
First, the basic operation of the database
1 View all databases
show databases;
2, into the database
use database name;
3, delete the database
drop database [IF EXISTS (absence not being given, warning)] database name;
4, see the table (the premise: access to the database)
describe [IF NOT EXISTS (absence not being given, warning)] table;
5. Create a database
create database [if not exists] database name;
Second, the type of database column
1, the value
-
int 4 bytes of data storage criteria
-
large data storage bigint 8 bytes
-
a string of decimal floating-point numbers (financial calculation, data-sensitive industries)
-
tinyint very small data one byte
-
smallint 2 bytes smaller data
-
mediumint medium-sized data of the three bytes
-
Float float 4 bytes
-
double floating point number 8 bytes
2, string
- The variable string varchar 0 ~ 65535 String (Java)
- text text string 2 ^ 16-1 save large files
- char string of fixed size of 0 to 255
- micro text tinytext 2 ^ 8-1
3, the date and time
- datetime YYY-MM-DD HH: mm: ss format most commonly used time
- 1970.1.1 timestamp to the current time stamp milliseconds
- data YYY-MM-DD, date format
- time HH: mm: ss, time format
- year year
4、null
- Does not make sense, generally do not hook is not empty, the default is usually null
Third, database type
1、MYISAM和INNODB
MYISAM | INNODB | |
---|---|---|
Affairs | not support | stand by |
Datarows | not support | stand by |
Foreign key constraint | not support | stand by |
Full-text index | stand by | not support |
Table space size | Small | About 2 times MYISAM |
the difference | Space saving, fast | High security, support for transaction processing, multi-table multi-user operation |
Fourth, the operating table
1, modify the table name
Old alter table table name rename as a new table name
2, increasing the fields of the table
alter table fields add ranked attribute table []
3, modify the constraints
alter table modify field ranked attribute table []
4, field rename
alter table table name change old field names among the new field attributes []
5, with the constraint modifying fields rename
Modify the constraints | Rename the field |
---|---|
The column properties modify the field names of fields, such as the int to varchar, modify a table constraint (constraint modification only) | Change the name field, must take the column properties can also be modified constraint (you can modify the name and constraints) |
6, delete table fields
alter table table name drop field names
7, delete the table
drop table [if exists] 表名
8, the foreign key (cool)
In the application layer is now generally used to write the code
alter table
表名
add constraintfk_外键字段
foreign key(外键字段
) references外链表
(外键字段
)
You must first remove the sheet list can link
Five, CRUD
1, increasing the data (field value must correspond) (the DML Language)
Single-line increase: to increase the one-way data
Syntax: insert into table (field 1, field 2, field 3, ...) values ( 'value 1', 'value 2', 'value 3', ...);
Example: insert into people (id, name, gender) values ( '1', 'John Doe', 'M 1');
A plurality of rows increases: a plurality of rows increases even two rows of data (with the first and second rows, spaced apart from, the other and so on)
Syntax: insert into table (field 1, field 2, field 3, ...) values ( 'value 1', 'value 2', 'value 3'), ( 'value 1', 'value 2', 'values 3 ');
Example: insert into people (id, name, gender) values ( '1', 'John Doe', '1'), ( '2', 'John Doe', 1 ');
2, modify data (a plurality of data modification, separated) (the DML Language)
Format: update table name = name of the new column set value [name = new value column] WHERE column name = value [and column name = value];
- Later data set is required to modify the column (a plurality of modified data separated by commas).
- It is a condition where the back (with a plurality of conditions are met and, with only a need to meet or), for locking the need to modify the line (conditional operators may be used).
Operation operator:
Operators | meaning | Examples | result |
---|---|---|---|
= | equal | 1=2 | false |
<> Or! = | not equal to | 1!=2 | true |
> | more than the | 1>2 | false |
< | Less than | 1<2 | true |
<= | less than or equal to | 1<=2 | true |
>= | greater than or equal to | 1>=2 | false |
between … and … | Within a certain range | id between 1 and 2; | id in [2] are met within |
and | && and must both meet | id = 1 && username = 'John Doe' | Both to meet the return true |
or | || or only need to meet a | id = 2 || username = 'John Doe' | As long as a satisfying return true |
★ Be sure to add conditions, otherwise all the data that column all the changes, if there are one million, one million will change.
例:update student_user set age = 18 where id = 3;
例:update student_user set age = 18 where id = 3 and username = '‘小琦’;
Wrong Demonstration:
3, delete data (DML language)
1.delete
Syntax: delete from table where conditions; (you can not add conditions, but will empty all the data, plus the conditions will delete the specified column)
例:delete from student_user where id = 1;
2.truncate (dedicated to the empty database table structure and the constraint index will not change)
Syntax: truncate table name;
And truncate the difference 3.delete
delete | truncate | |
---|---|---|
Same point | Data can be deleted without deleting the table structure | Data can be deleted without deleting the table structure |
difference | Increment data calculator, not cleared | You can make the increment data counter is cleared, again from the beginning |
It will affect the transaction | It will not affect the transaction |
4.delete deleted, restart the database
- INNODB Engine: increment restart from 1 (stored in memory, power outages and lost);
- MYISAM engine: do not start at 1 and continue on a self-increment from the beginning (stored in a file, will not be lost because of power failure);
4, query data (DQL, focus)
DQL (Data Query language): data query language;
- All queries are using select
- Simple and complex queries can all be done
- The most important language database
- Project 90% of the time using query
Single-table query: select * (* indicates that the query all the columns, you can also check the specified column) from table where [condition];
例:select username,passwrod form student_user where id = 3;
Since the column data can give an alias (using as keywords):
Multi-table query: select * (* representing a query for all the columns, the query may specify the column) from Table 1, Table 2 where [Condition];
例:select username,passwrod form student_user,people where id = 3;
Can give table names and column data from an alias (used as keywords):
MySQL the SELECT query: https://blog.csdn.net/sabstarb/article/details/105080290