sudo apt-get install mysql-server mysql-client
而后按照提示输入
service mysql start
service mysql stop
service mysql restart
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
将bind-address=127.0.0.1注释
grant all privileges on *.* to 'root'@'%' identified by 'mysql' with grant option; flush privileges;
mysql -uroot -p
回车后输入密码,当前设置的密码为mysql
quit或exit
查看版本:select version();
显示当前时间:select now();
mysql -hip地址 -uroot -p
create database 数据库名 charset=utf8;v
drop database 数据库名;
use 数据库名;
select database();
show tables;
auto_increment表示自动增加python
create table 表名(列及类型); 如: create table students( id int auto_increment primary key, sname varchar(10) not null );
alter table 表名 add|change|drop 列名 类型;
如:
alter table students add birthday datetime;
drop table 表名;
desc 表名;
rename table 原表名 to 新表名;
show create table '表名';
select * from 表名
全列插入:insert into 表名 values(...)
缺省插入:insert into 表名(列1,...) values(值1,...)
同时插入多条数据:insert into 表名 values(...),(...)...;
或insert into 表名(列1,...) values(值1,...),(值1,...)...;
update 表名 set 列1=值1,... where 条
delete from 表名 where 条件
alter table students add isdelete bit default 0;
若是须要删除则
update students isdelete=1 where ...;
sudo -s
cd /var/lib/mysql
ysqldump –uroot –p 数据库名 > ~/Desktop/备份文件.sql;
按提示输入mysql的密码
mysql -uroot –p 数据库名 < ~/Desktop/备份文件.sql
根据提示输入mysql密码
select * from 表名;
select distinct gender from students;
select * from 表名 where 条件;
select * from students where id>3;
select * from subjects where id<=4;
select * from students where sname!='黄蓉';
select * from students where isdelete=0;
select * from students where id>3 and gender=0;
select * from students where id<4 or isdelete=0;
select * from students where sname like '黄%';
select * from students where sname like '黄_';
select * from students where sname like '黄%' or sname like '%靖%';
select * from students where id in(1,3,8);
select * from students where id between 3 and 8;
select * from students where id between 3 and 8 and gender=1;
select * from students where hometown is null;
select * from students where hometown is not null;
select * from students where hometown is not null and gender=0;
select count(*) from students;
select max(id) from students where gender=0;
select min(id) from students where isdelete=0;
select sum(id) from students where gender=1;
select avg(id) from students where isdelete=0 and gender=0;
select 列1,列2,聚合... from 表名 group by 列1,列2,列3...
select gender as 性别,count(*) from students group by gender;
select hometown as 家乡,count(*) from students group by hometown;
select 列1,列2,聚合... from 表名 group by 列1,列2,列3... having 列1,...聚合...
方案一 select count(*) from students where gender=1; ----------------------------------- 方案二: select gender as 性别,count(*) from students group by gender having gender=1;
select * from 表名 order by 列1 asc|desc,列2 asc|desc,...
select * from students where gender=1 and isdelete=0 order by id desc;
select * from subject where isdelete=0 order by stitle;
select * from 表名 limit start,count
select * from students where isdelete=0 limit (n-1)*m,m
select distinct * from 表名 where .... group by ... having ... order by ... limit star,count