Mysql 数据库基础之数据库和表管理

一、实例表创建

创建数据库

create database school;
use school;

1、班级表

create table class(
    id int(11) not null auto_increment,
    name varchar(10) default null,
    primary key (id)
) engine=innoDB charset=utf8mb4

2、老师表

create table teacher(
    id int(11) not null auto_increment,
    name varchar(10) default null,
    phone char(11) default null,
    primary key (id)
);

3、学生表

create table student(
    id int auto_increment primary key,
    name varchar (10),
    age int(3),
    class_id int,
    foreign key(class_id) references class(id)
    );

4、多对多关系表

create table class2teacher(
    id int auto_increment primary key,
    class_id int,
    teacher_id int,
    foreign key(teacher_id) references teacher(id),
    foreign key(class_id) references class(id)
);

实例表添加
class(班级表)

insert into class(name) values
("云计算1810"),
("云计算1901"),
("云计算1902");

teacher(老师表)

insert into teacher(name, age, phone) values
("晗哥", 18, "13733878989"),
("强哥", 28, "15633878989"),
("磊哥", 30, "13933878989"),
("闫老师", 18, "13633878989");

student(学生表)

insert into student(name,age) values
("黛玉", 18, 3), ("钦文", 19, 3),("马邦德", 30, 1),
("九筒", 48, 1),("六子", 36, 2),("汤师爷", 18, 2),
("麻匪", 18,2),
("黛玉", 18,2);

class2teacher(班级到老师多对多关系表)

into class2teacher(class_id,teacher_id) values
(1,1),(1,2),(2,1),(2,2),(2,3),(3,1),(3,3);

单表查询

基础查询

select * from 表
select * from 表 where id > 2
select id,name,age as gg from 表 where id > 2

高级查询

a、条件
    select * from 表 where id > 1 and name != '王麻子' and age = 18;
 
    select * from 表 where id between 5 and 16;
 
    select * from 表 where id in (11,22,33)
    select * from 表 where id not in (11,22,33)
    select * from 表 where id in (select id from 表)
 
b、通配符
    select * from 表 where name like 'sha%'  - sha开头的所有(多个字符串)
    select * from 表 where name like 'shar_'  - sha开头的所有(一个字符)
 
c、限制
    select * from 表 limit 5;            - 获取前 5 行
    select * from 表 limit 0,2;          - 从第 1 行开始, 取出 2 行, 包含第 1 行
    select * from 表 limit 2 offset 0    - 从第 1 行开始, 取出 2 行, 包含第 1 行
 
d、排序
    select * from 表 order by 列 asc              - 根据 “列” 从小到大排列
    select * from 表 order by 列 desc             - 根据 “列” 从大到小排列
    select * from 表 order by 列1 desc,列2 asc    - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序
 
e、分组
    select age from 表 group by age
    select age,id from 表 group by age,id
    select age,id from 表  where id > 10 group by age,id order id desc
    select age,id,count(*),sum(age),max(age),min(age) from 表 group by age,id
 
    select age from 表 group by age having max(id) > 10
 
    特别的:group by 必须在where之后,order by之前
    
f、嵌套查询
select * from  (select name from t1 where age>18 and age < 25 order by id desc limit 2 ) as tt  order by id;

多表查询

微信图片_20190306215745.png

捕获.JPG

创建有外键的表

create table class(
    id int not null auto_increment primary key,
    class_name varchar(8),
    create_date DATE
);

create table teacher(
    id int not null auto_increment primary key,
    name varchar(12) not null,
    class_id int(2) not null,
    foreign key (class_id) references class(id) /*mysql 自动给外键起名字teacher_ibfk_1*/
    
    /*给外键命名*/
    /*constraint fk_class foreign key (class_id) references class(id)*/
);

增加数据

mysql> insert into class ( class_name, create_date ) values ( '云计算1805', '2018-05-16' );
mysql> insert into class ( class_name, create_date) values ( '云计算1806', '2018.06.16' );
mysql> insert into class ( class_name, create_date ) values ( '云计算1807', '2018_07_16' ),('云计算1808','20180816');
insert into teacher (name, class_id) values ('杨哥', 1);
insert into teacher (name, class_id) values ('强哥', 3);
insert into teacher (name, class_id) values ('磊哥', 02);

连表

/*无对应关系则不显示*/
select  A.class_name, B.name
from class as A, teacher as B
Where A.id = B.class_id
 
/* 内连接   无对应关系则不显示*/
select A.class_name, B.name
from class as A inner join teacher as B
on A.id = B.class_id
 
/* 左连接   左边的表(A)所有显示,如果右边的表(B)中无对应关系,则值为null*/
select A.class_name, B.name
from class as A left join teacher as B
on A.id = B.class_id
 
/* 右连接 右边的(B)表所有显示,如果左边的表(A)中无对应关系,则值为 NULL*/
select A.name, B.name
from class as A right join teacher as B
on A.id = B.class_id

Example

select a.class_name,b.name 
from class a,teacher b 
where a.id=b.class_id;

select a.class_name,b.name
from class a inner join teacher b    
on  a.id=b.class_id;

select a.class_name,b.name
from class a left join teacher b    
on  a.id=b.class_id;

select a.class_name,b.name
from class a right join teacher b    
on  a.id=b.class_id;

组合

/*组合,自动处理重合*/
select class_name
from class
union
select name
from teacher;

/*组合,不处理重合*/
select class_name
from class
union all
select name
from teacher;

拓展语句(可掌握)

select name 名字,age 年龄,count(*) as 总计 from student where id  !=7 group by name,age having age=18;
select * from student where id >1 and name != "六子" and age=18;
select * from student where name like '%玉';
select * from student where name like '_玉';
select * from student where name like '__玉';
select * from student where name in ("黛玉","麻匪");
select * from student where id not in (3,5,8,2);
select * from student limit 0,6;
select * from student limit 1,2;
select * from student limit 2,6;
select * from student limit 5 offset 2;
select * from student limit 5 offset 1;
select * from student order by age;
select * from student order by age desc;
select * from student order by age desc,class_id;
select * from student order by name ,class_id desc;
select * from student order by age ,class_id desc;
select age,id from student group by age,id;
select name,age,id from student group by name,age,id;
select name 姓名,age as 年龄 from student group by name,age;
select name 姓名,age as 年龄,count(age) 计数 from student group by name,age;
select name 姓名,age as 年龄,count(age) 计数 from student group by age,name;
select age,id,count(*),sum(age),max(age),min(age) from student group by age,id;
select age from student group by age having max(id) > 10;
select age from student group by age having max(id) >10;
select name,age,count(*) from student where id !=7 group by name,age having age=18;
select name from student where age>18 and age < 25 order by id desc limit 2;
select * from  (select name from student where age>18 and age < 25 order by id desc limit 2 ) as tt order by id;;

连表查询拓展练习

select * from class a,student b where a.id=b.id;
select * from class a,student b where a.id=b.class_id;
select a.name,b.name from class a,student b where a.id=b.class_id;
select a.name 班级,b.name 姓名 from class a,student b where a.id=b.class_id;
select * from class a inner join student b on a.id=b.class_id;
常用
select a.name,b.name from class a inner join student b on a.id=b.class_id;
select * from class a left join student b on a.id=b.class_id;
select a.name,b.name from class a left join student b on a.id=b.class_id;
select b.name from class a left join student b on a.id=b.class_id;
select * from class a left join student b on a.id=b.class_id;
select * from class a right join student b on a.id=b.class_id;
select * from student a right join class b on a.class_id=b.id;
/*左连接,不是外键,没有对应关系*/
insert into class (name) values ("云计算1903");
select * from class left join student on class.id=student.class_id;
+----+---------------+------+-----------+------+----------+
| id | name          | id   | name      | age  | class_id |
+----+---------------+------+-----------+------+----------+
|  1 | 云计算1810    |    3 | 马邦德    |   30 |        1 |
|  1 | 云计算1810    |    4 | 九筒      |   48 |        1 |
|  2 | 云计算1901    |    5 | 六子      |   36 |        2 |
|  2 | 云计算1901    |    6 | 汤师爷    |   18 |        2 |
|  2 | 云计算1901    |    7 | 麻匪      |   18 |        2 |
|  2 | 云计算1901    |    8 | 黛玉      |   18 |        2 |
|  3 | 云计算1902    |    1 | 黛玉      |   18 |        3 |
|  3 | 云计算1902    |    2 | 钦文      |   19 |        3 |
|  4 | 云计算1903    | NULL | NULL      | NULL |     NULL |
+----+---------------+------+-----------+------+----------+

select * from student a right join class b on a.class_id=b.id;
select * from student where name like '_玉';
select * from student where name like '__爷';
/*添加一个列的唯一属性*/               列 原属性 唯一属性
alter table student2 modify column email varchar(32) unique;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,961评论 5 473
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,444评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,009评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,082评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,101评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,271评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,738评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,395评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,539评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,434评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,481评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,160评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,749评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,816评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,038评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,548评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,140评论 2 341

推荐阅读更多精彩内容

  • 笔记: 一、聚合函数:计数 最大值 最小值 平均数 求和 1.计数 COUNT() 忽略NULL值 方式1:COU...
    凤之鸠阅读 5,147评论 0 1
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,222评论 0 7
  • 你的晚安是一根灯绳,‘’咔嗒‘’一声,熄灭整个城市的灯,夜晚才开始。 好梦
    随波逐流不落后阅读 133评论 0 0
  • 最近工作调整,量加大了很多,且时间集中。还有,上一棒没交接就撤了……反正吧,种种导致这两天工作量极度饱满,今天上午...
    无花果1030阅读 73评论 0 0
  • 今天打卡第二十天,特训营一共21天。今天应该进入倒计时了吧?这二十天,感觉自己的主要功能就是反复看教学视频跟着操练...
    谢雅茹阅读 1,882评论 0 0