Frequently used mysql operations to add new users/assign permissions/modify tables/create indexes _ MySQL

Source: Internet
Author: User
Mysql common operations add new users assign permissions to modify table creation indexes and other bitsCN.com

Mysql common operations include adding new users, assigning permissions, modifying tables, and creating indexes

Bin> mysql-u root

Mysql> grant permission 1, permission 2 ,... Permission n on database name. table name to user name @ user address identified by 'connection password ';

Permission 1, permission 2 ,... Permission n indicates 14 permissions, including select, insert, update, delete, create, drop, index, alter, grant, references, reload, shutdown, process, and file.

When permission 1, permission 2 ,... Permission n is replaced by all privileges or all, indicating that all permissions are granted to the user.

When the database name. table name is replaced by *. *, it grants the user the permission to operate all the tables in the database on the server.

The user address can be localhost, IP address, machine name, or domain name. You can also use '%' to connect from any address.

The 'connection password' cannot be blank; otherwise, creation fails.

For example:

Mysql> grant select, insert, update, delete, create, drop on vtdc. employee to joe@10.163.225.87 identified by '123 ′;

Assign the user joe from 10.163.225.87 the permission to perform select, insert, update, delete, create, drop, and other operations on the database's vtdc employee table, and set the password to 123.

Mysql> grant all privileges on vtdc. * to joe@10.163.225.87 identified by '000000 ′;

Assign the user joe from 10.163.225.87 the permission to perform all operations on all tables in the database vtdc and set the password to 123.

Mysql> grant all privileges on *. * to joe@10.163.225.87 identified by '000000 ′;

Assign the user joe from 10.163.225.87 the permission to perform all operations on all tables in all databases and set the password to 123.

Mysql> grant all privileges on *. * to joe @ localhost identified by '000000 ′;

Grant the local user joe the permission to perform all operations on all tables in all databases and set the password to 123.

Create a table with foreign keys:

Create table question (id int auto_increment primary key not null, content varchar (300) not null, intime datetime not null, u_id int not null, foreign key (u_id) references user (id) on delete cascade on update cascade );

Backup data table

Shell> mysqldump [OPTIONS] database [tables]

Example:

/Usr/local/mysql/bin/mysqldump-u username-p database name>./fedtrainning_db. SQL

If you do not specify any tables, the entire database will be exported.

Modify the attributes of a table ======================================================

Alter table modify y CHANGE content varchar (500) not null

// Primary key

Alter table tabelname add new_field_id int (5) unsigned default 0 not null auto_increment, add primary key (new_field_id );

// Add a new column

Alter table t2 add d timestamp;

Alter table infos add ex tinyint not null default '0 ';

// Delete a column

Alter table t2 drop column c;

// Rename a column

Alter table t1 change a B integer;

// Change the column type

Alter table t1 change B bigint not null;

Alter table infos change list tinyint not null default '0 ';

// Rename the table

Alter table t1 rename t2;

Add index

Mysql> alter table tablename change depno int (5) not null;

Mysql> alter table tablename add index name (field name 1 [, field name 2…]);

Mysql> alter table tablename add index emp_name (name );

Index with primary keywords

Mysql> alter table tablename add primary key (id );

Add an index with unique conditions

Mysql> alter table tablename add unique emp_name2 (cardnumber );

Delete an index

Mysql> alter table tablename drop index emp_name;

Modify table:

Add field:

Mysql> alter table table_name ADD field_name field_type;

Modify the original field name and type:

Mysql> alter table table_name CHANGE old_field_name new_field_name field_type;

Delete field:

Mysql> alter table table_name DROP field_name;

========================================================== ==============

1. modify the entire database server

In the [mysqld] section of the my. cf file, set:

Default-character-set = utf8

2. set a database separately:

Alter database testdb character set utf8;

3. check the encoding supported by mysql:

Show character set;

4. view the encoding format of the database:

Show create database testdb;

5. view the encoding settings of the database:

Mysql> show variables like 'character _ set _ % ';

Set names 'utf8 ';

It is equivalent to the following three commands:

SET character_set_client = utf8;

SET character_set_results = utf8;

SET character_set_connection = utf8;

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.