MySQL-NoSQL整体笔记---持续输出中

MySQL部分

一、搭建 MySQL 数据库服务器

1、下载并上传glibc版本的Mysql

在这里插入图片描述

2、新建用户以安全方式运行进程

[root@template ~]# groupadd -r -g 306 mysql
[root@template ~]# useradd -g 306 -r -u 306 mysql

3、安装并初始化mysql

[root@template ~]# tar xf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz  -C /usr/local/
[root@template ~]# cd /usr/local/
[root@template local]# ln -s mysql-5.7.36-linux-glibc2.12-x86_64/ mysql
[root@template local]# chown -R mysql.mysql mysql/*
[root@template local]# /usr/local/mysql/bin/mysqld  --initialize --user=mysql --datadir=/usr/local/mysql/data

记录初始密码:
Zz6_k2hkWAir

在这里插入图片描述## 4、修改mysql提供主配置文件

vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock

[mysqld_safe]
log-error=mysql.log
pid-file=mysql.pid

在这里插入图片描述## 5、为mysql提供sysv服务脚本

[root@template local]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@template local]# chkconfig --add mysqld
[root@template local]# chkconfig mysqld on

6、启动服务

[root@template local]# systemctl restart mysqld

在这里插入图片描述

7、配置环境变量

[root@template local]# cat  > /etc/profile.d/mysql.sh  << EOF
export PATH=/usr/local/mysql/bin:$PATH
EOF
[root@template local]# source /etc/profile.d/mysql.sh

8、修改密码

[root@template local]# mysqladmin -uroot -p'Zz6_k2hkWAir' password '123456'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

在这里插入图片描述

二、数据库基础

1、SQL语句的分类

在这里插入图片描述

2、MySQL基本操作

在这里插入图片描述在这里插入图片描述

mysql> create database bbsdb;
Query OK, 1 row affected (0.01 sec)

mysql> create database BBSDB;
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| BBSDB              |
| bbsdb              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

在这里插入图片描述

mysql> drop database BBSDB;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bbsdb              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use bbsdb;
Database changed

在这里插入图片描述

mysql> select database();
+------------+
| database() |
+------------+
| bbsdb      |
+------------+
1 row in set (0.00 sec)

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

在这里插入图片描述

mysql> show tables;
Empty set (0.00 sec)

mysql> create table bbsdb.user(name char(10),age int,homedir char(10));
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+-----------------+
| Tables_in_bbsdb |
+-----------------+
| user            |
+-----------------+
1 row in set (0.00 sec)

mysql> desc user;
+---------+----------+------+-----+---------+-------+
| Field   | Type     | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| name    | char(10) | YES  |     | NULL    |       |
| age     | int(11)  | YES  |     | NULL    |       |
| homedir | char(10) | YES  |     | NULL    |       |
+---------+----------+------+-----+---------+-------+
3 rows in set (0.04 sec)

在这里插入图片描述

mysql> insert into bbsdb.user values("bob",19,"USA");
Query OK, 1 row affected (0.00 sec)
mysql> insert into bbsdb.user values("CHAN",18,"SC");
Query OK, 1 row affected (0.00 sec)
mysql> select * from user;
+------+------+---------+
| name | age  | homedir |
+------+------+---------+
| bob  |   19 | USA     |
| CHAN |   18 | SC      |
+------+------+---------+
2 rows in set (0.00 sec)

mysql> select name  from user;
+------+
| name |
+------+
| bob  |
| CHAN |
+------+
2 rows in set (0.00 sec)

mysql> select name ,age from user;
+------+------+
| name | age  |
+------+------+
| bob  |   19 |
| CHAN |   18 |
+------+------+
2 rows in set (0.00 sec)

mysql>

在这里插入图片描述

mysql> update user set age=21 where name= "CHAN";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from user;
+------+------+---------+
| name | age  | homedir |
+------+------+---------+
| bob  |   19 | USA     |
| CHAN |   21 | SC      |
+------+------+---------+
2 rows in set (0.00 sec)

mysql> update user set homedir="CHINA";
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from user;
+------+------+---------+
| name | age  | homedir |
+------+------+---------+
| bob  |   19 | CHINA   |
| CHAN |   21 | CHINA   |
+------+------+---------+
2 rows in set (0.00 sec)

在这里插入图片描述

mysql> alter table user drop column name;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> select * from user;
+------+---------+
| age  | homedir |
+------+---------+
|   19 | CHINA   |
|   21 | CHINA   |
+------+---------+
2 rows in set (0.00 sec)

mysql> delete from user where age = 19;
Query OK, 1 row affected (0.00 sec)

mysql> select * from user;
+------+---------+
| age  | homedir |
+------+---------+
|   21 | CHINA   |
+------+---------+
1 row in set (0.00 sec)

mysql> delete from user;
Query OK, 1 row affected (0.00 sec)

mysql> select * from user;
Empty set (0.00 sec)

在这里插入图片描述

三、数据库类型

1、信息种类

在这里插入图片描述

字符类型
在这里插入图片描述在这里插入图片描述

mysql> create database bbs;
Query OK, 1 row affected (0.00 sec)


mysql> use bbs;
Database changed


mysql> create table t1(name char(5),email varchar(15));
Query OK, 0 rows affected (0.00 sec)

mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | char(5)     | YES  |     | NULL    |       |
| email | varchar(15) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

在这里插入图片描述

mysql> create table t2(name char,email varchar(3));
Query OK, 0 rows affected (0.00 sec)

mysql> desc t2;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name  | char(1)    | YES  |     | NULL    |       |
| email | varchar(3) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

在这里插入图片描述


mysql> # #char类型不指定存储几个字符,默认存储一个
mysql> insert into t2 values("a","a");
Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 values("aaa","aaa");
ERROR 1406 (22001): Data too long for column 'name' at row 1

在这里插入图片描述

表中插入中文字符
mysql> show create table bbs.t1 \G;		
#查看建表命令,\G 竖着来显示数据 ###默认表的字符集CHARSET为latin1,不能在表中插入中文

在这里插入图片描述

mysql> create table 学生表(姓名 char(15),地址 varchar(50)) DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> desc 学生表;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| 姓名   | char(15)    | YES  |     | NULL    |       |
| 地址   | varchar(50) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into 学生表 values("张三丰","武当山");
Query OK, 1 row affected (0.00 sec)

在这里插入图片描述

2、数值类型

在这里插入图片描述

mysql> create  table t3(name char(15),age tinyint unsigned,level tinyint);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t3 values("bob",18,6);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t3 values("tom",-18,-6);
ERROR 1264 (22003): Out of range value for column 'age' at row 1
mysql> insert into t3 values("tom",0,-6);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t3 values("GGbong",0,-127);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t3 values("GG",0,-129);
ERROR 1264 (22003): Out of range value for column 'level' at row 1
mysql>
mysql> insert into t3 values("jim",21.5,9);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t3;
+--------+------+-------+
| name   | age  | level |
+--------+------+-------+
| bob    |   18 |     6 |
| tom    |    0 |    -6 |
| GGbong |    0 |  -127 |
| jim    |   22 |     9 |
+--------+------+-------+
4 rows in set (0.00 sec)

在这里插入图片描述存小数,四舍五入问题

mysql> insert into t3 values("jim",21.5,9);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t3;
+--------+------+-------+
| name   | age  | level |
+--------+------+-------+
| bob    |   18 |     6 |
| tom    |    0 |    -6 |
| GGbong |    0 |  -127 |
| jim    |   22 |     9 |
+--------+------+-------+
4 rows in set (0.00 sec)

mysql> insert into t3 values("jim",21.3,9);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t3;
+--------+------+-------+
| name   | age  | level |
+--------+------+-------+
| bob    |   18 |     6 |
| tom    |    0 |    -6 |
| GGbong |    0 |  -127 |
| jim    |   22 |     9 |
| jim    |   21 |     9 |
+--------+------+-------+
5 rows in set (0.00 sec)

在这里插入图片描述在这里插入图片描述

浮点数

float(7,2) # 7指整个浮点数的最大位数,2指7位数字中有两位是小数位, 则取值范围 为:-99999.99 ~ 99999.99

float(5,3) #5指整个浮点数的最大位数,3指5位数字中有三位是小数位, 则取值范围为:-99.999 ~ 99.999

float(数字1,数字2)

数字1:总的位数
数字2:小数位的个数

mysql> create table t4 (name char(5),pay float(5,2));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t4 values("john",1000.88);
ERROR 1264 (22003): Out of range value for column 'pay' at row 1
mysql> insert into t4 values("john",999.88);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t4 values("jim",-999.99);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t4;
+------+---------+
| name | pay     |
+------+---------+
| john |  999.88 |
| jim  | -999.99 |
+------+---------+
2 rows in set (0.00 sec)

mysql> insert into t4 values("jim",9);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t4;
+------+---------+
| name | pay     |
+------+---------+
| john |  999.88 |
| jim  | -999.99 |
| jim  |    9.00 |
+------+---------+
3 rows in set (0.00 sec)

在这里插入图片描述

3、日期时间类型

在这里插入图片描述
在这里插入图片描述

mysql> # 创建与日期时间相关的表,指定名称,年份,上课时间,生日,聚会时间
mysql> create table t5(name char(15),s_year year ,uptime time ,birthday date,party datetime);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t5 values("bob",2002,083000,20231120,20230214183000);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t5;
+------+--------+----------+------------+---------------------+
| name | s_year | uptime   | birthday   | party               |
+------+--------+----------+------------+---------------------+
| bob  |   2002 | 08:30:00 | 2023-11-20 | 2023-02-14 18:30:00 |
+------+--------+----------+------------+---------------------+
1 row in set (0.00 sec)

mysql>

在这里插入图片描述

在这里插入图片描述

mysql> select curtime();			#获取当前的系统时间
mysql> select curdate();			#获取当前的系统日期
mysql> select now();			#获取当前的系统日期和系统时间
mysql> select year(now());		#从当前系统时间中只取出年份
mysql> select month(now());		#从当前系统时间中只取出月份
mysql> select day(now());		#从当前系统时间中只取出天数
mysql> select date(now());		#从当前系统时间中只取出年月日
mysql> select time(now());		#从当前系统时间中只取出时分秒

根据时间函数在t5表中插入一条数据

mysql>  insert into t5 values("tom",2000,time(now()),curdate(),now());
Query OK, 1 row affected (0.00 sec)

mysql> select * from t5;
+------+--------+----------+------------+---------------------+
| name | s_year | uptime   | birthday   | party               |
+------+--------+----------+------------+---------------------+
| bob  |   2002 | 08:30:00 | 2023-11-20 | 2023-02-14 18:30:00 |
| tom  |   2000 | 20:08:11 | 2023-08-06 | 2023-08-06 20:08:11 |
+------+--------+----------+------------+---------------------+
2 rows in set (0.00 sec)

在这里插入图片描述关于日期时间字段:当未给timestamp字段赋值时,自动以当前系统时间赋值,而datetime值为NULL(空)
创建t6表,指定姓名,约会时间,聚会时间,验证timestamp和datetime的区别

mysql> create table t6(name char(10), meetting datetime, party timestamp);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t6 values("zs", now(), now());
Query OK, 1 row affected (0.00 sec)

mysql> select * from t6;
+------+---------------------+---------------------+
| name | meetting            | party               |
+------+---------------------+---------------------+
| zs   | 2023-08-06 20:09:52 | 2023-08-06 20:09:52 |
+------+---------------------+---------------------+
1 row in set (0.00 sec)

在这里插入图片描述

mysql> # t6表中重新插入一条数据,只插入name和metting字段的值,party字段采用默认值
mysql> insert into t6(name,party) values("john", 19731001223000 );
Query OK, 1 row affected (0.00 sec)

mysql> select * from t6;
+------+---------------------+---------------------+
| name | meetting            | party               |
+------+---------------------+---------------------+
| zs   | 2023-08-06 20:09:52 | 2023-08-06 20:09:52 |
| john | NULL                | 1973-10-01 22:30:00 |
+------+---------------------+---------------------+
2 rows in set (0.00 sec)

在这里插入图片描述

mysql> # t6表中重新插入一条数据,只插入name和metting字段的值,party字段采用默认值
mysql> insert into t6(name,meetting) values("bob", 20231120224058);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t6;
+------+---------------------+---------------------+
| name | meetting            | party               |
+------+---------------------+---------------------+
| zs   | 2023-08-06 20:09:52 | 2023-08-06 20:09:52 |
| john | NULL                | 1973-10-01 22:30:00 |
| bob  | 2023-11-20 22:40:58 | 2023-08-06 20:12:45 |
+------+---------------------+---------------------+
3 rows in set (0.00 sec)

在这里插入图片描述

mysql> # t6表中重新插入一条数据,只插入name和party字段的值,meetting字段采用默认值
mysql> insert into t6(name,party) values("john", 19731001223000 );
Query OK, 1 row affected (0.01 sec)

mysql> select * from t6;
+------+---------------------+---------------------+
| name | meetting            | party               |
+------+---------------------+---------------------+
| zs   | 2023-08-06 20:09:52 | 2023-08-06 20:09:52 |
| john | NULL                | 1973-10-01 22:30:00 |
| bob  | 2023-11-20 22:40:58 | 2023-08-06 20:12:45 |
| john | NULL                | 1973-10-01 22:30:00 |
+------+---------------------+---------------------+
4 rows in set (0.00 sec)

在这里插入图片描述

4、year类型

要求使用4位赋值
当使用2位数赋值时:01-99
01 ~ 69 视为 2001 ~ 2069
70 ~ 99 视为 1970 ~ 1999

插入数据,只给t5表中的s_year字段赋值,其他默认或者为NULL

mysql> show tables;
+---------------+
| Tables_in_bbs |
+---------------+
| 学生表        |
| t1            |
| t2            |
| t3            |
| t4            |
| t5            |
| t6            |
+---------------+
7 rows in set (0.00 sec)

mysql> desc t5;
+----------+----------+------+-----+---------+-------+
| Field    | Type     | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| name     | char(15) | YES  |     | NULL    |       |
| s_year   | year(4)  | YES  |     | NULL    |       |
| uptime   | time     | YES  |     | NULL    |       |
| birthday | date     | YES  |     | NULL    |       |
| party    | datetime | YES  |     | NULL    |       |
+----------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> select s_year from t5;
+--------+
| s_year |
+--------+
|   2002 |
|   2000 |
+--------+
2 rows in set (0.00 sec)

mysql> insert into t5(s_year) values(03),(81);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select s_year from t5;
+--------+
| s_year |
+--------+
|   2002 |
|   2000 |
|   2003 |
|   1981 |
+--------+
4 rows in set (0.00 sec)

在这里插入图片描述

5、枚举类型

字段的值不能自己输入,必须在设置的范围内选择(有单选和多选之分)
enum 单选
格式:字段名 enum(值1,值2,值N)
仅能在列表里选择一个值
set 多选
格式:字段名 set(值1,值2,值3)
在列表里选择一个或多个值

创建t7表,指定字段:姓名(name),性别(sex),爱好(likes)


mysql> create table t7(name char(15), sex enum("boy", "girl", "no"), likes set("eat", "money", "game", "music"));
Query OK, 0 rows affected (0.00 sec)

mysql> desc t7;
+-------+-----------------------------------+------+-----+---------+-------+
| Field | Type                              | Null | Key | Default | Extra |
+-------+-----------------------------------+------+-----+---------+-------+
| name  | char(15)                          | YES  |     | NULL    |       |
| sex   | enum('boy','girl','no')           | YES  |     | NULL    |       |
| likes | set('eat','money','game','music') | YES  |     | NULL    |       |
+-------+-----------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql>  insert into t7 values('bob','boy','eat,game,music');
Query OK, 1 row affected (0.01 sec)

mysql> desc t7;
+-------+-----------------------------------+------+-----+---------+-------+
| Field | Type                              | Null | Key | Default | Extra |
+-------+-----------------------------------+------+-----+---------+-------+
| name  | char(15)                          | YES  |     | NULL    |       |
| sex   | enum('boy','girl','no')           | YES  |     | NULL    |       |
| likes | set('eat','money','game','music') | YES  |     | NULL    |       |
+-------+-----------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from t7;
+------+------+----------------+
| name | sex  | likes          |
+------+------+----------------+
| bob  | boy  | eat,game,music |
+------+------+----------------+
1 row in set (0.00 sec)

mysql> insert into t7 values('bob','man','girl,book');
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
mysql> insert into t7 values('bob','no','girl,book');
ERROR 1265 (01000): Data truncated for column 'likes' at row 1

在这里插入图片描述

四、约束条件,修改表结构

1、约束条件

约束条件:限制字段赋值

mysql> desc t6;
+----------+-----------+------+-----+-------------------+-----------------------------+
| Field    | Type      | Null | Key | Default           | Extra                       |
+----------+-----------+------+-----+-------------------+-----------------------------+
| name     | char(10)  | YES  |     | NULL              |                             |
| meetting | datetime  | YES  |     | NULL              |                             |
| party    | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+----------+-----------+------+-----+-------------------+-----------------------------+
Null   Key   Default   Extra    #这四列为约束条件
Null      #指是否允许为字段赋空值; 
 #YES,允许给字段赋空值,默认也是允许赋空值;
 #NO, 不允许给字段赋空值;
Default   #当不给字段赋值时,则使用默认值,初始默认值为NULL,可以修改
Extra     #额外的设置, 例如:可以设置学号为自动增长的

设置约束条件
null 允许为空(默认设置)
not null 不允许为null(空)
key 键值类型
default 设置默认值,缺省为NULL
extra 额外设置

建表时指定默认值不能为空,创建t8表,表字段包含:名字(name),年龄(age),班级(class)

mysql> create table t8(name char(10) not null, age tinyint unsigned default 19, class char(7) not null default "nsd2002");
Query OK, 0 rows affected (0.00 sec)

mysql> desc t8;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| name  | char(10)            | NO   |     | NULL    |       |
| age   | tinyint(3) unsigned | YES  |     | 19      |       |
| class | char(7)             | NO   |     | nsd2002 |       |
+-------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into t8(name) values("john");
Query OK, 1 row affected (0.00 sec)

mysql> select * from t8;
+------+------+---------+
| name | age  | class   |
+------+------+---------+
| john |   19 | nsd2002 |
+------+------+---------+
1 row in set (0.00 sec)

在这里插入图片描述

mysql> # 向t8表中插入数据,所有字段自己定义,可以不使用默认值
mysql> insert into t8 values("tom", 29, "nsd2003");
Query OK, 1 row affected (0.00 sec)

mysql> select * from t8;
+------+------+---------+
| name | age  | class   |
+------+------+---------+
| john |   19 | nsd2002 |
| tom  |   29 | nsd2003 |
+------+------+---------+
2 rows in set (0.00 sec)

在这里插入图片描述
验证null值和"null"值
#null 指的是没有任何的数据
#“null” 指的是有数据,但数据的内容为"null"

mysql> desc t8;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| name  | char(10)            | NO   |     | NULL    |       |
| age   | tinyint(3) unsigned | YES  |     | 19      |       |
| class | char(7)             | NO   |     | nsd2002 |       |
+-------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into t8 values(null,null,null);
ERROR 1048 (23000): Column 'name' cannot be null
mysql> insert into t8 values("null",null,null);
ERROR 1048 (23000): Column 'class' cannot be null
mysql> insert into t8 values("null",null,"");
Query OK, 1 row affected (0.01 sec)

mysql> select * from t8;
+------+------+---------+
| name | age  | class   |
+------+------+---------+
| john |   19 | nsd2002 |
| tom  |   29 | nsd2003 |
| null | NULL |         |
+------+------+---------+
3 rows in set (0.00 sec)

在这里插入图片描述

2、修改表结构

语法结构
用法: mysql> alter table 库名.表名 执行动作;

在这里插入图片描述
添加新字段
在这里插入图片描述

mysql> desc t5;
+----------+----------+------+-----+---------+-------+
| Field    | Type     | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| name     | char(15) | YES  |     | NULL    |       |
| s_year   | year(4)  | YES  |     | NULL    |       |
| uptime   | time     | YES  |     | NULL    |       |
| birthday | date     | YES  |     | NULL    |       |
| party    | datetime | YES  |     | NULL    |       |
+----------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table t5 add email varchar(30) not null default "stu@tedu.cn";
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t5;
+----------+-------------+------+-----+-------------+-------+
| Field    | Type        | Null | Key | Default     | Extra |
+----------+-------------+------+-----+-------------+-------+
| name     | char(15)    | YES  |     | NULL        |       |
| s_year   | year(4)     | YES  |     | NULL        |       |
| uptime   | time        | YES  |     | NULL        |       |
| birthday | date        | YES  |     | NULL        |       |
| party    | datetime    | YES  |     | NULL        |       |
| email    | varchar(30) | NO   |     | stu@tedu.cn |       |
+----------+-------------+------+-----+-------------+-------+
6 rows in set (0.00 sec)

mysql> select * from t5;
+------+--------+----------+------------+---------------------+-------------+
| name | s_year | uptime   | birthday   | party               | email       |
+------+--------+----------+------------+---------------------+-------------+
| bob  |   2002 | 08:30:00 | 2023-11-20 | 2023-02-14 18:30:00 | stu@tedu.cn |
| tom  |   2000 | 20:08:11 | 2023-08-06 | 2023-08-06 20:08:11 | stu@tedu.cn |
| NULL |   2003 | NULL     | NULL       | NULL                | stu@tedu.cn |
| NULL |   1981 | NULL     | NULL       | NULL                | stu@tedu.cn |
+------+--------+----------+------------+---------------------+-------------+
4 rows in set (0.00 sec)

在这里插入图片描述

mysql> alter table t5 add stu_id char(9) first;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t5;
+----------+-------------+------+-----+-------------+-------+
| Field    | Type        | Null | Key | Default     | Extra |
+----------+-------------+------+-----+-------------+-------+
| stu_id   | char(9)     | YES  |     | NULL        |       |
| name     | char(15)    | YES  |     | NULL        |       |
| s_year   | year(4)     | YES  |     | NULL        |       |
| uptime   | time        | YES  |     | NULL        |       |
| birthday | date        | YES  |     | NULL        |       |
| party    | datetime    | YES  |     | NULL        |       |
| email    | varchar(30) | NO   |     | stu@tedu.cn |       |
+----------+-------------+------+-----+-------------+-------+
7 rows in set (0.00 sec)

mysql> select * from t5;
+--------+------+--------+----------+------------+---------------------+-------------+
| stu_id | name | s_year | uptime   | birthday   | party               | email       |
+--------+------+--------+----------+------------+---------------------+-------------+
| NULL   | bob  |   2002 | 08:30:00 | 2023-11-20 | 2023-02-14 18:30:00 | stu@tedu.cn |
| NULL   | tom  |   2000 | 20:08:11 | 2023-08-06 | 2023-08-06 20:08:11 | stu@tedu.cn |
| NULL   | NULL |   2003 | NULL     | NULL       | NULL                | stu@tedu.cn |
| NULL   | NULL |   1981 | NULL     | NULL       | NULL                | stu@tedu.cn |
+--------+------+--------+----------+------------+---------------------+-------------+
4 rows in set (0.00 sec)

mysql>

在这里插入图片描述

mysql> alter table t5 add sex enum("boy", "girl") default "boy" after name;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t5;
+----------+--------------------+------+-----+-------------+-------+
| Field    | Type               | Null | Key | Default     | Extra |
+----------+--------------------+------+-----+-------------+-------+
| stu_id   | char(9)            | YES  |     | NULL        |       |
| name     | char(15)           | YES  |     | NULL        |       |
| sex      | enum('boy','girl') | YES  |     | boy         |       |
| s_year   | year(4)            | YES  |     | NULL        |       |
| uptime   | time               | YES  |     | NULL        |       |
| birthday | date               | YES  |     | NULL        |       |
| party    | datetime           | YES  |     | NULL        |       |
| email    | varchar(30)        | NO   |     | stu@tedu.cn |       |
+----------+--------------------+------+-----+-------------+-------+
8 rows in set (0.00 sec)

mysql> select * from t5;
+--------+------+------+--------+----------+------------+---------------------+-------------+
| stu_id | name | sex  | s_year | uptime   | birthday   | party               | email       |
+--------+------+------+--------+----------+------------+---------------------+-------------+
| NULL   | bob  | boy  |   2002 | 08:30:00 | 2023-11-20 | 2023-02-14 18:30:00 | stu@tedu.cn |
| NULL   | tom  | boy  |   2000 | 20:08:11 | 2023-08-06 | 2023-08-06 20:08:11 | stu@tedu.cn |
| NULL   | NULL | boy  |   2003 | NULL     | NULL       | NULL                | stu@tedu.cn |
| NULL   | NULL | boy  |   1981 | NULL     | NULL       | NULL                | stu@tedu.cn |
+--------+------+------+--------+----------+------------+---------------------+-------------+
4 rows in set (0.00 sec)

在这里插入图片描述

3、修改字段类型

在这里插入图片描述

mysql> # 此案例针对sex字段操作,被修改的字段类型不能与表中已有数据冲突
mysql> desc t5;
+----------+--------------------+------+-----+-------------+-------+
| Field    | Type               | Null | Key | Default     | Extra |
+----------+--------------------+------+-----+-------------+-------+
| stu_id   | char(9)            | YES  |     | NULL        |       |
| name     | char(15)           | YES  |     | NULL        |       |
| sex      | enum('boy','girl') | YES  |     | boy         |       |
| s_year   | year(4)            | YES  |     | NULL        |       |
| uptime   | time               | YES  |     | NULL        |       |
| birthday | date               | YES  |     | NULL        |       |
| party    | datetime           | YES  |     | NULL        |       |
| email    | varchar(30)        | NO   |     | stu@tedu.cn |       |
+----------+--------------------+------+-----+-------------+-------+
8 rows in set (0.00 sec)

mysql> # 使用modify修改t5表的sex字段,设置默认值为man
mysql>
mysql> # #修改失败,字段里需要包含原表中的数据类型boy,否则冲突
mysql> alter table t5 modify sex enum("man", "woman") default "man";
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
mysql>
mysql>
mysql> alter table t5 modify sex enum("man", "woman", "boy") default "man";
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> #修改成功,sex字段中存在和表中数据相同的类型'boy'
mysql>
mysql>
mysql> desc t5;
+----------+---------------------------+------+-----+-------------+-------+
| Field    | Type                      | Null | Key | Default     | Extra |
+----------+---------------------------+------+-----+-------------+-------+
| stu_id   | char(9)                   | YES  |     | NULL        |       |
| name     | char(15)                  | YES  |     | NULL        |       |
| sex      | enum('man','woman','boy') | YES  |     | man         |       |
| s_year   | year(4)                   | YES  |     | NULL        |       |
| uptime   | time                      | YES  |     | NULL        |       |
| birthday | date                      | YES  |     | NULL        |       |
| party    | datetime                  | YES  |     | NULL        |       |
| email    | varchar(30)               | NO   |     | stu@tedu.cn |       |
+----------+---------------------------+------+-----+-------------+-------+
8 rows in set (0.00 sec)

mysql>

在这里插入图片描述修改t5表中的name字段类型,修改为varchar(15)
在这里插入图片描述
案例实现字段值的位置调换
使用modify命令,将email字段移到sex字段的后面

在这里插入图片描述

4、删除字段

在这里插入图片描述
在这里插入图片描述删除t5表中的多个字段(email和birthday)

mysql> alter table t5 drop email,drop birthday;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t5;
+--------+---------------------------+------+-----+---------+-------+
| Field  | Type                      | Null | Key | Default | Extra |
+--------+---------------------------+------+-----+---------+-------+
| name   | varchar(15)               | YES  |     | NULL    |       |
| sex    | enum('man','woman','boy') | YES  |     | man     |       |
| s_year | year(4)                   | YES  |     | NULL    |       |
| uptime | time                      | YES  |     | NULL    |       |
| party  | datetime                  | YES  |     | NULL    |       |
+--------+---------------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

在这里插入图片描述

5、修改字段名

在这里插入图片描述修改t5表中s_year的字段名,使用change命令将字段s_year的名字改为csny


mysql>  alter table t5 change s_year csny year;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t5;
+--------+---------------------------+------+-----+---------+-------+
| Field  | Type                      | Null | Key | Default | Extra |
+--------+---------------------------+------+-----+---------+-------+
| name   | varchar(15)               | YES  |     | NULL    |       |
| sex    | enum('man','woman','boy') | YES  |     | man     |       |
| csny   | year(4)                   | YES  |     | NULL    |       |
| uptime | time                      | YES  |     | NULL    |       |
| party  | datetime                  | YES  |     | NULL    |       |
+--------+---------------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

在这里插入图片描述

6、修改表名

在这里插入图片描述使用rename命令来修改t5表的表名为stuinfo

mysql> alter table t5 rename stuinfo;
Query OK, 0 rows affected (0.01 sec)

mysql> desc t5;
ERROR 1146 (42S02): Table 'bbs.t5' doesn't exist
mysql> desc stuinfo;
+--------+---------------------------+------+-----+---------+-------+
| Field  | Type                      | Null | Key | Default | Extra |
+--------+---------------------------+------+-----+---------+-------+
| name   | varchar(15)               | YES  |     | NULL    |       |
| sex    | enum('man','woman','boy') | YES  |     | man     |       |
| csny   | year(4)                   | YES  |     | NULL    |       |
| uptime | time                      | YES  |     | NULL    |       |
| party  | datetime                  | YES  |     | NULL    |       |
+--------+---------------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> show tables;
+---------------+
| Tables_in_bbs |
+---------------+
| 学生表        |
| stuinfo       |
| t1            |
| t2            |
| t3            |
| t4            |
| t6            |
| t7            |
| t8            |
+---------------+
9 rows in set (0.00 sec)

在这里插入图片描述

五、普通索引

1、MySQL键值概述

键值类型
在这里插入图片描述
index、primary key、foreign key #生产环境一定会用到的键值类型

索引介绍
类似于书的目录
对表中字段值进行排序
索引算法:Btree、B+tree、hash
Btree算法(二叉树):

在这里插入图片描述
#1》查找数字5时,先用数字5和数字4对比;
#2》当数字5大于数字4,则直接从数字4的右分支进行查找;
#3》接下来用要查找的数字5和数字6对比;
#4》当数字5小于数字6,则直接从数字6的左分支进行查找;
#5》按照以上的方式继续比对查找,直到查找到数据为止;
索引的优缺点
在这里插入图片描述生产环境下,对数据查的请求远远高于对数据写的请求;

2、普通索引

在这里插入图片描述创建索引
建表的时候创建索引:index(字段名), index(字段名)…

创建t9表时,将name字段和class字段设置为索引

mysql> create table t9(name char(10), class char(9), sex enum("m", "w"), index(name), index(class));
Query OK, 0 rows affected (0.00 sec)

mysql> desc t9;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| name  | char(10)      | YES  | MUL | NULL    |       |
| class | char(9)       | YES  | MUL | NULL    |       |
| sex   | enum('m','w') | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
#约束条件Key变为MUL(索引的标志)
在已有的表里创建索引:create  index  索引名  on   表名(字段名);

在已有表t4表中为字段创建索引xxx(索引名称可以随便定义)

mysql> desc t4;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name  | char(5)    | YES  |     | NULL    |       |
| pay   | float(5,2) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> create index xxx on t4(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t4;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name  | char(5)    | YES  | MUL | NULL    |       |
| pay   | float(5,2) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> show   index  from   t4 \G;
*************************** 1. row ***************************
        Table: t4
   Non_unique: 1
     Key_name: xxx
 Seq_in_index: 1
  Column_name: name
    Collation: A
  Cardinality: 2
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
1 row in set (0.00 sec)

ERROR:
No query specified

在这里插入图片描述删除索引
drop index 索引名 on 表名;
删除t9表中的索引name

mysql> drop index xxx on t4;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>
mysql> drop index name on t9;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t9;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| name  | char(10)      | YES  |     | NULL    |       |
| class | char(9)       | YES  | MUL | NULL    |       |
| sex   | enum('m','w') | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> show index from t9\G;
*************************** 1. row ***************************
        Table: t9
   Non_unique: 1
     Key_name: class
 Seq_in_index: 1
  Column_name: class
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
1 row in set (0.00 sec)

ERROR:
No query specified

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/62365.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

STM32 DMA学习

DMA简称 DMA&#xff0c;Direct Memory Access&#xff0c;即直接存储器访问。DMA传输方式无需CPU直接控制传输&#xff0c;也没有中断处理方式那样保留现场和恢复现场的过程&#xff0c;通过硬件为RAM与I/O设备开辟一条直接传送数据的通路&#xff0c;能使CPU的效率大为提高。…

K8s中的Ingress

1.把端口号对外暴露&#xff0c;通过ip端口号进行访问 使用Service里面的NodePort实现 2.NodePort缺陷 在每个节点上都会起到端口&#xff0c;在访问时候通过任何节点&#xff0c;通过节点ip暴露端口号实现访问 意味着每个端口只能使用一次&#xff0c;一个端口对应一个应用…

STM32刷Micropython固件参考指南

STM32刷Micropython固件指南 其实刷固件和普通的程序下载烧录无多大的差异&#xff0c;主要是其他因数的影响导致刷固件或刷完固件无法运行的情况和相关问题。 &#x1f4d1;刷固件教程 固件下载。目前所支持的stm32型号有这些&#xff1a; stm32f0, stm32f4, stm32f7, stm32g…

[Docker实现测试部署CI/CD----Jenkins集成相关服务器(3)]

目录 7、 Jenkins 集成 SonarQubeJenkins 中安装 SonarScanner下载移动修改配置文件 8、Jenkins配置SonarQube安装插件添加SonarQube添加 SonarScanner 9、Jenkins集成目标服务器 7、 Jenkins 集成 SonarQube Jenkins 中安装 SonarScanner SonarScanner 是一种代码扫描工具&am…

基于springboot+vue的房屋租赁系统(前后端分离)

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战 主要内容&#xff1a;毕业设计(Javaweb项目|小程序等)、简历模板、学习资料、面试题库、技术咨询 文末联系获取 项目介绍…

MapReduce基础原理、MR与MPP区别

MapReduce概述 MapReduce&#xff08;MR&#xff09;本质上是一种用于数据处理的编程模型&#xff1b;MapReduce用于海量数据的计算&#xff0c;HDFS用于海量数据的存储&#xff08;Hadoop Distributed File System&#xff0c;Hadoop分布式文件系统&#xff09;。Hadoop MapR…

学编程实用网站

牛客网&#xff1a;面试刷题和面试经验分享的网站牛客网 - 找工作神器|笔试题库|面试经验|实习招聘内推&#xff0c;求职就业一站解决_牛客网 (nowcoder.com)https://www.nowcoder.com/ 慕课网&#xff1a;在线学习 慕课网-程序员的梦工厂 (imooc.com)https://www.imooc.com/ …

Vue-组件二次封装

本次对el-input进行简单封装进行演示 封装很简单&#xff0c;就给激活样式的边框(主要是功能) 本次封装主要使用到vue自带的几个对象 $attrs&#xff1a;获取绑定在组件上的所有属性$listeners: 获取绑定在组件上的所有函数方法$slots&#xff1a; 获取应用在组件内的所有插槽 …

【频率派和贝叶斯派】进阶学习-贝叶斯方法原理、基本结构、代码构建+图模型

文章目录 前言1.理论支撑贝叶斯思考模式贝叶斯定理贝叶斯公式 2. 应用转化2.1 拼写检查 3. 贝叶斯网络3.1 贝叶斯网络的定义3.2 三个形式和实际案例的构建关系 前言 频率派与贝叶斯派各自不同的思考方式&#xff1a; 1.频率派把需要推断的参数θ看做是固定的未知数&#xff0c…

亚马逊云科技七项生成式AI新产品生成式AI,为用户解决数据滞后等难题

7月27日&#xff0c;亚马逊云科技在纽约峰会上一连发布了七项生成式AI创新&#xff0c;涵盖了从底层硬件到工具、软件、再到生态的全方位更新&#xff0c;成为它在该领域迄今最全面的一次升级展示&#xff0c;同时也进一步降低了生成式AI的使用门槛。 亚马逊云科技凭借自身端到…

总结950

7:00起床 7:30~8:00复习单词300个&#xff0c;记忆100个 8:10~9:30数学660&#xff0c;只做了10道题&#xff0c;发现对各知识点的掌握程度不一。有些熟练&#xff0c;有些生疏 9:33~10:25计算机网络课程1h 10:32~12:02继续660&#xff0c;也不知道做了几道 2:32~4:00数据…

Cpp学习——string(2)

目录 ​编辑 容器string中的一些函数 1.capacity() 2.reserve() 3.resize() 4.push_back()与append() 5.find系列函数 容器string中的一些函数 1.capacity() capacity是string当中表示容量大小的函数。但是string开空间时是如何开的呢&#xff1f;现在就来看一下。先写…

常见猫咪种类

文章目录 中华田园猫猫图秀概况产地血统毛色特征形态特征性格特征近种区别饲养特点适养人群 英短猫图秀概况产地血统&#xff1a;毛色特征形态特征性格特征近种区别饲养特点适养人群 美短猫图秀概况产地血统毛色特征形态特征性格特征近种区别饲养特点适养人群 布偶猫猫图秀概况…

20天学会rust(一)和rust say hi

关注我&#xff0c;学习Rust不迷路 工欲善其事&#xff0c;必先利其器。第一节我们先来配置rust需要的环境和安装趁手的工具&#xff0c;然后写一个简单的小程序。 安装 Rust环境 Rust 官方有提供一个叫做 rustup 的工具&#xff0c;专门用于 rust 版本的管理&#xff0c;网…

新手注意事项-visual studio 来实现别踩白块儿

自己之前为了熟悉easyx练习过一个简单的项目&#xff0c;别踩白块儿&#xff0c;链接在这里&#xff0c;别踩白块儿&#xff0c;当时比较稚嫩&#xff0c;很多东西都不会&#xff0c;可以说是只知道最基本的语法&#xff0c;头文件都不知道&#xff0c;一个一个查资料弄懂的&am…

CS 144 Lab Four 收尾 -- 网络交互全流程解析

CS 144 Lab Four 收尾 -- 网络交互全流程解析 引言Tun/Tap简介tcp_ipv4.cc文件配置信息初始化cs144实现的fd家族体系基于自定义fd体系进行数据读写的adapter适配器体系自定义socket体系自定义事件循环EventLoop模板类TCPSpongeSocket详解listen_and_accept方法_tcp_main方法_in…

远程调试MySQL内核

1 vscode 需要安装remote-ssh插件 安装成功后&#xff0c;登录&#xff1a; 默认远程服务器的登录 ssh rootip注意&#xff0c;Linux需要设置root远程登录&#xff1b; 2 安装debug扩展 C\C extemsion Pack C\C3 设置Attach进程 {// Use IntelliSense to learn about poss…

MySQL5.7源码编译Debug版本

编译环境Ubuntu22.04LTS 1 官方下载MySQL源码 https://dev.mysql.com/downloads/mysql/?spma2c6h.12873639.article-detail.4.68e61a14ghILh5 2 安装基础软件 cmakeclangpkg-configperl 参考&#xff1a;https://dev.mysql.com/doc/refman/5.7/en/source-installation-prere…

JMeter压力测试记录

最近在学习redis解决高并发下导致数据库数据不准确的问题&#xff0c;使用到了一些工具&#xff0c;包括Jmeter&#xff0c;Redis-desktop-manager.。Jmeter用于模拟高并发情景&#xff0c;Redis-desktop-manager是redis数据库的GUI界面。 一、单元测试生成测试数据 1)插入20…