Mysql基础命令整理(增删查改)

基础的增删查改命令整理

敲命令的时候发现不同目标下mysql命令还不一样

有必要整理一下

启动mysql服务:net start mysql

停止mysql服务:net stop mysql

登录mysql:mysql -h localhost -u root -p    其中-h 和localhost可省略,而登录到其他主机的mysql时可换localhost地址,root是用户名

(1)增

增加一个数据库database       create database database_name;

增加一个表table       create table table_name(id int not null,name varchar(10),age double(16,3),love float,primary key(id));

其中可在类型后面加not null,auto_increment(自增)参数,primary key是主键

Mysql基础命令整理(增删查改)

增加(插入)一列column       alter table table_name add column column_name int;

Mysql基础命令整理(增删查改)

增加(插入)一行数据       insert into table_name (id,name,age,love) values (1,"white cat",24,3.5);

Mysql基础命令整理(增删查改)

增加(插入)某个数据       insert into table_name (id) values (2);

Mysql基础命令整理(增删查改)

(2)删

删除一个数据库       drop database database_name;

删除一个表       drop table table_name;

删除一个表中的所有数据       delete from table_name;

删除一列       alter table table_name drop column column_name;

删除一行数据       delete from table_name where id=2;

删除某个数据       update table_name set age=null where id=1;

(3)查

查询数据库       show databases;       用use database_name;进入某数据库才能查表

查询数据库的表       show tables;

Mysql基础命令整理(增删查改)

查询表结构       desc table_name;

Mysql基础命令整理(增删查改)

查询表中的数据       select * from table_name;

Mysql基础命令整理(增删查改)

按照某条件查询数据       select column1,column2 from table_name where id=1;

Mysql基础命令整理(增删查改)

(4)改

改表名字       alter table table_name rename new_name;

改列名字       alter table table_name change old_column new_column int;

改列变量类型       alter table table_name modify column column_name double(16,4) not null;

改行中数据       update table_name set age=18 where id=1;

进阶命令:

查询后为字段重命名as      select age as my_age from test;

模糊查询like+‘%‘匹配多个字符+‘_‘匹配一个字符       

                                          select name as my_name from test where name like ‘whi%‘;

                                          select name as my_name from test where name like ‘whiteca_‘;

排序order by  以某个字段为主进行排序,升序 asc (asc可以不写),降序desc       

                                         select * from test order by id asc;

限制显示数据数量limit ,limit 只接一个数字n时表示显示前面n行       

                                         select * from test limit 5;

                                         limit 接两个数字m,n时表示显示第m行之后的n行       

                                         select * from student limit 2,4;

聚合函数       min,max,avg,sum,round(四舍五入),count(计数)

分组查询  group by    筛选条件使用having,having后接条件必须是select后存在的字段

                                         select age,count(age) from details group by age having age>30;

                                  以age为组统计每个age的人数最后筛选出age大于30的

END

相关推荐