Mysql的基础常用命令

数据库操作:

查看所有数据库

show databases;

查看当前使用的数据库

select database();

使用数据库

use 数据库名;

创建数据库

create database 数据库名 charset=utf8;

删除数据库-慎重

drop database 数据库名

 

表操作

添加表

create table 表名(
   字段名称  数据类型   可选的约束条件,
   column1  datatype   contrai,
   column2  datatype   
   ......
   columnN  datatype,
   -- 主键说明可以放在字段中单独说明 也可以放在最后统一说明
   primary key(one or more columns)
);

-- 例:
create table students(
   id int unsigned primary key auto_increment not null,
   name varchar(20) default '',
   age int  unsigned default 0,
   score decimal(3,2),
   cls_id int unsigend default 0
);

修改表

修改表-添加字段

alter table 表名 add 列名 类型;
-- 例:
alter table students add birthday datetime;

修改表-修改字段:重命名版

在表中已有字段 但是字段名不满足要求 类型或约束满足或者不满足均可。

alter table 表名 change 原名 新名  类型及约束;
-- 例:
alter table students change birthday birth datetime not null;

修改表-修改字段:不重命名版

在表中已有字段 并且字段名也满足要求 但是类型或约束不满足要求

alter table 表名  modify 列名 类型及约束;
-- 例:
alter table students modify birth date not null;

修改表-删除字段

当表中多出一个字段 已经不再需要的时候

alter table 表名 drop 列名;
-- 例:
alter table students drop birthday

删除表

drop table 表名;
-- 例:
drop table students;

查看表

show create table 表名;
-- 例:
show create table classes;

查看表结构

desc 表名;
-- 例:
desc classes;

 

数据操作

数据添加

-- 全插入
insert into 表名 values (...)
-- 例:
insert into students values(1,’老王‘,1,'河南','2016-10-06'); 


-- 指定字段插入
insert into 表名 (列1,...) values(值1,...)
-- 例:
insert into students(name,hometown,birthday) values('小王','桃花岛','2016-11-18');


-- 插入多条数据
insert into 表名(列1,...) values(值1,...),(值1,...)...;
-- 例:
insert into students(name) values('隔壁老王'),('隔壁'),('老王');

数据修改

update 表名 set 列1=值1,列2=值2... where 条件
-- 例:
update students set gender=0,hometown='北京' where id=5;

数据删除

delete from 表名 where 条件
-- 例:
delete from students where id=5;

数据查询

as 		                     起别名
distinct					 去重
where						 条件查询
=,>,>=,<,<=,<>				 比较运算符
and,or,not					 逻辑运算符
like						 模糊查询
in							 范围查询
is null						 空判断
order by					 排序
limit						 分页
group by					 分组
count,max,min,sum,avg		 聚合操作

-- 列:
select name as 姓名, age as 年龄 where id in(2,4,6,8,10,12,14,16,18,20) order by age desc limte 2,2;

select gender,count(*) from students group by gender having count(*)>2;

表的关联操作

一对多建表

create table goods(
    id int primary key auto_increment not null,
    name varchar(20) default '',
    price decimal(5,2),
    cate_id int unsigned,
    brand_id int unsigned,
    is_show bit default 1,
    is_saleoff bit default 0,
    foreign key(cate_id) references goods_cates(id),
    foreign key(brand_id) references goods_brands(id)
);

多对多建表

create table hero(
    id int primary key auto_increment not null,
    name varchar(20) default '',
    price decimal(5,2),
);

create table arms(
    id int primary key auto_increment not null,
    name varchar(20) default '',
    price decimal(5,2),
    number int 
);

create table hero_arms(
    id int primary key auto_increment not null,
    h_id int,
    a_id int,
    foreign key(h_id) references hero(id),
    foreign key(a_id) references arms(id)
  
);

自关联建表

create table areas(
    aid int primary key,
    atitle varchar(20),
    pid int
);

关联查询

-- 一对多关联查询
select * from students inner join classes on students.cls_id = classes.id;

-- 多对多关联查询
select * from hero h inner join hero_arms ha on h.id=ha.h_id inner join arms a on a.id=ha.a_id;

-- 自关联查询
select city.* from areas as city inner join areas as province on city.pid=province.aid
where province.atitle='河南省';

 

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值