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