超详细数据库基础知识!!!

数据库基础

  • 数据库安装
  • 数据库的概念
  • 数据库的操作
  • 数据表的操作
  • 数据行的操作

默认端口号

3306

安装

1.yum makecache  
2.yum clean all 
3.wget -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
4.yum -y install mysql-server  mysql-client libmysqld-dev 
				 mysql服务器    mysql客户端   依赖库  
5.service mysqld start  #开启服务  

mysql -u root -p 
输入密码 #不行


# 修改mysql root 密码  

vim /etc/my.cnf 

[mysqld]
skip-grant-tables  #绕过密码验证  


mysql -u root -p 
直接回车就登录成功 

show databases; #列出所有的数据库 

use mysql;

update mysql.user set authentication_string=password("新密码") where user='root'; # 更新密码 
update user set plugin="mysql_native_password"; #更新原始密码   
flush privileges; #刷新权限    

vim /etc/my.cnf  

#skip-grant-tables  #注释掉  


service mysqld restart #重启mysql服务  



如果想用 本地终端 连接远程数据库  
1.授权

如果遇到以下错误 
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.


mysql -u root -p

> set global validate_password_policy=0;
> set global validate_password_length=1;
> alter user 'root'@'localhost' identified by '你的密码';


开始授权 

grant all on *.* to root@'%' identified by '123456' with grant option;#授权 在任何主机上 用root  123456 登录 可以操作所有的数据库 以及所有的数据表   

flush privileges; #刷新权限 

数据库的五个基本单位

1.数据库服务器  
2.数据库
3.数据表
4.数据字段 
5.数据行

数据库的操作

show dababases;#列出所有的数据库 
use 数据库名字;选中数据库 
show tables; #查看数据库有哪些数据表 
drop database whpython;

数据表的操作

创建数据表
create table 表名(字段名 字段类型(长度))engine=Innodb default charset=utf8; #mysql没有utf-8 只有utf8


mysql> create table article(id int(11));  #采用默认引擎  默认的字符集 
Query OK, 0 rows affected (0.03 sec)

mysql> create table book(id int(11))default charset=utf8; #指定字符集 
Query OK, 0 rows affected (0.02 sec)

mysql> create table test(id int(11))engine=innodb default charset=utf8; #指定引擎 指定字符集 
Query OK, 0 rows affected (0.04 sec)

查看表结构
desc 表名;

字段名     字段类型  是否为空 是否主键 是否有默认值  其它信息
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

show create table 表名;#查看建表语句  

show create table 表名\G  #以最佳的阅读体验阅读  

engine mysql引擎  
charset 字符集  

删除数据表
drop table 表名;

数据字段的操作

alert table 表名

修改表字段类型 modify
alter table 表名 modify 字段名 字段类型(长度);

alter table article modify id varchar(50);
mysql> alter table article modify password after name;
mysql> alter table article modify password varchar(64) first;

增加表字段 add
alter table 表 add column 字段名 类型; #column 可加 可不加 
alter table article add username varchar(50);
alter table article add username varchar(50) first;
alter table article add username varchar(50) after 字段;
alter table article add column password varchar(50);

增加表字段 调整增加的顺序 first 、after

默认把字段加到末尾处

alter table article add sex int first; #添加字段到最开头的位置
mysql> alter table article add title varchar(64) after username; #添加新字段到指定字段的后边  
删除表字段 drop
mysql> alter table article drop column sex;
mysql> alter table article drop age;  #column可加 可不加 

表字段 改名字 change

mysql> alter table article change username name varchar(30);
alter table article change username name char(30) first;
alter table article change username name varchar(30) after 字段名 ;

总结

  1. modify 修改表字段类型 、change 表字段改名 、add 添加表字段 后边都可以跟上 first 和 after 字段名;
修改表名
alter table 旧表名 rename 新表名;

类型 、引擎、字符集、索引

类型

  • 数值类型: 整型、浮点型
  • 字符串类型
  • 日期类型
  • 符合类型
  • 空间类型
数值类型
整型
类型占字节数范围
int4
tinyint1-128~127
smallint2-32768-32767
mediumint3-8388608~8388607
bigint8
浮点型
类型占字节数范围
float(m,d)4单精度 m 总个数 d 小数点后边位数 float(10,4)66666.66666 这个总共10位但是小数点后边5位 不满足条件
double(m,d)8双精度 m 总个数 d 小数点后边位数
decimal储存为字符串的浮点数

使用注意事项

  1. 如果我们使用int的时候 不希望是负数 unsigned 表示无符号的 只能正数 不能负数
  2. 性别 无符号整型 一般不存男或者nv 一般用 tinyint 来表示 要么 0 要么1 要么2
  3. 年龄 无符号整型 smallint 就ok
  4. 能用数字表示的 不用字符串 为了节约空间
  5. 银行中 对金额要求比较高 经常用的是 decimal 本质是字符串存储
字符串类型
类型字节数范围
varchar0-255
char0-255
tinytext0-255
text0-65535
longtext极大文本
tinyblob二进制形式的文本数据 比如存图片 视频等 0-255
blob0-65535 二进制形式的长文本
longblob极大文本

总结

  1. char 给他分配了30长度 如果实际上字符超出了30长度 那么会将该字符截短 只能存30 如果不足30 用空格补齐
  2. varchar 给他分配了30长度 超过30长度 还是会截短 如果不足30长度 不用空格补齐
  3. blob 和 text的区别 区分大小写 text 不区分大小写
时间类型
类型字节数范围
date32020-07-20
time316:23:30
datetime82020-07-20 16:23:30
timestamp4自动存储记录修改的时间
year1年份

开发过程中 好多产品用时间戳 代替 日期和时间 这样的好处也是为了节约空间

复合类型 了解
类型说明举例
set集合类型set(‘num1’,‘num2’,‘num3’)
enum枚举类型enum(‘num1’,‘num2’,‘num3’)

enum 只允许一个集合中取一个值

set 允许 一个集合取多个值

sql语句常见的限定条件
  • unsigned 无符号 如果哪个字段的值 必须是正整数 那么需要加上它
  • default 设置默认值
  • zerofill 也是为了阻止填充负值
  • not null 不能为空
create table if not exists numbers(
	id int unsigned not null,
	username varchar(64) not null,
	content text not null,
	age tinyint(4) zerofill,
	sex tinyint unsigned not null default 1,
	create_time datetime not null)engine=innodb default charset=utf8;  #没有utf-8 只有utf8

字符集

  • ASCII A 65 a 97
  • gbk 汉字编码规范 向下兼容 gb2312
  • unicode 国际组织定义的 所有语言编码规范 用来跨语言 跨平台 文本之间的转化
  • utf8 针对 Unicode的一个可变长度编码

工作过程中使用的字符编码:

  1. utf8_general_ci 多国语言不区分大小写
  2. gbk_general_ci 简体中文 不区分大小写

引擎

show engines; 列出所有的引擎

常用的: innodb myisam memory

innodb和 myisam 引擎的区别:

1.mysql 5.5.5以后默认的引擎是 innodb 之前默认的引擎是 myisam

2.myisam引擎 读效率高

3.innodb 写数据更加的安全

索引 重点

缩小查询的范围 提升查询的速度的

  • 普通索引
  • 主键索引 这一列不能有重复值
  • 唯一索引
  • 复合索引
  • 全文索引
create table qf2002(id int(11) unsigned not null primary key auto_increment)default charset=utf8;
每个都有一个逐渐  一般创建表的时候都给他指定了   

如果没有建表的时候指定主键


mysql> desc book;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> alter table book add primary key(id); #可以通过这种方式指定某个字段为主键  
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc book;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

增删改查

  • 增 insert
  • 删 delete
  • 改 update
  • 查 select

insert into 表名 values(值1,值2,…值n) #表结构有多少个字段 values 你就要写多少个字段

insert into 表名(字段1,字段2,字段3,字段4) values(值1,值2,值3,值4) 有些字段为空 或者有默认值可以不用写 只需要插入要求的字段就好了

mysql> desc articles;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| age      | int(11)     | YES  |     | NULL    |       |
| password | varchar(64) | YES  |     | NULL    |       |
| id       | varchar(50) | YES  |     | NULL    |       |
| sex      | int(11)     | YES  |     | NULL    |       |
| name     | varchar(30) | YES  |     | NULL    |       |
| title    | varchar(64) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> insert into articles values(18,'123456',1,1,'jiadong','山鸡');
Query OK, 1 row affected (0.00 sec)

mysql> insert into articles(name,password,title,age) values('liangzai','654321','陈浩南',19);
Query OK, 1 row affected (0.00 sec)

mysql> select * from articles;
+------+----------+------+------+----------+-----------------+
| age  | password | id   | sex  | name     | title           |
+------+----------+------+------+----------+-----------------+
|   18 | 123456   | 1    |    1 | jiadong  | 山鸡      |
|   19 | 654321   | NULL | NULL | liangzai | 陈浩南        |
+------+----------+------+------+----------+-----------------+
2 rows in set (0.00 sec)

以上是插入一条数据

接下来 插入多条数据

mysql> insert into articles values(20,'123456789',3,0,'weiming','山鸡'),(20,'123456789',3,0,'weiming','山鸡'),(20,'123456789',3,0,'weiming','山鸡');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

删除数据

delete from 表名;#删除整张表的数据 
delete from 表名 where 条件; #删除制定条件的数据 
mysql> delete from articles where age=25; #删除 age为25的数据行  


truncate table 表名; #也是删除整张表的数据 


区别:delete from 表名; 清空数据以后 再次插入数据  id 从原来的id 往后顺延  
truncate table 表名;清空数据以后 再次插入数据  id 从1开始  

update 表名 set 字段名1=值1,字段名2=值2 where 条件;

mysql> update articles set age=19,name='二十不惑' where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from articles;
+----+------+----------+------+--------------+----------------------------------+
| id | age  | password | sex  | name         | title                            |
+----+------+----------+------+--------------+----------------------------------+
|  1 |   19 | 123456   |    1 | 二十不惑     | 我不是出色的电焊工               |
|  2 |   18 | 123456   |    1 | kangbazi     | 没有点到你                       |
|  3 |   18 | 123456   |    1 | kangbazi     | ..有把你我的心焊在一起           |
+----+------+----------+------+--------------+----------------------------------+
3 rows in set (0.00 sec)

select 你要查询的字段名 from 表名 where 条件;
mysql> select name,title from articles where id>=1;
+--------------+----------------------------------+
| name         | title                            |
+--------------+----------------------------------+
| 二十不惑     | 我不是出色的电焊工               |
| kangbazi     | 没有点到你                       |
| kangbazi     | ..有把你我的心焊在一起           |
+--------------+----------------------------------+
3 rows in set (0.00 sec)

mysql> select name as 姓名,title as 题目 from articles where id>=1;
+--------------+----------------------------------+
| 姓名         | 题目                             |
+--------------+----------------------------------+
| 二十不惑     | 我不是出色的电焊工               |
| kangbazi     | 没有点到你                       |
| kangbazi     | ..有把你我的心焊在一起           |
+--------------+----------------------------------+
3 rows in set (0.00 sec)

select * from 表名; #显示所有的 字段  以及所有的数据行  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值