mysql basis of common commands

Enter the mysql command interface

mysql -uroot -p;

Show all databases

show databases;

Delete the database db1

drop database db1;

Create a database db1

create database db1;

Enter the database db1

use db1;

All tables show

show tables;

Create a table t1

Create  Table T1 ( 
ID int  Primary  Key AUTO_INCREMENT, 
name VARCHAR ( 30 ) Not  null Comment ' name ' , 
Age int ( . 11 ) Not  null Comment ' aged ' 
);

To add an index field name

ALTER  Table T1 the Add  index name_index (name ( 30 )) Comment ' names separate index ' ;

Add a unique index to the age field

ALTER  Table T1 the Add  UNIQUE  index age_unique_index (Age) Comment ' Age unique index ' ;

View sql statement to create table t1

show create table t1\G;

Delete the name, age index on two fields

alter table t1 drop index name_index;
alter table t1 drop index age_unique_index;

To name, age adding two fields combined index

ALTER  Table T1 the Add  index multi_index (name ( 30 ), Age) Comment ' combination index ' ;

Inserting data into a table t1

insert into table t1 (name,age) values('name1',1);

The name field id update table t1 is a data name2

update t1 set name='name2' where id=1;

According to id delete a data table t1

delete from t1 where id=1;

The id data query table t1

select * from t1 where id=1;

解释查询语句使用了什么索引,加上explain

explain select * from t1 where name='name2'\G;

在id字段后加入新的字段nickname

alter table t1 add column nickname varchar(30) not null comment '昵称' after id;

更改nickname字段为nickname2

alter table t1 change column nickname nickname2 varchar(30) not null comment '昵称'

删除nickname2字段

alter table drop column nickname2;

删除t1表

drop table t1;

这些操作能基本的使用mysql,但是想要更好的使用还需要学习集合函数查询、多表查询、索引。

 

Guess you like

Origin www.cnblogs.com/darkclouds/p/11706122.html