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

news2024/11/22 19:17:23

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

在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Azure Developer】使用 Microsoft Graph API 获取 AAD User 操作示例

问题解答 使用Microsoft Graph API&#xff0c;演示如何获取AAD User信息&#xff0c;因参考文档是针对Global Azure&#xff0c;所以文档种的URL为&#xff1a; // Global Azure Microsoft Graph API Host GET https://graph.microsoft.com/v1.0/me 需要修改为 // 中国区A…

iperf 安装与使用

iperf命令是一个网络性能测试工具&#xff0c;可以测试TCP和UDP带宽质量。同时也可以通过UDP测试报告网丢包率或者发包性能&#xff0c;是一个非常实用的工具 1.windwos安装 可以直接通过官网下载对应系统版本进行安装&#xff08;https://iperf.fr/iperf-download.php&#…

无涯教程-Perl - each函数

描述 在列表context中调用此函数时,将返回一个由2个元素组成的列表,该列表由哈希的下一个元素的键和值组成,以便您可以对其进行迭代。在标量context中调用时,仅返回哈希中下一个元素的键。 语法 以下是此函数的简单语法- each HASH返回值 在列表context中调用此函数时,将返…

红帽认证前景如何,有什么优势?

红帽Linux认证优势: RedHat是全球最大的开源技术厂家&#xff0c;是世界领先的开源解决方案供应商&#xff0c;其产品RedHat Enterprise Linux&#xff08;红帽企业级 Linux&#xff09;也是全世界应用最广泛的Linux。 作为检验Linux技能的黄金标准&#xff0c; RHCE认证项目已…

农商行基于分类分级的数据安全管控建设实践

《数据安全法》颁布实施以来&#xff0c;以分类分级为基础&#xff0c;对数据进行差异化管理和防护&#xff0c;成为行业共识。 金融行业作为数据密集的高地&#xff0c;安全是重中之重&#xff0c;而鉴于金融数据种类和内容庞杂&#xff0c;面临规模化用数、普惠用数、跨机构共…

Selenium自动化测试之学会元素定位

这是我经常被问到的一个问题&#xff0c;也是我很讨厌回答的问题&#xff0c;因为要想回答这个问题需要知道上下文。什么样式的元素&#xff0c;有哪些属性&#xff0c;是否有表单嵌套&#xff0c;都尝试了哪些定位方法。。。而且没几个提问者能事先详细的说明这些。哪儿像提bu…

[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

1.今天开发了一套服务程序&#xff0c;使用的是Odbc连接MySql数据库&#xff0c; 在我本机用VS打开程序时&#xff0c;访问一切正常&#xff0c;当发布出来装在电脑上&#xff0c;连接数据库时提示&#xff1a; [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定…

Python Opencv实践 - 基本图像IO操作

import numpy as np import cv2 as cv import matplotlib.pyplot as plt#读取图像 #cv2.IMREAD_COLOR&#xff1a; 读取彩色图像&#xff0c;忽略alpha通道&#xff0c;也可以直接写1 #cv2.IMREAD_GRAYSCALE: 读取灰度图&#xff0c;也可以直接写0 #cv2.IMREAD_UNCHANGED: 读取…

跨平台开发框架Qt:面向对象、丰富API

Qt是一个跨平台C图形用户界面应用程序开发框架&#xff0c;它具有以下三大优势&#xff1a; 优良的跨平台特性&#xff1a;Qt支持多种操作系统&#xff0c;包括Windows、Linux、Solaris、HP-UX、Irix、FreeBSD等&#xff0c;使开发人员能够在不同平台上开发和部署应用程序&…

如何在业务中体现TCC事务模型?

在分布式系统设计中&#xff0c;随着微服务的流行&#xff0c;通常一个业务操作被拆分为多个子任务&#xff0c;比如电商系统的下单和支付操作&#xff0c;就涉及到了创建和更新订单、扣减账户余额、扣减库存、发送物流消息等&#xff0c;那么在复杂业务开发中&#xff0c;如何…

【多重信号分类】超分辨率测向方法——依赖于将观测空间分解为噪声子空间和源/信号子空间的方法具有高分辨率(HR)并产生准确的估计(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

ApplicationContent 和BeanFactory的关系

选中这个类&#xff0c; ctrl alt u 从springboot的启动类说起 我们知道这个启动类返回值呢是哟个configurableApplicationContent 类型 我们查看她的类图 从图中我们可以看出&#xff0c;configurableApplicationContent是见解的继承了BeanFactory接口&#xff0c;扩展了他…

Centos7克隆快速复制多台虚拟机|互通互联

背景&#xff1a;有时候&#xff0c;我们在用虚拟机的时候会用到多个进行使用。重新安装会花费大量的时间&#xff0c;此时&#xff0c;我们可以通过vmware虚拟机自带的功能快速克隆出完全相同的系统。 前提&#xff1a;被克隆的虚拟机系统要处于关闭状态 步骤&#xff1a;…

elevation mapping学习笔记2之高程图输入、输出、服务和参数配置的定义和说明

文章目录 0 引言1 话题Topics1.1 订阅subscribe1.2 发布publish 2 服务Services3 参数Parameters 0 引言 elevation mapping学习笔记1已经成功编译安装elevation mapping高程图工程&#xff0c;并运行示例turtlesim3_waffle_demo&#xff0c;在仿真环境下&#xff0c;控制带有…

九、ESP32控制1602LCD屏幕显示内容

1. 运行效果 2. 1602简介 1602LCD是指显示的内容为16X2,即可以显示两行,每行16个字符液晶模块(显示字符和数字) 市面上字符液晶大多数是基于HD44780液晶芯片的 需要8根数据线来传输要显示的数据,当然还

每天一道leetcode:剑指 Offer 04. 二维数组中的查找(中等二分查找)

今日份题目&#xff1a; 在一个 n * m 的二维数组中&#xff0c;每一行都按照从左到右 非递减 的顺序排序&#xff0c;每一列都按照从上到下 非递减 的顺序排序。请完成一个高效的函数&#xff0c;输入这样的一个二维数组和一个整数&#xff0c;判断数组中是否含有该整数。 示…

EP4CE6E22C8 FPGA最小系统电路原理图+PCB源文件

资料下载地址&#xff1a;EP4CE6E22C8 FPGA最小系统电路原理图PCB源文件 一、原理图 二、PCB

Windows 安装Tensorflow2.1、Pycharm开发环境

文章目录 1、安装anaconda2、安装Tensoflow2.1、创建虚拟环境2.2、安装Tensorflow依赖2.3、验证Tensorflow是否成功 3、配置pycharm环境4、错误记录 1、安装anaconda https://www.anaconda.com/download 打开命令行工具&#xff0c;出现base就表示安装成功了&#xff0c;表示当…

SQL-每日一题【1204. 最后一个能进入巴士的人】

题目 表: Queue 有一队乘客在等着上巴士。然而&#xff0c;巴士有1000 千克 的重量限制&#xff0c;所以其中一部分乘客可能无法上巴士。 写一条 SQL 查询语句找出 最后一个 上巴士且不超过重量限制的乘客&#xff0c;并报告 person_name 。题目测试用例确保顺位第一的人可以…

学编程实用网站

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