Finishing construction of the table specification
finishing DDL statements
finishing three sentences
First look at an example of construction of the table, go to study what norms should be followed
create table rzdata( id int(11) not null auto_increment, name varchar(200), age int(3), createuser varchar(200) , createtime timestamp not null default current_timestamp, updateuser varchar(200) , updatetime timestamp not null default current_timestamp on update current_timestamp, primary key (id) );
a. table
It is not Chinese, Pinyin is not, or very low
b. style uniform
Unified style for all tables can be viewed from existing tables, or find a leader inspection, post-maintenance convenience
c. first field
The first field must be an id, and self-growth, is the primary key, does not make sense -> Development: Why?
d. primary key
A table has only one primary key, primary key == unique + not null
e. When four fields
After four fields include: users, create time, modify user, modification time
createuser varchar(200) , createtime timestamp not null default current_timestamp, updateuser varchar(200) , updatetime timestamp not null default current_timestamp on update current_timestamp,
f. Business Fields
Business needs a unique field presence, using unique constraints, such as order number
Business fields must add a comment
The COMMENT ' username '
g. charset CHARSET
View characters
>> show variables like '%char%'; +--------------------------+---------------------------------------------------------------+ | Variable_name | Value | +--------------------------+---------------------------------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.6.23-linux-glibc2.5-x86_64/share/charsets/ | +--------------------------+---------------------------------------------------------------+ 8 rows in set (0.00 sec)
DDL statements and points to note
Query: select query field from table;
Note :
Do not use * instead of all the fields under 1. production environment
Error demonstration:
mysql> select * from tb_user; +----+----------+----------------------------------+-------------+---------------------+----------------------------------+ | id | username | password | phone | created | salt | +----+----------+----------------------------------+-------------+---------------------+----------------------------------+ | 28 | zhangsan | e21d44f200365b57fab2641cd31226d4 | 13600527634 | 2018-05-25 17:52:03 | 05b0f203987e49d2b72b20b95e0e57d9 | | 30 | leyou | 4de9a93b3f95d468874a3c1bf3b25a48 | 15855410440 | 2018-09-30 11:37:30 | 4565613d4b0e434cb496d4eb87feb45f | +----+----------+----------------------------------+-------------+---------------------+----------------------------------+ 2 rows in set (0.01 sec)
Correct demonstration:
mysql> select username,password from tb_user; +----------+----------------------------------+ | username | password | +----------+----------------------------------+ | zhangsan | e21d44f200365b57fab2641cd31226d4 | | leyou | 4de9a93b3f95d468874a3c1bf3b25a48 | +----------+----------------------------------+ 2 rows in set (0.00 sec)
2. If the query is particularly large amount of data that must be used where or limit, or need to use a lot of resources
Error demonstration:
select name,image,letter from tb_brand;
Correct demonstration:
select name,image,letter from tb_brand limit 100;
select name,image,letter from tb_brand where id=100;
New statement: insert into table (Field 1, Field 2 ...) values (data 1, data 2 ...);
Note :
After the table name with the corresponding field names to be added
insert into tb_brand (name,letter) values(tunan,T);
Modify the statement: field where conditions after the update table set modification;
Note :
Be sure to add conditions, otherwise it is a global modification
update tb_brand set name="xiaoqi" where id = 1;
Delete statement: delete from table where conditions;
Be sure to add conditions, otherwise it is a global deleted;
delete from tb_brand tb where id = 1;
When a drag strip SQL authentication process of how to do?
Use show processlist; see sql mysql process of
mysql> show processlist; +-----+------+---------------------+--------+---------+------+----------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +-----+------+---------------------+--------+---------+------+----------+------------------+ | 272 | root | localhost | leyou1 | Query | 0 | starting | show processlist | | 273 | root | 121.62.184.34:56629 | leyou1 | Sleep | 448 | | NULL | | 274 | root | 121.62.184.34:56631 | leyou1 | Sleep | 592 | | NULL | +-----+------+---------------------+--------+---------+------+----------+------------------+ 3 rows in set (0.00 sec)
Then delete it according to id
kill id;
You need to remember three commands
change Password:
User Update SET password = password ( 'password ' ) WHERE User = 'username ' ;
edit permission:
. grant all privileges on * * to username @ ' % ' IDENTIFIED by ' password ' ;
Refresh permissions:
flush privileges;