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: 0mysql> 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: 0mysql> 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: 0mysql>
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 changedmysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> #修改成功,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: 0mysql> 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: 0mysql> 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: 0mysql> 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: t4Non_unique: 1Key_name: xxxSeq_in_index: 1Column_name: nameCollation: ACardinality: 2Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
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: 0mysql>
mysql> drop index name on t9;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> 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: t9Non_unique: 1Key_name: classSeq_in_index: 1Column_name: classCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
Index_comment:
1 row in set (0.00 sec)ERROR:
No query specified

在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Django实现音乐网站 ⑷

使用Python Django框架制作一个音乐网站&#xff0c;在系列文章3的基础上继续开发&#xff0c; 本篇主要是后台歌曲类型表、歌单表模块功能开发。 目录 表结构设计 歌曲类型表结构 歌单表结构 创建表模型 创建表 后台注册表模型 引入表模型 后台自定义 总结 表结构设计…

在.net 6.0中 调用远程服务器web服务,Webservices(xxx.asmx) ,RESTful 风格,2种解决方案。

1.使用 Connected Services&#xff1a; 右键单击您的项目&#xff0c;选择 "Add"&#xff08;添加&#xff09;-> "Connected Services"&#xff08;已连接的服务&#xff09;。 在 "Connected Services" 对话框中&#xff0c;选择 "W…

Gitlab CI/CD笔记-第一天-GitOps和以前的和jenkins的集成的区别

一、GitOps-CI/CD的流程图与Jenkins的流程图 从上图可以看到&#xff1a; GitOps与基于Jennkins技术栈的CI/CD流程&#xff0c;无法从Jenkins集成其他第三方开源的项目来实现换成了Gitlab来进行集成。 好处在于&#xff1a;CI 一个工具Gitlab就行了&#xff0c;但CD部分依旧是…

SpringBoot + Docker 实现一次构建到处运行~

一、容器化部署的好处 图片 Docker 作为一种新兴的虚拟化方式&#xff0c;它可以更高效的利用系统资源&#xff0c;不需要进行硬件虚拟以及运行完整操作系统等额外开销。 传统的虚拟机技术启动应用服务往往需要数分钟&#xff0c;而 Docker 容器应用&#xff0c;由于直接运行…

关于Java的IO流开发

IO概述 回想之前写过的程序&#xff0c;数据都是在内存中&#xff0c;一旦程序运行结束&#xff0c;这些数据都没有了&#xff0c;等下次再想使用这些数据&#xff0c;可是已经没有了。那怎么办呢&#xff1f;能不能把运算完的数据都保存下来&#xff0c;下次程序启动的时候&a…

嵌入式该往哪个方向发展?

1. 你所在的城市嵌入式Linux岗位多吗&#xff1f;我觉得这是影响你做决定的另一个大问题。我们学嵌入式Linux这门技术&#xff0c;绝大部分人是为了从事相关的工作&#xff0c;而不是陶冶情操。但是根据火哥统计来看&#xff0c;嵌入式Linux的普遍薪资虽然高于单片机&#xff0…

【论文阅读】UNICORN:基于运行时来源的高级持续威胁检测器(NDSS-2020)

UNICORN: Runtime Provenance-Based Detector for Advanced Persistent Threats NDSS-2020 哈佛大学 Han X, Pasquier T, Bates A, et al. Unicorn: Runtime provenance-based detector for advanced persistent threats[J]. arXiv preprint arXiv:2001.01525, 2020. 源码&…

IDEA中怎么使用git下载项目到本地,通过URL克隆项目(giteegithub)

点击 新建>来自版本控制的项目 点击后会弹出这样一个窗口 通过URL拉取项目代码 打开你要下载的项目仓库 克隆>复制 gitee github也是一样的 返回IDEA 将刚刚复制的URL粘贴进去选择合适的位置点击克隆 下载完成

新式健身房,如何实现都市人的健身自由?

在中国超40万亿的庞大消费市场中&#xff0c;从来不缺少叙事宏大的故事。 只不过&#xff0c;像突破万家门店这样的故事&#xff0c;往往出现在餐饮、医药、零售等行业的头部玩家身上&#xff0c;比如瑞幸、蜜雪冰城、华莱士、益丰药房、美宜佳等品牌。 健身房这个文化体育领…

ORB-SLAM2配置与安装

本篇博客最早发布于实验室公共博客&#xff0c;但已无人维护&#xff0c;现迁移至个人博客 有这些依赖项&#xff1a; https://github.com/raulmur/ORB_SLAM2 主要参考下面的博文 ORB-SLAM2 初体验 —— 配置安装 - MingruiYu - 博客园 (cnblogs.com) 注意在安装依赖项Pangoli…

详解Quest 2积分与奖励规则

7月28日&#xff0c;在万众期待中&#xff0c;Mysten Labs在Quest门户网站上宣布了Quest 2的到来。经过严密的筹划&#xff0c;本着真实、公平以及用户至上的原则&#xff0c;现在向大家介绍Quest 2的积分规则以及奖励规则。 温馨提示&#xff1a;第一轮Bullshark Quest是一次精…

Docker 网络模型使用详解 (1)Dockers网络基础

目录 环境准备 Dockers 网络基础 1.端口映射 查看随机映射端口范围 -p可以指定映射到本地端口 映射指定地址和指定端口 映射指定地址 宿主机端口随机分配 指定传输协议 端口暴露 容器互联 自定义网络 现在把container7加入到demo_net中 在启动一个容器加入到demo_net…

从0到1开发go-tcp框架【4实战片— — 开发MMO之玩家聊天篇】

从0到1开发go-tcp框架【实战片— — 开发MMO】 MMO&#xff08;MassiveMultiplayerOnlineGame&#xff09;&#xff1a;大型多人在线游戏&#xff08;多人在线网游&#xff09; 1 AOI兴趣点的算法 游戏中的坐标模型&#xff1a; 场景相关数值计算 ● 场景大小&#xff1a; 250…

AI 绘画Stable Diffusion 研究(四)sd文生图功能详解(上)

大家好&#xff0c;我是风雨无阻。 通过前面几篇AI 绘画Stable Diffusion 研究系列的介绍&#xff0c;我们完成了Stable Diffusion整合包的安装、模型ControlNet1.1 安装、模型种类介绍与安装&#xff0c;相信看过教程的朋友们&#xff0c;手上已经有可以操作实践的Stable Diff…

UE4查看加密PAK里边的资源Android/iOS/PC方法

我们经常会需要把1个模型进行减面然后在移动端使用,有时候会出现移动端模型和PC端模型不一致的问题,这时候就需要将移动端的模型和PC端的模型进行对比,找到问题出现的原因,检查Mesh、Normal、UV0、UV1、MaterialId、碰撞等是否一致。 如何打包Pak文件,见这篇文章:UE4打包…

C语言假期作业 DAY 15

一、选择题 1、有如下代码&#xff0c;则 *(p[0]1) 所代表的数组元素是&#xff08; &#xff09; int a[3][2] {1, 2, 3, 4, 5, 6}, *p[3]; p[0] a[1]; A: a[0][1] B: a[1][0] C: a[1][1] D: a[1][2] 答案解析 正确答案&#xff1a; C p 是一个指针数组&#xff0c; p[0] a…

备忘录模式(C++)

定义 在不破坏封装性的前提下&#xff0c;捕获一-个对象的内部状态&#xff0c;并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态。 应用场景 ➢在软件构建过程中&#xff0c;某些对象的状态在转换过程中&#xff0c;可能由于某种需要&#xff0c;要…

【Java设计模式】建造者模式 注解@Builder

概念 将一个复杂对象的构造与它的表示分离&#xff0c;使同样的构建过程可以创建不同的表示。它使将一个复杂的对象分解成多个简单的对象&#xff0c;然后一步步构建而成。 每一个具体建造者都相对独立&#xff0c;而与其它的具体建造者无关&#xff0c;因此可以很方便地替换具…

JavaWeb 手写Tomcat底层机制

目录 一、Tomcat底层整体架构 1.简介 : 2.分析图 : 3.基于Socket开发服务端的流程 : 4.打通服务器端和客户端的数据通道 : 二、多线程模型的实现 1.思路分析 : 2.处理HTTP请求 : 3.自定义Tomcat : 三、自定义Servlet规范 1. HTTP请求和响应 : 1 CyanServletRequest …