mysql- build tables, add fields, modify field, add the index SQL statement written

Operating table

1, construction of the table

- If there is USERS table is deleted

DROP TABLE IF EXISTS USERS; 
The CREATE TABLE USERS ( 
the above mentioned id INT UNSIGNED the NOT NULL AUTO_INCREMENT, # primary key UNSIGNED unsigned AUTO_INCREMENT increment 
name VARCHAR (30) NOT NULL COMMENT ' user name' 
Sex ENUM ( 'male', 'female') NOT NULL DEFAULT 'male' # using an enumerated type may be inserted when expressed numerically, from the beginning as M 1 represents. 
Age TINYINT UNSIGNED the NOT NULL the DEFAULT 1, 
a PRIMARY KEY ( `Id`) 
) = the DEFAULT the CHARSET = INNODB the UTF8 ENGINE;

Operating fields are format ALTER TABLE table name

2, add fields

ALTER TABLE USERS ADD alias varchar(20) COMMENT '别名';

3, modify the field

  3.1 does not change the name to use modify

ALTER TABLE USERS MODIFY name varchar (20) NOT NULL COMMENT 'username';

  3.2 change the name using the format change is a change to modify the name of the new name ...

ALTER TABLE USERS CHANGE name username varchar(20) NOT NULL COMMENT '用户名';

4, delete the field

The COLUMN DROP TABLE USERS Alias the ALTER; 
- delete multiple fields 
- ALTER TABLE USERS DROP COLUMN alias, DROP COLUMN age;

 

Library Operation

1, building a database

CREATE DATABASE test CHARACTER SET utf8;

2, modify the default encoding library

ALTER DATABASE test CHARACTER SET utf8;

3, delete the library on foot

DROP DATABASE test;

  

 

Index (constraint) operation

1, add a primary key

- Drop Primary

ALTER TABLE 表名 DROP PRIMARY KEY;

 

ALTER TABLE USERS ADD PRIMARY KEY ( `id` )

2, add a unique index

ALTER TABLE USERS ADD UNiQUE ( `username` )

3, add a normal index

ALTER TABLE `USERS` ADD INDEX index_name ( `age` )

4, add full-text indexing

ALTER TABLE `the ADD FULLTEXT table name` ( 
`` column name 
)

5, add multi-column index 

ALTER TABLE `table` ADD INDEX index_name ( `1` column name, column name` 2`, `3` column name)

6, the foreign key added

Rarely foreign key, foreign key constraint code logic implemented by

alter table add constraint fk_ reference table id foreign key (reference id) References referenced table (referenced id)

 

alter table table name drop forign key fk_ reference id

Database management commands

1, the connection

mysql -u root -p123456 
or 
mysql -u root -p 

password

2, viewing operation

- View all the libraries
show databases;

- View the table where the database
show tables;

- Check the library's creation statements
show create database test;

- see the table creation statements
show create table test;

- see the table of the index
- show index from table_name (table name)
Show index from the Users;

- Check mysql version
select version ();

- Check your current user
select user ();

3. Create user

- Of particular note, in MySQL, an account consists of two parts: 
- 1. the User 
- 2. host 
- even if the same user, as long as the host is different will be considered different account. 
- This is very easy to access from different ip addresses fine access control. 
- By default, users created for host '%', which is a sign, like fuzzy query in the meaning, which matches all 
create user [user name] identified by '[password]'; 
the Create the User vip IDENTIFIED by 'vippp'; - all connections 
create user vip@'127.0.0.1 'identified by' xxx '; - local connections 
create user vip@'192.168.%' identified by 'yyy '; - 192.168 network connection

4, delete users

drop user username; 
or 
delete from mysql.user where user = 'username' and host = '%'

5, change passwords

set password from 'username' @ host = password ( 'new password'); 
or 
update mysql.user set password = password ( 'new password') where user = 'username' and Host = '%'; 

- Use password () encrypted

6, set user permissions

. grant all on * * to vip@'127.0.0.1 '; - all the rights granted to all databases on the VIP users connected through the machine; 

Grant Books * All privileges to VIP ON @.'% '; - the books say on the database has the right to grant all vip users are connected; 

grant the SELECT oN books.users to vip @ '%'; - the development of access to users on the books database table to vip user; 

grant All oN * . * to vip @ '%' with grant potions; --with grant potionss mean you can give permission to vip to other users 

flush privileges; - after setting the permissions to refresh permission

 

Guess you like

Origin www.cnblogs.com/f-rt/p/11141421.html