Mysql数据库的安装与基本操作_32

一 数据库的基本概念

1 数据管理技术的发展阶段

什么是数据管理:对各类数据进行分类、组织、编码、检索、维护。

阶段:
人工管理阶段(50年代): 没有磁盘,没有U盘,没有管理软件;
文件系统管理(50年代后期-60年代):磁盘出现了,有了操作系统;
数据库系统阶段(60年代后期->现在):网络技术的兴起,文件系统满足不了需求了,出现了数据库技术,关系型数据库;

2 数据库系统的一些概念

什么是数据库:按照一定规则,存储、管理数据的仓库;
什么是数据库管理系统:操作和管理数据库的大型软件,可以建立,使用,维护数据库,同时可以对数据库进行统一的管理和控制;
常用的数据库管系统:Oracle、MySQL, SQL Server , DB2
什么是关系型数据库:采用关系统模型来组织的数据库。以行和列来存储数据,理解为一个二维表格模型。

3 什么是SQL

Structure Query Language(结构化查询语句)
使用最广泛的关系型数据库的标准语句,由IBM在70年代设计,ISO国籍标准化组织吸收,采纳。

4 MySQL的发展

MySQL AB公司。
2008年 SUN收购了MySQL, (Java)
2009年 SUM公司被Oracle公司收购

二 MySQL的安装和配置

1 添加环境变量

在这里插入图片描述

2 以管理员身份打开cmd窗口

输入命令:
1 初始化:

mysqld --initialize --console
这里会记录一个密码

在这里插入图片描述

2 安装:

mysqld -install

3 开启服务:

net start mysql

4 登录:

mysql -uroot -p
用你上面记录的密码进行登录

在这里插入图片描述

5 密码更改:

 alter user 'root'@'localhost' identified by '456789';

6 修改密码加密规则:

alter user 'root'@'localhost' identified with mysql_native_password by '456789';

三 数据库的基本操作

1、查看数据库

 show databases;

在这里插入图片描述

2、创建数据库

create database qwer;

3、使用数据库

 use qwer;

4、删除数据库

drop database qwer;

在这里插入图片描述
安全方式删除

mysql> drop database qwer;

ERROR 1008 (HY000): Can’t drop database ‘qwer’; database doesn’t exist

mysql> drop database if exists qwer ;

Query OK, 0 rows affected, 1 warning (0.00 sec)

5、数据库存储引擎
MySQL的核心就是存储引擎

 show engines \g

在这里插入图片描述

6、查看数据库当中有哪一些表

show tables;

7、数据表的创建
这里创建qwer表,并且使用qwer表

create table student(
id  int,
name varchar(10),
sex  varchar(2),
age int,
score int
);

在这里插入图片描述
8、删除表

drop table student;

9、1064错误
ERROR 1064 表示SQL语句错误。

10、修改列

修改表名:alter table student rename to student1;
在这里插入图片描述
11、desc查看表的字段类型

 desc student1;

在这里插入图片描述
12、往表中插入数据并查询

插入单条记录:

insert into student
(id, name, sex,age, score)
value
(2, "张三", "男", 18,  100);

插入多条记录:

insert into student
(id, name, sex,age, score)
values
(2, "李四", "男", 19, 99),
(3, "王五", "女", 17, 98);

查询:select * from student1;

13、使用主键约束
主键:主码
主键约束:设置为主键的字段,数据是唯一,不能为空。
主键能够唯一的表示表中的一条记录。
a.单个字段主键

create table student(
id int primary key,
name varchar(10),
sex  varchar(2),
age int,
score int
);

b.在定义之后指定主键

create table student(
id int,
name varchar(10),
sex  varchar(2),
age int,
score int,
primary key(id)
    );

c.多字段联合主键

create table student(
id int,
name varchar(10),
sex  varchar(2),
 age int,
 score int,
 primary key(id, name, age)
 );

注意:字符串做主键是可以重复的。
在这里插入图片描述

d.自增

create table student(
id int primary key auto_increment,
name varchar(10),
sex  varchar(2),
age int,
score int
);

14、数据类型

数值类型、日期/时间类型、字符串类型
(1)数值类型:
整数类型:tinyint, smallint ,mediumint, int, bigint

tinyint		很小的整形,1个字节,有符号:-128->127 , 	无符号:0->255
smallint	小的整形,	2个字节,有符号:-32768->32767,	无符号:0->65535
mediumint, 	中等整形,	3个字节,有符号:-8388608->8388607,无符号:0->16777215
int,		普通整形,	4个字节,
bigint		大的整形,	8个字节

age tinyint unsigned,

(2)小数类型

浮点型和定点型  
浮点型和定点型都可以使用(M,N的方式来表示) , M:精度,总位数,N:标度,小数位数

浮点型:
单精度浮点型和双精度浮点型:

float		单精度浮点型	4个字节
double		双精度浮点型	8个字节
decimal(M,N)定点型			M+2字节		以串的方式存储

15、日期时间类型:

DATETIME	YYYY-MM-DD HH:MM:SS		1000-01-01 00:00:00 -> 9999-12-31 23:59:59	8个字节
DATE		YYYY-MM-DD				1000-01-01 -> 9999-12-31					3个字节
TIMESTAMP	YYYY-MM-DD HH:MM:SS		1970-01-01 00:00:00 -> 2038-01-19 03:14:07 	4个字节
TIME		HH:MM:SS				-838:59:59->838:59:59						3个字节
YEAR		YYYY					1901->2155									1个字节

16、文本字符串类型

char(M)		固定长度字符串			M字节, 1<=M<=255
varchar(M)	可变长字符串			L+1字节,L<=M,  1<=M<=255 (L:字符串实际长度)
text
tinytext	小的字符串				L+1字节,L<2^8
mediumtext	中等字符串				L+2字节,L<2^16
longtext	长的文本				L+3个字节,L<2^24


char和varchar区别: char是固定长度,固定M个字节,而varchar是实际长度+1,实际长度不能超过M
如果要求查询速度:可以使用char, 尽量不要浪费空间
varchar的查询速度低于char,如果要求存储空间,可以使用varchar

四 详解数据库的查询

a.查询所有字段
查询所有数据:(*是一个通配符,匹配所有字段)

select * from student;

b.查询多个字段:

select id, name, score from student;

c.制定条件查询(where)

=		相等
<>, !=	不等于
<		小于
>		大于
<=		小于等于
>=		大于等于
between...and	位于两者之间
 select * from student where name="皮蛋";

查询所有女生:

select id,name,sex from student where sex="女";
select id,name,sex from student where sex <> "男";
select id,name,sex from student where sex != "男";

查询成绩在60-80分范围:

select id ,name ,score from student where score between 60 and 80;

查询成绩在60-80分范围,并且是女生:

select * from student where score between 60 and 80 and sex='女';

d.带in关键字查询
把在指定包含数字查询出来

select * from student where score in(60,100);

e.not in关键字查询

select * from student where score not in(60,70,80,90,100);

f.带like的关键字
一般和like联合使用的两个通配符;% , _

(1)、使用%通配符,匹配任意长度的字符,甚至包括零字符

 select * from student where name like "王%";
select * from student where name like "%老%";
select * from student where name like "王%五";

(2)、下划线通配符 _ ,一次只能匹配任意一个字符

select * from student where name like "王_五";
select * from student where name like "王__五";
select * from student where name like "_老%";

g.查询空值
NULL和0不一样,跟空字符串不一样,NULL表示数据是未知的。

select * from student where score is null;

h.查询非空值
is not null:select * from student where score is not null;

i.带and多条件查询(两个条件同时满足)

查询男生中及格人

select * from student where sex="男" and score >= 60;

j.带or的多条件查询

查询是男生或者及格的人

select * from student where sex="男" or score >= 60;

k.消除重复记录

select distinct name from student;

l.对查询结果排序
order by排序 (默认升序)

select * from student order by score

ASC关键字(升序)

select * from student order by score asc;

DESC关键字(降序)

select * from student order by score desc;

m.对查询结果进行数量限制limit

查询前面5条记录:

select * from student limit 5;

等价于:

select * from student limit 0,5;

从第几条开始查询,总共查询5条

select * from student limit 3,5;
select * from student order by score desc limit 3,5;

n.count函数

count(*) :总行数

 select count(*) from student;

as:起别名

select count(*) as num from student;

count(字段名) 统计总行数,但是会忽略空值

select count(score) from student;

o.sum函数,求和函数

select sum(score) as sum_score from student;
select sum(score) as sum_score from student where sex="女";

p.avg函数,求平均值

select avg(score) as avg_screo from student;

五 更新数据

update student set name="天才" where id = 9;
update student set
name = "皮蛋",
sex = "男",
age = 25,
score = 89
where id = 8;

也可以更新一个范围

update student set score = 60 where age between 10 and 17;

六 删除数据

删除单条记录:

delete from student where id = 7;

删除多条记录:

delete from student where score < 60;
delete from student where id = 1 or id = 11; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

敲折耳根的

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

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

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

打赏作者

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

抵扣说明:

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

余额充值