二阶段Day17-mysql

一、数据库介绍

数据库 - 数据的仓库 - database
关系型数据库 - 数据持久化 - 管理数据 - 方便的检索数据
理论基础:集合论、关系代数
表象:用二维表存储数据

  • 行 - row - 记录
  • 列 - column - 字段 - 数据类型
  • 主键列 - primary key - 唯一标识一条记录
    有自己的编程语言:结构化查询语言 - SQL

DB - database
DBMS - database management system
DBA - database administrator
DBS - database system = DB + DBMS + DBA

关系型数据库的产品(数据库管理系统):

  1. Oracle
  2. MySQL ---> 服务器+客户端工具
    C/S应用模式 - Client / Server

IBM - DB2
微软 - SQL Server
PostgreSQL


Redis - 键值对数据库
MongoDB - 文档数据库
ElasticSearch/Solr - 搜索引擎

Windows系统

启动MySQL服务器

  1. Win+R ---> 运行
  2. services.msc ---> 服务
  3. MySQL57 ---> 启动

net start mysql57

通过客户端程序连接服务器
开始菜单 ---> MySQL ---> MySQL Client Program
图形化的客户端程序
Navicat for MySQL - 猫
SQLyog - 海豚
Toad for MySQL - 蟾蜍

二、简单的mysql

-- 关系型数据库
-- 关系代数 + 集合论
-- 用二维表组织数据(行和列)
-- 结构化查询语言(SQL)
-- SQL: Structured Query Language - 结构化查询语言 - 不区分大小写
-- 1. DDL: Data Definition Language - 数据定义语言 - create / drop / alter
-- 2. DML: Data Manipulation Language - 数据操作语言 - insert / delete /
update / select
-- 3. DCL: Data Control Language - 数据控制语言 - grant / revoke /
commit / rollback
-- 如果存在名为school的数据库就删除
drop database if exists school;
-- 创建school数据库并设置默认字符集为utf8
create database school default charset utf8;
-- 切换到school数据库的上下文环境
use school;
-- 如果存在名为tb_student的表就删除
drop table if exists tb_student;
-- 查看MySQL支持哪些数据类型
-- ? data types
-- ? int
-- ? varchar
-- 创建学生表tb_student
create table tb_student
(
stuid int not null comment '学号',
stuname varchar(31) not null comment '姓名',
stusex enum('男', '女') default '男' comment '性别',
stubirth date comment '出生日期',
stuaddr varchar(255) comment '家庭住址',
primary key (stuid)
);
-- 修改tb_student表的表结构添加一个新列
alter table tb_student add column stutel char(11) comment '联系电话';
-- 修改tb_student表的表结构删除一个列
alter table tb_student drop column stutel;
-- 修改tb_student表的表结构修改一个列
alter table tb_student change column stuname stuname varchar(20)
not null comment '姓名';
-- 如果存在名为tb_course的表就删除
drop table if exists tb_course;
-- 创建课程表tb_course
create table tb_course
(
cid int not null comment '课程编号',
cname varchar(31) not null comment '课程名称',
credit tinyint unsigned not null comment '学分',
cdate timestamp default now() comment '开课日期',
primary key (cid)
);
-- 录入完整的学生数据
insert into tb_student values
(1001, '骆昊', '男', '1980-11-28', '四川成都');
-- 指定列录入对应的数据
insert into tb_student (stuid, stuname, stubirth)
values (1002, '王大锤', '1990-2-2');
-- 一次性插入多条数据
insert into tb_student
(stuid, stuname, stusex)
values
(1003, '白元芳', default),
(1004, '白洁', '女'),
(1005, '狄仁杰', '男'),
(1006, '武则天', '女');
-- 录入课程数据
insert into tb_course
(cid, cname, credit)
values
(1111, 'Python程序设计', 3),
(2222, 'Web前端开发', 2),
(3333, 'Linux系统入门', 2),
(4444, '数据库理论和实践', 3),
(5555, '企业应用架构', 2);
-- 删除记录
delete from tb_student where stuid=3001;
delete from tb_student where stuid>1005;
delete from tb_student where stuid between 1003 and 1005;
-- 删全表
truncate table tb_student;
-- 更新记录
update tb_student set stubirth='1995-5-5' where stuname='白元芳';
update tb_student set stubirth='1996-6-6', stuaddr='河北保定'
where stuid=1004;
update tb_course set credit=4 where cid in (2222, 5555);
update tb_course set credit=2 where cid=2222 or cid=5555;
-- 查询记录
select * from tb_student;
select * from tb_course;
-- 投影
select stuid, stuname, stusex from tb_student;
-- 别名
select stuid as 学号, stuname as 姓名, stusex as 性别 from tb_student;
-- 筛选
select stuid, stuname, stusex from tb_student where stusex='男';
select stuname, stubirth from tb_student
where stubirth between '1990-1-1' and '1999-12-31';
select stuname, stubirth, stusex from tb_student
where stubirth between '1990-1-1' and '1999-12-31' and stusex='男';
select stuname, stubirth, stusex from tb_student
where stubirth between '1990-1-1' and '1999-12-31' or stusex='男';
-- 集合运算in和not in
select stuid, stuname from tb_student
where stuid not in (1001, 1003, 1005, 2001);
-- 判断空值(null)不能用=或<>,需要使用is或is not
select * from tb_student where stuaddr is null;
select * from tb_student where stuaddr is not null;
insert into tb_student values (1007, '李白', default, null, null);
insert into tb_student values (1008, '李白菜', '女', null, null);
-- 模糊
select * from tb_student where stuname like '白%';
select * from tb_student where stuname like '%白';
select * from tb_student where stuname like '%白%';
select * from tb_student where stuname like '白';
select * from tb_student where stuname like '白
';
select * from tb_student where stuname like '
白';
select * from tb_student where stuname like '白';
select * from tb_student where stuname like '
';
-- 排序
select * from tb_student order by stuid desc;
select * from tb_student order by stuname asc;
select * from tb_student order by stusex desc, stuid desc;
-- 分页查询
select * from tb_student order by stuid desc limit 3;
select * from tb_student order by stuid desc limit 3 offset 3;
select * from tb_student order by stuid desc limit 3,3;
select * from tb_student order by stuid desc limit 3 offset 6;
select * from tb_student order by stuid desc limit 6,3;
select distinct substr(stuname, 1, 1) from tb_student;
select substr(stuname, 2, length(stuname)) from tb_student;
-- 聚合函数(统计函数)
select max(credit) from tb_course;
select min(credit) from tb_course;
select max(stubirth) from tb_student;
select min(stubirth) from tb_student;
select sum(credit) from tb_course;
select avg(credit) from tb_course;
select count(stuid) from tb_student;
select count(cid) from tb_course;
-- 分组查询
select stusex, count(stuid) from tb_student group by stusex;
-- 创建用户
create user 'wangdachui'@'%' identified by '1qaz2wsx';
create user 'wangdachui'@'localhost' identified by '1qaz2wsx';
-- 授权操作
grant all privileges on . to 'wangdachui'@'localhost';
grant all privileges on . to 'wangdachui'@'localhost' with grant option;
-- 召回权限
revoke all privileges on . from 'wangdachui'@'localhost';
grant select on school.* to 'wangdachui'@'localhost';
grant insert, delete, update on school.* to 'wangdachui'@'localhost';
grant create, drop, alter on school.* to 'wangdachui'@'localhost';
-- 表与表之间的关系
-- 一对一 (人和身份证、用户和购物车)
-- 一对多 / 多对一(人和银行卡、用户和订单)
-- 多对多(学生和课程、订单和商品)
-- 外键:外来的主键(其他表的主键)
-- 学生、课程、学院、老师

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,387评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,845评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,091评论 0 246
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,308评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,662评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,795评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,008评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,743评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,466评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,687评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,181评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,531评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,177评论 3 239
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,126评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,902评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,862评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,734评论 2 274

推荐阅读更多精彩内容

  • sql语句练习sql练习2 MYSQL导入数据出现The MySQL server is running with...
    十二右阅读 1,210评论 0 0
  • 花了3天时间学习MySql,考了个二级MySql 书籍参考:高等教育出版社《全国计算机等级考试二级教程-MySQL...
    如果仲有听日阅读 1,231评论 4 4
  • Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 S...
    忘了呼吸的那只猫阅读 2,688评论 0 8
  • 2017.6.24 今日体验: 听妈妈说,表哥看上了公务员,听到这个消息特别高兴,觉得这两年表哥没黑天没白天的学习...
    小鑫小磊阅读 467评论 0 3
  • 如果我着实认为这个世界不美好,该怎样让自己不和悲伤触摸,不让心沦陷至心灰意冷的绝境,一时之间无法缓解,又该怎样...
    暖暖的好阅读 271评论 3 4