您的位置:首页 > 数据库 > MySQL

Mysql数据库常用命令

2015-12-13 22:58 786 查看
一、mysql连接、创建、建表、录入和删除!

//查看所有数据库

show databases;

//使用数据库(相当于打开文件夹)

use mysql;

//显示数据库中的所有的表

show tables;

//创建c58数据库,编码为utf8

create database c58 charset utf8;

//删除c57的数据库

drop database c57;

//创建表(先use库)

use c58;

//创建学生表,包含sid列名,类型为数值中的tinyint

create table stu(sid tinyint);

//录入数据库

insert into stu set sid=1;

insert into stu set sid=2;

//查询表的数据

select * from stu;

+——+

| sid |

+——+

| 1 |

| 2 |

+——+

//删除表

drop table stu;

//创建学生表(decimal(5,2)总共5位数字,2位小数)

create table stu(sid tinyint,money decimal(5,2));

//插入数据

insert into stu set sid=1,money=2000;

insert into stu set sid=1,money=10;

insert into stu set sid=1,money=100.235;

//tinyint(1),并不代表能存1位数字

drop table stu;

create table stu(sid tinyint(1),money decimal(5,2));

insert into stu set sid=100;

select * from stu;

+——+——-+

| sid | money |

+——+——-+

| 100 | NULL |

+——+——-+

//字符串类型 char(5),只能存5个字符

drop table stu;

create table stu(sid tinyint,sname char(5));

//录入数据

insert into stu set sid=1,sname=”;

insert into stu set sid=1,sname=”;

//char和varchar区别

//查询速度,char要优于varchar

//存储大小,varchar要优于char

//

//char(5) 存一个字符串,那么也是占5个,定长

//varchar(5) 存一个字符,就占一个,变长

//前导零 zerofill 也会把类型变为正数范围

//tinyint(10) 如果存1位数字,那么会补充9个零

create table stu(sid tinyint(10) zerofill);

insert into stu set sid=1;

select * from stu;

+————+

| sid |

+————+

| 0000000001 |

| 0000000002 |

| 0000000200 |

+————+

//非负 unsigned,把类型变为正数范围

create table stu(sid tinyint unsigned);

//enum 枚举(类似于单选)

create table stu(sid tinyint unsigned,sname char(20),sex enum(‘男’,’女’));

//录入数据

insert into stu set sid=1,sname=’小明’,sex=’女’;

//set (类似于多选)

create table stu(

sid tinyint unsigned,

sname char(20),

sex enum(‘男’,’女’),

hobby set(‘篮球’,’排球’,’乒乓球’,’PHP’)

);

insert into stu set sid=1,sname=’伊’,sex=1,hobby=’篮球,排球,PHP’;

二、mysql复制、查询

//查看表结构

desc stu;

//default 字段修饰,默认值

//not null 和 default 和结合起来使用的

create table user(

uid smallint unsigned not null default 0,

name char(20) not null default ”

);

//unique 非重

create table user(

uid int unsigned,

username char(20) not null default ” unique,

password char(32) not null default ”

)

//主键 primary key

//自增 auto_increment

create table arc(

aid int unsigned primary key auto_increment,

title char(120) not null default ”,

content text,

click smallint unsigned not null default 0

);

//删除aid为2的数据

delete from arc where aid=2;

//复制user表的表结构为user_bak

//不复制内容

create table user_bak like user;

//把user表的数据复制到user_bak

insert into user_bak select * from user;

//创建表同时复制数据

create table user_bak1 select * from user;

//查询所有

select * from arc;

//查询aid

select aid from arc;

//查询aid,title

select aid,title from arc;

//别名 as

select aid,title as t from arc;

//where 代表条件

select * from arc where aid>1 and click>100;

//concat连接字符串

select concat(title,’-‘,click) as c from arc;

+————————————————–+

| c |

+————————————————–+

| 今天有雾霾,北京还能不能待了-10000 |

| 今天有雾霾-1000 |

| 今天有雾霾-1000 |

+————————————————–+

//查询结构与自身比较

select click,click>5000 as c from arc;

+——-+—+

| click | c |

+——-+—+

| 10000 | 1 |

| 1000 | 0 |

| 1000 | 0 |

+——-+—+

//把click重复的值去掉

select distinct(click) from arc;

//查找set类型

create table stu(

sid int unsigned primary key auto_increment,

sname char(20) not null default ”,

hobby set(‘篮球’,’足球’,’PHP’,’JS’)

);

select * from stu;

+—–+——–+————+

| sid | sname | hobby |

+—–+——–+————+

| 1 | 小明 | 篮球,PHP |

| 2 | 小王 | PHP |

| 3 | 小红 | PHP,JS |

+—–+——–+————+

//查找喜欢篮球的同学

//1.find_in_set() 不推荐

select * from stu where find_in_set(‘篮球’,hobby);

+—–+——–+————+

| sid | sname | hobby |

+—–+——–+————+

| 1 | 小明 | 篮球,PHP |

| 4 | 瑶瑶 | 篮球 |

+—–+——–+————+

//2. &1 不推荐

select * from stu where hobby &1;

+—–+——–+————+

| sid | sname | hobby |

+—–+——–+————+

| 1 | 小明 | 篮球,PHP |

| 4 | 瑶瑶 | 篮球 |

+—–+——–+————+

//3. like 模糊匹配 推荐

select * from stu where hobby like ‘%篮球%’;

+—–+——–+————+

| sid | sname | hobby |

+—–+——–+————+

| 1 | 小明 | 篮球,PHP |

| 4 | 瑶瑶 | 篮球 |

+—–+——–+————+

//查询uid 为null的数据

select * from user where uid is null;

//if使用

select click,if(click>5000,’多’,’少’) as i from arc;

+——-+—–+

| click | i |

+——-+—–+

| 10000 | 多 |

| 1000 | 少 |

| 1000 | 少 |

+——-+—–+

//order 排序

//desc 降序 asc 升序

//创建百度表

create table baidu(

bid int unsigned primary key auto_increment,

name char(100) not null default ”,

sort smallint unsigned not null default 0

);

insert into baidu set name=’腾讯搜索’,sort=98;

insert into baidu set name=’360搜索’,sort=100;

insert into baidu set name=’google搜索’,sort=99;

//修改bid为1的数据中的sort变为101

update baidu set sort=101 where bid=1;

//排序,升序

select * from baidu order by sort asc;

//降序

select * from baidu order by sort desc;

+—–+————–+——+

| bid | name | sort |

+—–+————–+——+

| 2 | 360搜索 | 100 |

| 1 | 腾讯搜索 | 101 |

| 3 | google搜索 | 200 |

+—–+————–+——+

//limit 截取

//limit 1 从开始截取1条

select * from baidu limit 1;[]

//limit 1,2 从1开始截取2条

select * from baidu limit 1,2;

//between 101 and 200

//查找101到200之间的值,包括101和200

select * from baidu where sort between 101 and 200;

+—–+————–+——+

| bid | name | sort |

+—–+————–+——+

| 1 | 腾讯搜索 | 101 |

| 3 | google搜索 | 200 |

| 4 | | 150 |

+—–+————–+——+

//查询100或者150的数据

select * from baidu where sort in(100,150);

+—–+———–+——+

| bid | name | sort |

+—–+———–+——+

| 2 | 360搜索 | 100 |

| 4 | | 150 |

+—–+———–+——+

//查找以“小”开头的同学

select * from stu where sname like “小%”;

//查找包含“小”的同学

select * from stu where sname like “%小%”;

//查找以“小”结尾的同学

select * from stu where sname like “%小”;

//以小开头后面跟一个字符的同学

select * from stu where sname like ‘小_’;

//把sname从左边截取一位字符

select left(sname,1) from stu;

//从第二个位置截取一个字符串

select mid(sname,2,1) from stu;

//把sname从右边截取一位字符

select right(sname,1) from stu;

//随机得到一条数据

select * from baidu order by rand() limit 1;

//设置字符集为utf8

//客户端是什么编码就告诉mysql设置什么编码

set names utf8;

//查看字符集变量

show variables like “%character%”;

//要查看的选项

character_set_client | gbk //客户端

character_set_connection | gbk //连接端

character_set_results | gbk //结果端

//随便找一张表录入汉字数据

insert into arc set title=’今天是得跳舞’;

//查看结果

select * from arc;

三、mysql一次多个录入内容、修改添加类型、统计分组

//一次录入多条数据;

insert into stu (sname,hobby) values (‘小绿’,’PHP’),(‘小黄’,’JS’);

//清空表的所有数据,谨慎操作!;

truncate stu;

//sid为1的记录,如果没有就添加,如果有就替换;

replace into stu (sid,sname,hobby) values (1,’小粉’,’PHP’);

//修改(where条件可以不加,可以修改全部数据,谨慎操作!);

update stu set sname=’小白’ where sid=1;

//删除(where条件可以不加,可以删除全部数据,谨慎操作!);

delete from stu where sid=4;

//把stu表改名为student

alter table stu rename student;

//change 改名同时改类型

//把sname改成name,然后类型改为char(15)

alter table student change sname name char(15) not null default ”;

//modify 改类型

alter table student modify name char(30) not null default ” first;

//修改name为char(30)并且放在sid的后面

alter table student modify name char(30) not null default ” after sid;

//追加字段

//给student表追加sex字段,并且在name的后面

alter table student add sex enum(‘男’,’女’) not null default ‘男’ after name;

//删除字段

//删除hobby字段

alter table student drop hobby;

//(1)增加stu表的主键

alter table stu add primary key(sid);

//(2)增加自增

alter table stu modify sid int unsigned auto_increment;

//(1)先删自增

alter table stu modify sid int unsigned;

//(2)删除主键

alter table stu drop primary key;

//date类型的使用方式

create table member(

mid int unsigned primary key auto_increment,

name char(20) not null default ”,

birthday date

);

//录入数据

insert into member (name,birthday) values (‘小红’,’1990-1-1’),(‘小绿’,19901001);

//输出现在的时间

select now();

+———————+

| now() |

+———————+

| 2015-12-10 12:05:27 |

+———————+

//统计会员总数

select count(*) from member;

select * from member;

+—–+——–+————+——-+

| mid | name | birthday | click |

+—–+——–+————+——-+

| 1 | 小红 | 1990-01-01 | 200 |

| 2 | 小绿 | 1990-10-01 | 0 |

+—–+——–+————+——-+

//查找点击次数最小的数据

select min(click) from member;

//查找点击次数最大的数据

select max(click) from member;

//取得click总和

select sum(click) from member;

//取得平均数

select avg(click) from member;

//按照性别分组,筛选性别为女的组

//group by 和 having组合使用的

select * from member group by sex having sex=’女’;

四、关联数据,多个查询分组统计!

//—————-1:1———–

//关联字段在那张表都可以

城市表 city

cid name

1 北京

2 上海

3 杭州

区号表 code

coid num cid

1 020 2

2 010 1

3 0571 3

//—————-1:N————–

//关联字段放在多的一方

//多的保存少的

分类表 category

cid cname

1 新闻

2 娱乐

3 体育

文章表 arc

aid title cid

1 虚拟现实2016年将首先在游戏中实现 1

2 ab怀孕了 2

3 科比下个赛季将要退役了 3

4 马唱了一首歌 2

5 在纳斯达克上市了 1

//分类表

create table cate(

cid int unsigned primary key auto_increment,

cname char(20) not null default ”

);

//文章表

create table arc(

aid int unsigned primary key auto_increment,

title char(120) not null default ”,

cid int unsigned not null default 0

);

//笛卡尔积(无意义的)

select * from arc,cate;

+—–+————————————————+—–+—–+——–+

| aid | title | cid | cid | cname |

+—–+————————————————+—–+—–+——–+

| 1 | 虚拟现实2016年将首先在游戏中实现 | 1 | 1 | 新闻 |

| 1 | 虚拟现实2016年将首先在游戏中实现 | 1 | 2 | 娱乐 |

| 1 | 虚拟现实2016年将首先在游戏中实现 | 1 | 3 | 体育 |

| 2 | ab怀孕了 | 2 | 1 | 新闻 |

| 2 | ab怀孕了 | 2 | 2 | 娱乐 |

| 2 | ab怀孕了 | 2 | 3 | 体育 |

| 3 | 科比下个赛季将要退役了 | 3 | 1 | 新闻 |

| 3 | 科比下个赛季将要退役了 | 3 | 2 | 娱乐 |

| 3 | 科比下个赛季将要退役了 | 3 | 3 | 体育 |

| 4 | 马唱了一首歌 | 2 | 1 | 新闻 |

| 4 | 马唱了一首歌 | 2 | 2 | 娱乐 |

| 4 | 马唱了一首歌 | 2 | 3 | 体育 |

| 5 | 在纳斯达克上市了 | 1 | 1 | 新闻 |

| 5 | 在纳斯达克上市了 | 1 | 2 | 娱乐 |

| 5 | 在纳斯达克上市了 | 1 | 3 | 体育 |

+—–+————————————————+—–+—–+——–+

//加完限制条的 笛卡尔积(有意义的)

select * from arc as a,cate as c where a.cid=c.cid;

+—–+————————————————+—–+—–+——–+

| aid | title | cid | cid | cname |

+—–+————————————————+—–+—–+——–+

| 1 | 虚拟现实2016年将首先在游戏中实现 | 1 | 1 | 新闻 |

| 2 | ab怀孕了 | 2 | 2 | 娱乐 |

| 3 | 科比下个赛季将要退役了 | 3 | 3 | 体育 |

| 4 | 马唱了一首歌 | 2 | 2 | 娱乐 |

| 5 | 在纳斯达克上市了 | 1 | 1 | 新闻 |

+—–+————————————————+—–+—–+——–+

//查询指定字段

select aid,title,cname from arc as a,cate as c where a.cid=c.cid;

+—–+————————————————+——–+

| aid | title | cname |

+—–+————————————————+——–+

| 1 | 虚拟现实2016年将首先在游戏中实现 | 新闻 |

| 2 | ab怀孕了 | 娱乐 |

| 3 | 科比下个赛季将要退役了 | 体育 |

| 4 | 马唱了一首歌 | 娱乐 |

| 5 | 在纳斯达克上市了 | 新闻 |

+—–+————————————————+——–+

//内部连接 inner join(和上面的写法比起来,推荐写法)

//join 关联

//on 关联的条件

select * from arc as a join cate as c on a.cid=c.cid;

+—–+————————————————+—–+—–+——–+

| aid | title | cid | cid | cname |

+—–+————————————————+—–+—–+——–+

| 1 | 虚拟现实2016年将首先在游戏中实现 | 1 | 1 | 新闻 |

| 2 | ab怀孕了 | 2 | 2 | 娱乐 |

| 3 | 科比下个赛季将要退役了 | 3 | 3 | 体育 |

| 4 | 马唱了一首歌 | 2 | 2 | 娱乐 |

| 5 | 在纳斯达克上市了 | 1 | 1 | 新闻 |

+—–+————————————————+—–+—–+——–+

//关联之后的where的使用

select * from arc as a join cate as c on a.cid=c.cid where a.cid=2;

+—–+————————–+—–+—–+——–+

| aid | title | cid | cid | cname |

+—–+————————–+—–+—–+——–+

| 2 | ab怀孕了 | 2 | 2 | 娱乐 |

| 4 | 马唱了一首歌 | 2 | 2 | 娱乐 |

+—–+————————–+—–+—–+——–+

//右连接 right join,偏向右边的表,无论条件是否成立,数据都会出来

select * from arc as a right join cate as c on a.cid=c.cid;

+——+————————————————+——+—–+——–+

| aid | title | cid | cid | cname |

+——+————————————————+——+—–+——–+

| 1 | 虚拟现实2016年将首先在游戏中实现 | 1 | 1 | 新闻 |

| 2 | ab怀孕了 | 2 | 2 | 娱乐 |

| 3 | 科比下个赛季将要退役了 | 3 | 3 | 体育 |

| 4 | 马唱了一首歌 | 2 | 2 | 娱乐 |

| 5 | 在纳斯达克上市了 | 1 | 1 | 新闻 |

| NULL | NULL | NULL | 4 | 军事 |

+——+————————————————+——+—–+——–+

//查询没有文章的分类

select * from arc as a right join cate as c on a.cid=c.cid where aid is null;

+——+——-+——+—–+——–+

| aid | title | cid | cid | cname |

+——+——-+——+—–+——–+

| NULL | NULL | NULL | 4 | 军事 |

+——+——-+——+—–+——–+

//统计每个分类下面的文章总数

select cname,count(*) from cate as c join arc as a on a.cid=c.cid group by cname;

+——–+———-+

| cname | count(*) |

+——–+———-+

| 体育 | 1 |

| 娱乐 | 2 |

| 新闻 | 2 |

+——–+———-+
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: