MySQL笔记:(概念/数据库基本操作/数据库存储引擎/MySQL数据类型/表的操作)

本文详细介绍了MySQL的基本概念,包括数据管理历史、数据库系统、SQL语句及MySQL发展。涵盖数据库操作、存储引擎选择、数据类型、表的创建、修改、删除及约束设置,还有查询、更新和删除数据的方法,适合初学者和进阶者参考。
摘要由CSDN通过智能技术生成

目录

目录

一、MySQL的基本概念

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

        2.数据库系统 

        3.什么是SQL语句: 

        4. MySQL发展史

 二、数据库的基本操作:

        1.系统数据库:

        2.创建数据库

        3.删除数据库

         4.选择数据库

三、数据库存储引擎

查看所支持的存储引擎

四、MySQL数据类型:

        1.整数类型 

        2. 浮点型和定点型

        3.字符串类型 

        4.日期时间类型

 五、表的操作

1.表的概念:

2.使用数据库:

3.显示表 show tables;

4.创建表

        语法形式:

        例:创建一个学生表:

 5.查看表的结构

        ①describe 或者 desc语句来查看表的定义

        ②show create table 语句来查看表的详细定义

6.删除表

        ①drop table 数据表名称;

        ②安全删除方式:

        ③一次删除多张表:

        ④安全删除多张表:

7.修改表

        ①修改表名 :alter &rename (to)

        ②增加字段alter &add

        ③修改字段alter+modify/change

8.表的约束

        ①MySQL支持的完整性约束

        ②设置非空约束not null

        ③设置字段的默认值default

        ④设置唯一约束unique

        ⑤设置主键约束

        ⑥设置字段自动递增

        ⑦设置外键约束

一、MySQL的基本概念

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

数据管理:对各种数据的分类、组织,存储,检索;

数据管理的发展阶段:

人工管理阶段:没有磁盘,没有文件系统,没有数据库管理软件;用纸带来记录数据

文件系统阶段:磁盘出现了,操作系统,高级语言

数据库系统阶段:网络技术的发展,软件,硬件的功能,不能满足需求,出现了数据库技术,影响最大的就是关系型数据库。

        2.数据库系统 

什么是数据库:按照一定的规则存储,管理数据的仓库;

什么是数据库管理系统:操作和管理数据的大型软件;建立,使用,维护一体化。可以对数据统一管理。

常用的数据库管理系统:Oracle、MySQL、SQL Server、DB2、Access

什么是关系型数据库:采用关系模型来组织数据的数据库。以行和列的形式来存储数据的。(表格)

关系模型:简单的理解为二维表格模型。

        3.什么是SQL语句: 

Structure Query Language(SQL)结构化查询语句。

IBM公司70年代设计出来的, ISO国际标准化组织采纳吸收为国际标准。

        4. MySQL发展史

MySQL最早是可以追溯到1979年,瑞典的一家公司开发的 

MySQL AB公司

2008年 金融危机来临,MySQL 被SUN公司收购,2009年的时候SUN被Oracle公司收购。

 二、数据库的基本操作:

        1.系统数据库:

show databases;

 注意:SQL语句不区分大小写

        2.创建数据库

create database 数据库的名称;

数据库的命名规则:

  1. 数据库名字不能与已经存在的数据库名重合;
  2. 名称由字母、数字、下划线,@,#,$符号组成,a-z, A-Z,也可以是其他的语言;
  3. 名称不能是MySQL保留字
  4. 长度小于128
  5. 不允许有空格和特殊字符
  6. 数据库的名称一定要见名知意,具有实际意义;

执行完之后,提示居于分为三部分

Query Ok:表示SQL语句执行成功了;

1 row affected :表示操作影响了一行;

0.08 sec:表示操作执行的时间;

已安全的方式创建数据库:

create database if not exists test;

        3.删除数据库

drop database test;

注意:

  1. drop database 命令一定要非常谨慎,数据不能恢复;
  2. 如果删除指定的数据库不存在,会删除报错

 已安全的方式删除数据库:

 drop database if exists test;

         4.选择数据库

use 数据库名;

如果选择的数据库名称不存在,就会报错。

三、数据库存储引擎

show engines \g

存储引擎:提高MySQL数据库管理系统的使用效率;

根据不同的需要来使用存储引擎

存储引擎制定了表的类型,表的存储方式,索引数据,是否支持事务等。

查看所支持的存储引擎

 show engines;

show engines \g

\g和“;”一样的

\G以详细详情的方式来列举:

 show engines \G

 

Mysql 4.1.2版本以前:

 show variables like "have_%";

四、MySQL数据类型:

MySQL支持很多种数据类型:数值类型、日期/时间类型、字符串类型

(1)数值类型:

整数类型:tinyint、smallint、mediumint、int、bigint

浮点型:float、double

定点型:decimal

(2)字符串类型:文本字符串、二进制字符串

有char、varchar、text、binary、varbinary

(3)日期/时间型

有year、date、time 、datetime、时间戳:timestamp

        帮助文档:

 help contents;

        1.整数类型 

类型名称

说明

字节

有符号

无符号

tinyint

很小的整数

1个字节

-128->127

0->255

smallint

小的整数

2个字节

-32768->32767

0->65535

mediumint

中等整数

3个字节

-2^23->2^23 - 1

0->2^24 -1

int

普通的整形

4个字节

-2^31->2^31 - 1

0->2^32 -1

bigint

大整数

8个字节

-2^63->2^63 - 1

0->2^64 -1

搞不清楚,help一下。  example:   help int;

创建数据表:

create table test_int(

num1 tinyint unsigned,

num2 smallint,

num3 mediumint,

num4 int,

num5 bigint

);

测试数据:

insert into test_int

(num1, num2 ,num3, num4 , num5)

value

(128,  32767, 123459, 12345678, 45678945112);

如果数据超出范围,报错

show error;
show warning;

可以查看错误或者警告信息:

查询所有数据:

 select * from test_int;

        2. 浮点型和定点型

浮点型

所占字节

取值范围

float

4

 -3.402823466E+38->-1.175494351E-38,0, 1.175494351E-38->

3.402823466E+38

double

8

 -1.7976931348623157E+308->-2.2250738585072014E-308,0,

2.2250738585072014E-308->1.7976931348623157E+308

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

float(M,N):M:总位数,N是小数点后面的位数,单精度浮点型精确到大约小数点后面7位

double(M,N):双精度浮点型大约可以精确到小数点后15位

        定点型:

定点型类型

字节

取值范围:

decimal(M, N)或者dec(M,N)

M+2

M:最大65, N:最大30

创建数据表:

create table test_float(

num1 float,

num2 double,

num3 dec(64,30)

);

插入数据:

insert into test_float

(num1, num2, num3)

value

(1.1, 123456.78945,1234567890123456789012345678901234.89012345678901234567890444);

查看表结构

describe test_float1;

desc test_float1;

查看建表

show create table test_float1;

        3.字符串类型 

char系列的字符串

字节

M的范围

char(M)

所占字节都是M,不管我的字符串长度是不是M,

0<=M<=255

varchar(M)

实际有N个字符,所占字节:N+1,最多可以存储M个字符

0<=M<=65535

区别一下:char和varchar

char是固定长度,varchar可变长度,存储所占字节不一样。Char指定最多可以容纳多少,那么所占的字节数也就是这么多,不管你存没存满;

但是varchar根据字符串的长度来,如果你所占的字符串与最多可容纳数少,那么以字符串长度+1表示所占字节数。

char类型的字符串的查询效率的非常高的,非常快,varchar的效率没有char高,但是varchar要比char可能节约存储空间。

varchar:以时间换空间,牺牲一点时间

char:空间换时间。

helloword

char(20)    =>20

varchar(20): => 11

Text系列

字节

实际所占

tinytext

0->255

M长度+2个字节

text

0->65535

M长度+2个字节

mediumtext

0->16777215

M长度+3个字节

longtext

0->4,294,967,295

M长度+4个字节

新闻:  Text

下面两个用的非常少:

存储少量二进制数据

实际很大一部分,存文件,寸硬盘。

Binary系列

字节

Binary(M)

M

存储空间M字节

Varbinary(M)

N

最多可以存M

存储大量二进制数据

blob系列

字节

tinyblob

0-》255

blob

0-》65535

mediumblob

0-》16777215

longblob

0-》4,294,967,295

创建数据库

create table test_char(

id int,

name varchar(10),

sex char(1),

age tinyint unsigned,

log text

);

插入数据:

insert into test_char

(id, name ,sex, age, log)

value

(2, "李四", "女", 18, "6666666666666666666666666666");

4.日期时间类型

日期时间类型

字节

最小值

最大值

year

1

1000

9999

date

4

1000-01-01

9999 12-31

time

3

-838:59:59.000000

838:59:59.000000

datetime

8

1000-01-01 00:00:00

9999-12-31 23:59:59

timestamp

4

1970-01-01 00:00:01.000000

2038-01-19 03:14:07.999999'

查看系统时间:(方法函数)

 select curdate(), curtime(), now(), year(now()), time(now());

创建数据表

create table test_datetime(

value1 date,

value2 time,

value3 datetime,

value4 year,

value5 timestamp

);

插入数据:

insert into test_datetime

(value1,value2,value3,value4,value5)

value

("2022-07-12", "23:09:00", "2022-06-01 00:00:00", "2022", now());

insert into test_datetime

(value1,value2,value3,value4,value5)

value

("2022-07-12", "23:09:00", "2022-06-01 00:00:00", 2019, now());

 五、表的操作

1.表的概念:

以行和列的格式进行组织的。

一行数据代表一条唯一的记录,每一列代表记录的一个字段;

2.使用数据库:

        在操作数据库之前,一定要先选择数据库

        use 数据库名;

3.显示表 show tables;

4.创建表

        语法形式:

create table 数据表名(

属性名  数据类型,

属性名  数据类型

....

);

属性名: 表示字段的名称

数据类型:表示字符的数据类型

        例:创建一个学生表:

create table student(

id int,

name varchar(10),

sex char(1),

age tinyint unsigned,

scord int

);

 5.查看表的结构

        ①describe 或者 desc语句来查看表的定义

                describe student;  或者 desc student;

        ②show create table 语句来查看表的详细定义

                 show create table student;

6.删除表

        ①drop table 数据表名称;

注意:如果数据表不存在,则报错。

        ②安全删除方式:

 drop table if exists student;

        ③一次删除多张表:

drop table 表1,表2,表3;

drop table student1, student2, student3;

        ④安全删除多张表:

drop table if exists student1, student2, student3;

7.修改表

        ①修改表名 :alter &rename (to)

        alter table 老数据表名 rename 新数据表名;

        或者or

        alter table 老数据表名 rename to 新数据表名;

                举例:

        alter table student3 rename student;

        alter table student3 rename to student;

        ②增加字段alter &add

                (A)在最后面的位置增加一个字段(直接加)

                alter table student add 字段名  字段类型;

                        举例:

                alter table student add height int;

                (B)在表的最前面第一个位置增加字段 first

                alter table 数据表名add 字段名 字段类型 first;

                        举例:                

                alter table student add stuno int first;

                (C)在表的指定位置之后增加字段after  

                alter table 数据表名 add 字段名 字段类型 after 字段名;

                        举例:

                alter table student add weight float after sex;

                (D)删除字段 alter& drop

                alter table数据表名 drop 字段名;

                        举例:

                alter table student drop scord;

        ③修改字段alter+modify/change

                (A)修改字段的数据类型

                alter table student modify 字段名 新的数据类型;

                alter table student modify height smallint;

                (B)修改字段名change

                alter table student change 原来字段名  新的字段名 原来的数据类型;

                alter table student change english physical tinyint unsigned;

                (C)既要改字段名,又要改数据类型change

               alter table student change 原来字段名  新的字段名 新的数据类型

                alter table student change physical english smallint;

                (D)修改字段的顺序 modify +after&first

                alter table 数据表名 modify 字段名1 字段1的数据类型 after 字段名2;

                alter table student modify stuno int after id;

                调到最前面:

                alter table 数据表名 modify 字段名 数据类型 first;

               alter table student modify stuno int first;

                (E)同样的,可用modify更改表的约束

8.表的约束

        ①MySQL支持的完整性约束

什么是完整性约束:数据的准确性和一致性;

MySQL提供了一些列机制来检测数据库表中的数据是不是满足我们的要求,满足我们条件,来保证我们数据库表中的数据的一致性和准确性, 这种机制就叫做约束。

        ②设置非空约束not null

        语法:

create table table_name(

属性名  数据类型 not null,

....

);

        举例:

create table student(

stuno int not null,

name varchar(6) not null,

sex char(1) not null,

age tinyint unsigned,

score int

);

必须插入:stuno , name, sex

        ③设置字段的默认值default

挡在插入一条新的记录的时候,如果没有为某一个字段赋值,那么数据库会自动的为这个字段插入一个默认值。

create table table_name(

属性名 数据类型 default 默认值,

.....

);

举例:

create table student(

stuno int not null default 0,

name varchar(6) not null default "张三",

sex char(1) default "男",

age tinyint unsigned,

score int

);

insert into student

(stuno, name, sex, age, score)

value

(1, "张三", "男", 20, 90);

insert into student

(age ,score)

value

(18, 88);

        ④设置唯一约束unique

当数据库中某一个内容不允许重复的时候,可以使用唯一约束。

        语法:

create table table_name(

属性名 数据类型 unique,

.....

);

        举例:

create table student(

stuno int not null default 0,

name varchar(6) unique,

sex char(1) default "男",

age tinyint unsigned,

score int

);

insert into student

(stuno, name, sex, age, score)

value

(1, "张三", "男", 20, 90);

insert into student

(stuno, name, sex, age, score)

value

(2, "张三", "女", 22, 98);

        ⑤设置主键约束

想要数据库中的某一个字段来唯一的标识这一条记录,可以使用主键约束,主键约束就是为了唯一标识。

目的:为了快速地查找这表中的记录。

设置主键:

字段是唯一的、非空的。

根据主键设置可以分为        

                A.单字段主键             

        语法形式:

create table 数据表名(

属性名 数据类型primary key,

....

);

        举例:

create table student(

id int not null primary key,

name varchar(12) not null default ''

);

总结:主键相当于非空约束加上唯一约束;

                B.多字段主键:

多个字段组成而成的时候。

        语法:

create table table_name(

属性名...数据类型,

.....

primary key(属性名1,属性名2)

);

        举例:

create table student(

id int not null,

name varchar(12) not null default '',

sex char(1) not null default '男',

primary key(name, sex)

);

两个字段的值同时存在,报错。

问题:什么时候用多字段主键?

一个主键不能唯一标识表中的记录的时候,才使用多个字段主键。

        ⑥设置字段自动递增

auto_increment是MySQL中唯一扩展的完整性约束。

当数据库表中插入一条新的记录的时候,字段上设置了auto_increment会自动生成一个唯一的ID,自动递增。

  1. 、一个数据表中,只能有一个字段自动递增;
  2. 、这个字段的数据类型必须是整型;

auto_incremen唯一,设置为主键。

        语法:

create table table_name(

属性名 数据类型 auto_increment,

......

);

        举例:

create table student(

id int not null primary key auto_increment,

name varchar(12) not null default '',

sex char(1) not null default '男'

);

        ⑦设置外键约束

外键作用:保证两个表数据的具有参照完整性。

下面用两个表进行说明其相关操作:

总看语法为:

constraint 本表需要绑定变量+foreign+key(外表被绑定的变量)+reference+外表名(外表变量名)

例:

create table department(

did int primary key auto_increment,

department_name varchar(20) not null default ''

);

create table employee(

wid int primary key auto_increment,

name varchar(10) not null default '',

sex char(1) not null default '',

did int,

constraint e_did foreign key(did)  references department(did)

);

insert into department

(department_name)

value

("PHP事业部");

只能插入部门表里面已经存在的部门,部门表里面不存在的部门,插入报错,这就是外键约束。

六、查询数据

        1.基本查询语句 

select 字段1,字段2,字段3,... from 表名 where 条件;

*:表示查询所有的字段(*:通配符,能够查询所有的字段);

 select * from student;

select name from student;

        2.查询指定的记录 

=      相等

!=     不等于

<>     不等于

<      小于

>      大于

<=      小于等于

>=      大于等于

between....and....   在两者之间(包含边边)

 例:

        ①等于=

select id,name,score from student where name = "闰土";

         ②<小于查询

select id,name,score from student where score < 60;

         ③between....and....两者之间查询

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

         3.查询空值

                ①is null  或者 <=>  null

select id,name ,score from student where  score is  null;

                ② is not null

select id,name ,score from student where  score is not null; 

        4.带in的关键查询(枚举) 

                ①In

select id,name ,score from student where score in(60,70,80,90,100);

原来的

查询之后:

 

                ②Not in 

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

如果是小数,使用decimal类型

select id,name ,score from student where score in(99.8, 99.7, 99.9);

        5. 带like的字符的匹配

                ①%通配符

                %:表示匹配任意长度的字符串,包含零字符

select * from student where name like '张%';

 查询姓名中间带有”张”的

 查询以“张”开头“友”结尾:select * from student where name like '张%友';

 

                ② 下划线 _ 通配符

                _与%有点类型,_一次只能匹配任意一个字符

查询以三个字的姓名,并且以“友”结尾

select * from student where name like '__友';

        6. 带and多条件查询

select * from student where name = "张学友" and score =100.0;

 select * from student where name = "张学友" and score < 100.0 and sex="男";

         7.带or的查询

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

        8.去掉重复值

select distinct name from student;

        9.对结果排序 

降序:desc

升序:asc ,默认

select * from student  order by score desc;

 

对多列排序

select * from student order by name,score;

 如果第一个排序有重复值,再排序第二个值。

        10. 使用limit限制查询结果的数量

limit 从哪一行开始  行数

网站:limit来翻页             

select * from student limit 5;

等价于:

select * from student limit 0,5;

select * from student limit 3,5;

 最后结果只有两条:

        11.使用函数来查询

                ①count函数

                 select count(*) from student;

                 select count(*) as student_record_count from student;

                 ②sum函数

select sum(score) as score_total from student;

                 ③avg函数

select avg(score) as avg_score from student;

 七、更新数据

        update student set score = 55  where id = 1;

 注意:update语句一定要加条件,如果不加条件,整个表全部会被更新。

更改多个条件的语句:

update student set score = 60,  name = "牛牛" where id = 13;

 

八、删除数据

 delete

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_Ocean__

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

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

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

打赏作者

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

抵扣说明:

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

余额充值