MySQL数据库笔记

数据定义语言:简称DDL(Data Definition Language),用来定义数据库对象:数据库,表,列等。关键字:create,alter,drop等

数据操作语言:简称DML(Data Manipulation Language),用来对数据库中表的记录进行更新。关键字:insert,delete,update等

数据控制语言:简称DCL(Data Control Language),用来定义数据库的访问权限和安全级别,及创建用户。

数据查询语言:简称DQL(Data Query Language),用来查询数据库中表的记录。关键字:select,from,where等

数据库管理系统(DBMS):用户通过数据库管理系统访问数据库中表内的数据,数据库管理系统可以管理多个数据库,一般开发人员会针对每一个应用创建一个数据库。
———————————————————————————————————————————
如何使用终端操作数据库?
1.如何登陆数据库?
(1)win+r—cmd—cd C:\Program Files\MySQL\MySQL Server 8.0\bin/*找到安装路径进入bin文件夹*/—mysql -uroot -p19980506ls...
(2)win+r—cmd—cd C:\Program Files\MySQL\MySQL Server 8.0\bin/*找到安装路径进入bin文件夹*/—mysql -uroot -p—*************/*不显示密码*/
(3)win+r—cmd—mysql -hlocalhost -uroot -p/*电脑ip,可远程登录数据库*/—*************/*不显示密码*/
2.dos窗口如何退出数据库?
   exit--回车
———————————————————————————————————————————
1.如何关闭数据库服务?
(1)win+r--services.msc--右键关闭
(2)win+r--cmd--net stop mysql80(以管理员身份启动dos命令)
2.如何打开数据库服务?
(1)右键我的电脑--管理--服务和应用程序--服务--MySQL80--右键启动
(2)win+r--services.msc--右键启动
(3)win+r--cmd--net start mysql80(以管理员身份启动dos命令)
3.如何卸载数据库?
(1)关闭数据库服务;
(2)卸载所有关于数据库的程序;
(3)打开Program Files和Program Files(x86)两个文件夹中关于mysql的文件夹(包括隐藏文件夹)
———————————————————————————————————————————
DDL语句(db1为数据库名)
1.直接创建数据库
   create database db1;/*不能创建重名数据库*/
2.如何查询数据库?
(1)show databases;
(2)show create database db3;
3.判断是否存在并创建数据库?
   create database if not exists db1;
4.创建数据库并指定字符集(编码表)
   create database db1 character set 字符集;
5.修改数据库字符集格式
   alter database db3 default character set utf8;
6.如何删除数据库?
   drop database db3;
7.查看正在使用中的数据库
   select database();
8.使用、切换数据库
   ues db1;
-------------------------------------------------------操作表-------------------------------------------------------
9.创建表(student)
   create table student(id int,name varchar(10),age int,birthday date,sex char(1));
10.查看表
   show tables;
11.查看表结构
   desc student;
12.查看创建表语句
   show create table student;
13.创建一个表结构相同的表(student1)
   create table student1 like student;
14.删除表
   drop table student1;
15.判断表是否存在并删除表
   drop table if exists students1;
16.修改表名(旧表名:student   新表名:student2)
   rename table student to student2;
-------------------------------------------------------操作列-------------------------------------------------------
17.添加表列(添加一个字段:remark)
   alter table student2 add remark varchar(20);
18.修改列类型(改为varchar(100))
   alter table student2 modify remark varchar(100);
19.修改列名(旧列名:remark   新列名:intro)
   alter table student2 change remark intro varchar(30);
20.删除列
   alter table student2 drop intro;
21.查看字符集
   show create table student2; 
22.修改字符集
   alter table student2 character set utf8;
———————————————————————————————————————————
DML语句(数据库:db2   表名:student)
例:创建完整学生信息表,包括学员id,姓名,年龄,性别,家庭住址,电话号码,生日,数学成绩,英语成绩。
1.插入全部字段
(1)所有字段名都写出来
   格式:insert into 表名 (字段名1,字段名2,字段名3...) values (值1,值2,值3...);
   create table student(id int,name varchar(20),age int,sex char(1),address varchar(200),phone varchar(50),birthday date,math double,english double);
(2)不写字段名
   格式:insert into 表名 values (值1,值2,值3...);
2.插入部分数据(没有添加数据的字段会使用null值)
   insert into 表名(字段名1,字段名2,字段名3...) values (值1,值2,值3...);   insert into student(id,age,birthday,name,address) values(1927175402,null,'1997-10-15','李四','长春市');
------------------------------------------------------蠕虫复制------------------------------------------------------
3.蠕虫复制:在已有的数据基础之上,将原来的数据进行复制,插入到对应的表中(首先新建一张表)
   格式:insert intu 新表名 select * from 旧表名;
   insert into student1 select * from student;
4.只复制旧表当中某些字段到新表当中(首先新建一张表)
   格式:insert into 新表名(字段1,字段2...) select 字段1,字段2 from 旧表名;
   insert into student2(name,age,id) select name,age,id from student;/*再次复制会加上后复制的数据*/
-----------------------------------------------------更新表记录-----------------------------------------------------
5.不带条件修改数据(update:修改数据   set:修改哪些字段   where:指定条件)
   格式:update 表名 set 字段名=值;
   updaye student1 set id=1927175403;/*将id全部改为1927175403*/
6.带条件修改数据
   格式:update 表名 set 字段名=值 where 字段名=值;
   update student1 set name='zhangsan' where age=25;/*将年龄为25岁的学生名字改为zhangsan*/
7.一次修改多个列
   格式:update 表名 set 字段名1=值1,字段名2=值2 where 字段名3=值3;
   update student1 set age=27,math=100 where name='张三';/*将姓名为张三的学生,年龄改为27岁,数学改为100分*/
-----------------------------------------------------删除表记录-----------------------------------------------------
8.不带条件删除数据
   格式:delete from 表名;
   select * from student3;/*删除表中数据,将表中数据一条一条删除*/
9.带条件删除
   格式:delete from 表名 where 字段名=值;
   delete from student1 where age=25;/*删除“年龄=25”学生的数据*/
10.truncate删除表
   truncate table 表名;/*删除表中数据,将之前表摧毁,再重新创建一样的表结构*/
———————————————————————————————————————————
DQL语句(查询以及条件查询)
   条件:
   create table student(id int,name varchar(20),age int,sex char(2),address varchar(200),phone varchar(20),birthday date,math double,english double);/*创建student表*/
   insert into student values(1,'闫妮',43,'女','北京市','0438-1234567','2019-05-30',92.8,88);
   insert into student values(2,'郭富城',21,'男','上海市','12345678910','2015-06-25',97.5,65.5);
   insert into student values(3,'赵丽颖',44,'女','深圳市','12345657442','2012-07-03',42.5,74.5);
   insert into student values(4,'张学友',34,'男','杭州市','12345654389','2013-11-17',69,65);
   insert into student values(5,'成龙',51,'男','哈尔滨市','12345650955','2005-10-12',88,97);
   insert into student values(6,'刘德华',57,'男','盘锦市','12345651265','2015-11-11',74.5,92.5);
   insert into student values(7,'马伊利',42,'女','长沙市','12345652654','2008-03-26',86.5,71.5);
   insert into student values(8,'黎明',49,'男','昆明市','12345655456','2000-09-14',77.5,60);
1.使用*表示所有列
   格式:select * from 表名;
   select * from student;
2.查询所有列(与1结果相同,但效率高,1更方便)
   格式:select 字段名1,字段名2,字段名3... from 表名;
   select id,name,age,sex,address,phone,birthday,math,english from student;
3.查询指定列
   格式:select 字段名1,字段名2,字段名3... from 表名;
   select name,age,sex from student;
4.别名查询(将想要查询的字段名改成别名再查询。as可以省略不写)
   格式:select 字段名1 as 别名,字段2 as 别名... from 表名;
   select name as 姓名,age as 年龄,sex as 性别 from student;
5.清除重复值(查询指定列并且结果不出现重复的数据)
   格式:select distinct 字段名 from 表名;
   select distinct name from student;
   格式:select distinct 字段名1,字段名2 from 表名;
   select distinct name,age from student;
6.查询结果参与运算(参与运算的必须是数值类型)
(1)某列数据和固定值运算
   格式:select 列名1 + 固定值 from 表名;
   select age + 2 from student;
(2)某列数据和其他列数据参与运算
   格式:select 列名1 + 列名2 from 表名;
   select math + english from student;
   select math + english 总成绩 from student;/*将列名改为别名进行运算*/
   select name 姓名,age 性别,math 数学成绩,english 英语成绩,math + english 总成绩 from student;
   select *,math+english 总成绩 from student;/*查询所有列,并把数学英语成绩相加显示为总成绩*/
———————————————————————————————————————————
Navicat Premium15(可视化工具)
1.条件查询
   格式:select 字段名 from 表名 where 条件;
   流程:取出表中的每条数据,满足条件的记录就返回,不满足条件的记录不返回
2.比较运算符
   >大于   <小于   <=小于等于   >=大于等于   =等于   <>/!=不等于
   格式:select * from 表名 where 字段名>数值;
   select * from hero where attack>350;
3.逻辑运算符
   and   多个条件同时满足
   or      多个条件其中一个满足
   not    不满足
   格式:select * from hero where 字段1>数值1 and 字段2<数值2;
   select * from hero where age>=35 and life<4000;
   格式:select * from hero where 字段1=数值 or 字段2=字符串 or 字段3=日期;
   select * from hero where id=1 or sex='男';
4.in关键字(与or结果相同)
   格式:select 字段名 from 表名 where 字段 in (数据1,数据2...);
   select * from hero where id in(1,3,5);
   select * from hero where id not in(1,3,5);
5.范围运算符
   格式:select * from 表名 where 字段 between 值1 and 值2;(表示从值1到值2范围,包含值1和值2)
   select * from hero where grounding_date between '2000-01-01' and '2019-01-01';
6.like关键字(模糊查询)
   格式:select * from 表名 where 字段名 like '通配符字符串';/*满足通配符字符串规则的数据就会显示出来*/
   通配符:%:表示0个或多个字符(任意个字符)   _:表示一个字符
   select * from hero where name like '%吕_';
7.排序
(1)单列排序(“where 字段=值”可以省略)
   格式:select 字段名 from 表名 where 字段=值 order by 字段名 asc/desc;(asc:升序,默认为升序;desc:降序)
   select * from hero where age<=35 order by age asc;/*年龄小于35,升序排列*/
(2)组合排序
   格式:select 字段名 from 表名 where 字段=值 order by 字段名1 asc/desc,字段名2 asc/desc;
   select * from hero where age<=35 order by age asc,life desc;/*年龄小于35,升序排列;如果年龄相同,按照生命降序排列*/
8.聚合函数
   五个聚合函数:
   count:统计指定列记录数,记录为null的不统计;
   sum:计算指定列的数值和,如果不是数值类型,那么计算结果为0;
   max:计算指定列的最大值;
   min:计算指定列的最小值;
   avg:计算指定列的平均值,如果不是数值类型,那么计算结果为0。
   格式:select 字段名... from 表名;(select count(age) from 表名;)
   select count(age) from hero;/*查询有多少条age的数据*/
   select count(*) from hero;/*查询有多少条数据*/
9.分组
   格式:select 字段1,字段2... from 表名 group by 分组字段 having 条件;
   select sum(life),sex from hero GROUP BY sex ;/*不同性别的生命总和*/
   select count(*),sex from hero where age>30 GROUP BY sex;/*查询年龄大于30不同性别的人数*/
   select count(*),sex from hero where age>25 GROUP BY sex having count(*)>2;/*查询年龄>25,显示性别组人数大于2的*/
   having和where的区别:
(1)having是在分组后对数据进行过滤;where是在分组前对数据进行过滤
(2)having后面可以使用聚合函数;where后面不可以使用聚合函数
10.limit语句(限制查询记录的条数,分页)
   格式:select * 字段列表 as 别名 from 表名 where 子句 group by 子句 order by 子句 limit 子句;
   语法格式:limit offset,length;   limit length;(offset是指偏移量,可以认为是跳过的记录数量,默认为0;length是指需要显示的总记录数)
   select * from hero limit 2,6;/*查询hero表中数据,从第3条开始显示,显示6条;2为跳过不显示的条数,当跳过条数为0时可省略*/
———————————————————————————————————————————
数据库约束
1.约束种类:
   primary key:主键
   unique:唯一
   not null:非空
   default:默认
   foreing key:外键
2.主键(用来唯一标识一条记录,每个表都应该有一个主键,且只有一个主键;id字段)
   创建主键:create table hero1(id int primary key);/*创建一个hero1表,将id字段设为主键*/
   删除主键:alter table 表名 drop primary key;
   主键自增:create table 表名(id int primary key auto_increment); 
   修改自增起始值:alter table 表名 auto_increment=起始值;(注:drop语句将之前数据清空后,再次添加数据,id接着删除之前的数值开始自增;turncate语句将之前数据清空后,再次添加数据,id从1开始自增)
   delete和turncate的区别:
(1)delete删除表中的数据,但不重置auto_increment的值;
(2)truncate摧毁表,重置表,auto_increment重置为1。
3.唯一(在这张表中这个字段的值不能重复;出现多个null值,不存在问题。)
   创建唯一:create table hero3(id int primary key auto_increment,name varchar(20) UNIQUE);/*创建一个hero3表,将id字段设为主键,将name字段设为唯一*/
4.非空(这个字段必须设置值,不能是null)
   创建非空:create table hero4(id int,name varchar(20) unique not null,gender char(2));/*创建表hero4,包含字段id,name,gender,其中name唯一且不能为null*/
5.默认值(在表中添加数据时,如果不指定这个字段的数据,就使用默认值)
   创建默认值:create table hero5(id int,name varchar(20) ,location varchar(50) default '射手');/*创建表hero5,包含字段id,name,location,其中location默认为“射手”*/
   注:添加数据时,当有省略数值出现时,values前应将字段写出来!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猛扇赵四那半好嘴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值