2018-11-20mysql学习

-- SQL: Structured Query Language(结构化查询语言)
-- 1. DDL: 数据定义语言 - create / drop / alter(修改)
-- 2. DML: 数据操作语言 - insert / delete/ update/ select
-- 3. DCL: 数据控制语言 - grant / revoke/ commit / rollback

-- creat database school default charset utf8;

-- 如果存在名为school的数据库就删除
drop database if exists school;

-- 创建school数据库并设置默认字符集为utf8
create database school default charset utf8;

-- 切换到school数据库的上下文环境
use school;

-- 创建学生表tb_student
create table tb_student
(
stuid int not null comment '学号',
stuname varchar(20) not null comment '学生姓名',
stusex enum('男', '女') default '男' comment '学生性别',
stubirh date comment '出生日期',
stuaddr varchar(255) comment '家庭住址',
primary key (stuid)
);

-- 修改表(添加字段)
alter table tb_student add column stutel char(11) comment '联系电话';

-- 删除字段
alter table tb_student drop column stutel;

alter table tb_student change column stuname stuname varchar(20) not null comment '姓名';

-- 新建课程表
drop table if exists tb_course;

create table tb_course
(
cid int not null comment '课程id',
cname varchar(31) not null comment '课程名',
credit tinyint unsigned not null comment '课程学分',
cdate timestamp default now() comment '开课时间',
primary key (cid)
);

-- tinyint 1字节
-- smallint 2字节
-- int 4字节
-- bigint
-- now() 获取系统当前时间
select now();
select year(now());
select date(now());
select time(now());

-- 在学生表中插入数据
-- into 可以省略
-- 录入一条完整的数据
insert into tb_student values (1001, '张三', default, '2001-12-23', '成都');
-- values 中的数据必须意义对应

-- 指定列插入对应的数据
insert into tb_student (stuid, stuname, stubirh) values (1002, '王大锤', '1990-1-1');

-- 一次录入多个值
insert into tb_student (stuid, stuname, stusex)
values
(1003, '李四', default),
(1004, '小明', '男'),
(1005, '小芳', '女'),
(1006, '白元芳', default);

-- 向课程表中插入数据

insert into tb_course
values
(1001, '语文', 2, default),
(1002, '数学', 3, '2018-10-2'),
(1003, '英语', 2, '2018-11-3');

insert into tb_course
values
(1004, 'python', 2, default),
(1005, 'java', 3, '2018-10-2'),
(1006, 'C', 2, '2018-11-3');

-- 删除记录

delete from tb_student where stuid=1006;
delete from tb_student where stuid>5;
delete from tb_student stuid between 1002 and 1005;

-- 删除全表
truncate table tb_student;

-- 更新数据

update tb_student set stubirh='1995-5-5' where stuid=1006;
-- 增删改的条件一般用主键

update tb_student set stubirh='1996-6-6', stuaddr='河北保定' where stuid=1004;

update tb_course set credit=4 where cid=1003;

update tb_course set credit=2 where cid=1002 or cid=1001;

update tb_course set credit=3 where cid in (1003, 1006);

-- 查询所有记录
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, stusex from tb_student where stubirh between '1990-1-1' and '1999-12-31';

-- 集合运算
select stuname, stusex from tb_student where stuid in (1003, 1005, 1006);

select stuname, stusex from tb_student where stuid not in (1003, 1005, 1006);

-- 判断空值不能用= <> 要用is null 或者 is not null
select * from tb_student where stuaddr is null;

select * from tb_student where stuaddr is not null;

insert into tb_student (stuid, stuname, stusex) values (1007, '李白', '女');

insert into tb_student (stuid, stuname, stusex) values (1008, '小小舒', '女');

-- 模糊查询
select * from tb_student where stuname like '小%';
-- % 支持匹配0个或多个字符

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;
-- asc --- 默认值升序
-- desc --- 降序

select * from tb_student order by stuname asc;

-- 指定多个排序字段进行
select * from tb_student order by stusex desc, stuid desc;

-- 先排列在看前3条
select * from tb_student order by stuid desc limit 3;

select * from tb_student order by stuid desc limit 3 offset 3;
-- offset 跳过指定的条数看指定条数(分页查询)
-- 分页查询可以用以下到的语句: limit 偏移量, 条数
select * from tb_student order by stuid desc limit 6, 3;

select substr(stuname, 1, 1) from tb_student;

-- distinct 去重
select distinct substr(stuname, 1, 1) from tb_student;

select substr(stuname, 2, length(stusex)) from tb_student;

-- 字符串连接
select concat(stuname, stubirh) from tb_student;

-- 聚合查询(聚合函数-- 统计函数)
-- max()
select max(credit) from tb_course;
select max(stubirh) from tb_student;

-- min()
select min(credit) from tb_course;
select min(stubirh) from tb_student;

-- sum()
select sum(credit) from tb_course;

-- avg()
select avg(credit) from tb_course;

-- count()
select count(stuid) from tb_student;
select count(cid) from tb_course;

-- 分组查询
select stusex, count(stuid) from tb_student group by stusex;

-- DCL 创建用户
create user 'me'@'%' identified by '123456';
-- 'localhost' ---- 只能本地登录
-- 'ip地址' ---- 通过指定的ip登录
-- '%' ---- 任意行式登录

-- 修改用户权限
grant all privileges on . to 'me'@'%';
-- 把所有的权限都给指定用户(但是他不能将自己的所有的权限给别的用户)
-- 收回所有的权限
revoke all privileges on . from 'me'@'%';
-- with grant option
-- 加上以上语句me用户就能将自己的权限授予别的用户

grant select on school.* to 'me'@'%';

grant insert, delete, update on school.* to 'me'@'%';

grant create, drop, alter on school.* to 'me'@'%';

-- 表与表之间的关系(实体之间的关系)
-- 一对一
-- 一对多
-- 多对多

-- 通过外键进行表关联
-- 外键

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

推荐阅读更多精彩内容

  • 一、数据库介绍 数据库 - 数据的仓库 - database关系型数据库 - 数据持久化 - 管理数据 - 方便的...
    龙神海王阅读 352评论 0 0
  • 花了3天时间学习MySql,考了个二级MySql 书籍参考:高等教育出版社《全国计算机等级考试二级教程-MySQL...
    如果仲有听日阅读 1,225评论 4 4
  • sql语句练习sql练习2 MYSQL导入数据出现The MySQL server is running with...
    十二右阅读 1,206评论 0 0
  • ORACLE自学教程 --create tabletestone ( id number, --序号usernam...
    落叶寂聊阅读 1,008评论 0 0
  • 郭亚锋 焦点六期 坚持分享57天 最近两天又追了一部悬幻小说,描写鬼故事。书中讲因果报应,人们无意中一句话也...
    自由飞翔的蜗牛阅读 184评论 0 0