码迷,mamicode.com
首页 > 数据库 > 详细

mysql数据库中常用命令

时间:2021-06-04 18:53:42      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:就是   l数据库   删除   auto   inner   学生   values   not   drop   

创建表:
create table people(
id int primary key auto_increment not null,
name varchar(10) not null,
age int default 18
);

创建学生表:

create table students(
id int primary key auto_increment not null,
name varchar(10) not null,
age int default 18,
height decimal(5,2),
gender varchar(10),
cls_id int,
s_delete int
);

添加数据:

insert into students values
(0,‘小明‘,18,180.00,2,1,0),
(0,‘小月月‘,18,180.00,2,2,1),
(0,‘彭于晏‘,29,185.00,1,1,0),
(0,‘刘德华‘,59,175.00,1,2,1),
(0,‘黄蓉‘,38,160.00,2,1,0),
(0,‘凤姐‘,28,150.00,4,2,1),
(0,‘王祖贤‘,18,172.00,2,1,1),
(0,‘周杰伦‘,36,NULL,1,1,0),
(0,‘程坤‘,27,181.00,1,2,0),
(0,‘刘亦菲‘,25,166.00,2,2,0),
(0,‘金星‘,33,162.00,3,3,1),
(0,‘静香‘,12,180.00,2,4,0),
(0,‘郭靖‘,12,170.00,1,4,0),
(0,‘周杰‘,34,176.00,2,5,0);

 

添加字段:
  Alter table people add home varchar(20);
修改字段名:
  Alter table people change home jia varchar(20);

修改字段类型:
  Alter table people modify jia varchar(40) not null;

删除字段:
  Alter table people drop jia;

删除表:
  Drop table people;

添加数据:
  insert into people values (1,‘fan‘,32);

添加多条数据:
  insert into people values (3,‘fan3‘,32),(4,‘fan4‘,40),(5,‘fan5‘,50);
  insert into people (name) values (‘fan3‘),(‘fan4‘);

修改id=3的name字段为pp3
  updata people set name=‘pp3‘ where id=3;

删除id=3的数据:
  delete from people where id=3;

查询所有:
  select * from people;


查询结果name是姓名的:
  select name as ‘姓名‘ from 表名;
  select name as ‘姓名‘ from students;


条件查询:
  查询id(2,4,6,8,10)的数据信息:
  select * from students where id in (2,4,6,8,10);
  查询id>5或者name=‘小月月‘的数据信息:
  select *from 表名 where id>5 or name=‘小月月‘;
  select *from students where id>5 or name=‘小月月‘;
模糊查询:
  (name中含有‘菲’的):
  select * from 表名 where name like ‘%菲%‘;
  (name中以‘菲’结尾的):
  select * from 表名 where name like ‘%菲‘;
  (name中以‘菲’开头的):
  select * from 表名 where name like ‘菲%‘;

分页:(从第8条数据开始)
  select * from 表名 limit 7,3;
  select * from students limit 7,3;

一页3条数据查看第4页的数据:(n-1*页大小)
  select * from 表名 limit 9,3
按年龄分组:
  select age from 表名 group by age;
按性别分组:
  select gender from 表名 group by gender;
  select gender,group_concat(name) from 表名 group by gender;
  select gender,group_concat(age) from 表名 group by gender;

聚合函数:
  select gender,sum(age) feom feom 表名 group by gender;

  select id as ‘编号‘, name as ‘姓名‘ from 表名 where id>5 order by age limit 2,2;

  select id as ‘编号‘, name as ‘姓名‘, age from 表名 where id>5 order by age limit 2,2;


# 外键的作用:约束多表中的数据必须是在主表中存在的

# 公司里一般不用外键,公司里常使用的是逻辑外键

# 所谓逻辑外键就是一个普通的字段(类型为int)

# 物理外键:就是使用Foreign Key来约束过的字段

# 物理外键和逻辑外键的不同:ORM来查的时候必须使用物理外键

# 表一 (inner,left,right) join 表二 on 表一.id=表二.外键id
  select s.name,c.name from students s inner join classes c in s.cls_id=c.id where s.name=‘小明‘
  select * from students s inner join classes c on s.cls_id=c.id;
  select * from students s left join classes c on s.cls_id=id;
  select * from students s right join classes c on s.cls_id=id;

 

mysql数据库中常用命令

标签:就是   l数据库   删除   auto   inner   学生   values   not   drop   

原文地址:https://www.cnblogs.com/98yf/p/14847128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!