博客
关于我
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
阅读量:789 次
发布时间:2023-02-11

本文共 13159 字,大约阅读时间需要 43 分钟。

mysql表中数据表的各种操作,创建表、添加各类约束、查看表结构、修改和删除表。这次带你捋清楚,从头再走一遍。

实验目的

创建、修改和删除表,掌握数据表的基本操作。

实验结果

创建数据库company,按照以下两个表结构在company数据库中创建两个数据表officesemployees

表1(offices):

图片

表2(employees):

图片

实验过程

1、登录数据库

PS C:\Users\22768> mysql -uroot -pEnter password: *************Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 9Server version: 8.0.29 MySQL Community Server - GPLCopyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>

2、创建数据库

创建数据库company

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

3、进入数据库

切换到我们新创建的数据库中;

mysql> use company;Database changedmysql>

4、创建表一

创建数据表offices

mysql> create table offices (officeCode int not null unique, city varchar(50) not null, address varchar(50) not null, country varchar(50) not null, postalCode varchar(15) not null, primary key (officeCode));Query OK, 0 rows affected (0.06 sec)mysql> desc offices;+------------+-------------+------+-----+---------+-------+| Field      | Type        | Null | Key | Default | Extra |+------------+-------------+------+-----+---------+-------+| officeCode | int         | NO   | PRI | NULL    |       || city       | varchar(50) | NO   |     | NULL    |       || address    | varchar(50) | NO   |     | NULL    |       || country    | varchar(50) | NO   |     | NULL    |       || postalCode | varchar(15) | NO   |     | NULL    |       |+------------+-------------+------+-----+---------+-------+5 rows in set (0.01 sec)mysql>

5、创建表二

创建数据库employees

mysql> create table employees (employeeNumber int not null primary key auto_increment, lastname varchar(50) not null, firstname varchar(50) not null, mobile varchar(25) not null, officeCode int not null, jobTitle varchar(50) not null, birth datetime, note varchar(255), sex varchar(5), constraint office_fk foreign key(officeCode) references offices(officeCode));Query OK, 0 rows affected (0.05 sec)mysql> desc employees;+----------------+--------------+------+-----+---------+----------------+| Field          | Type         | Null | Key | Default | Extra          |+----------------+--------------+------+-----+---------+----------------+| employeeNumber | int          | NO   | PRI | NULL    | auto_increment || lastname       | varchar(50)  | NO   |     | NULL    |                || firstname      | varchar(50)  | NO   |     | NULL    |                || mobile         | varchar(25)  | NO   |     | NULL    |                || officeCode     | int          | NO   | MUL | NULL    |                || jobTitle       | varchar(50)  | NO   |     | NULL    |                || birth          | datetime     | YES  |     | NULL    |                || note           | varchar(255) | YES  |     | NULL    |                || sex            | varchar(5)   | YES  |     | NULL    |                |+----------------+--------------+------+-----+---------+----------------+9 rows in set (0.00 sec)mysql>

6、mysql字段移动

将数据表employeesmobile字段修改到officeCode字段后面;

mysql> alter table employees modify mobile varchar(25) after officeCode;Query OK, 0 rows affected (0.07 sec)Records: 0  Duplicates: 0  Warnings: 0mysql>  desc employees;+----------------+--------------+------+-----+---------+----------------+| Field          | Type         | Null | Key | Default | Extra          |+----------------+--------------+------+-----+---------+----------------+| employeeNumber | int          | NO   | PRI | NULL    | auto_increment || lastname       | varchar(50)  | NO   |     | NULL    |                || firstname      | varchar(50)  | NO   |     | NULL    |                || officeCode     | int          | NO   | MUL | NULL    |                || mobile         | varchar(25)  | YES  |     | NULL    |                || jobTitle       | varchar(50)  | NO   |     | NULL    |                || birth          | datetime     | YES  |     | NULL    |                || note           | varchar(255) | YES  |     | NULL    |                || sex            | varchar(5)   | YES  |     | NULL    |                |+----------------+--------------+------+-----+---------+----------------+9 rows in set (0.00 sec)mysql>

7、mysql字段修改

将数据表employeesbirth字段改名为employee_birth

mysql> alter table employees change birth employee_birth datetime;Query OK, 0 rows affected (0.03 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> desc employees;+----------------+--------------+------+-----+---------+----------------+| Field          | Type         | Null | Key | Default | Extra          |+----------------+--------------+------+-----+---------+----------------+| employeeNumber | int          | NO   | PRI | NULL    | auto_increment || lastname       | varchar(50)  | NO   |     | NULL    |                || firstname      | varchar(50)  | NO   |     | NULL    |                || officeCode     | int          | NO   | MUL | NULL    |                || mobile         | varchar(25)  | YES  |     | NULL    |                || jobTitle       | varchar(50)  | NO   |     | NULL    |                || employee_birth | datetime     | YES  |     | NULL    |                || note           | varchar(255) | YES  |     | NULL    |                || sex            | varchar(5)   | YES  |     | NULL    |                |+----------------+--------------+------+-----+---------+----------------+9 rows in set (0.00 sec)mysql>

8、mysql修改字段类型

将数据表sex字段的数据类型,改成char(1),非空约束;

mysql> alter table employees modify sex char(1) not null;Query OK, 0 rows affected (0.08 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> desc employees;+----------------+--------------+------+-----+---------+----------------+| Field          | Type         | Null | Key | Default | Extra          |+----------------+--------------+------+-----+---------+----------------+| employeeNumber | int          | NO   | PRI | NULL    | auto_increment || lastname       | varchar(50)  | NO   |     | NULL    |                || firstname      | varchar(50)  | NO   |     | NULL    |                || officeCode     | int          | NO   | MUL | NULL    |                || mobile         | varchar(25)  | YES  |     | NULL    |                || jobTitle       | varchar(50)  | NO   |     | NULL    |                || employee_birth | datetime     | YES  |     | NULL    |                || note           | varchar(255) | YES  |     | NULL    |                || sex            | char(1)      | NO   |     | NULL    |                |+----------------+--------------+------+-----+---------+----------------+9 rows in set (0.00 sec)mysql>

9、mysql删除字段

删除数据表employees中的note字段;

mysql> alter table employees drop note;Query OK, 0 rows affected (0.02 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> desc employees;+----------------+-------------+------+-----+---------+----------------+| Field          | Type        | Null | Key | Default | Extra          |+----------------+-------------+------+-----+---------+----------------+| employeeNumber | int         | NO   | PRI | NULL    | auto_increment || lastname       | varchar(50) | NO   |     | NULL    |                || firstname      | varchar(50) | NO   |     | NULL    |                || officeCode     | int         | NO   | MUL | NULL    |                || mobile         | varchar(25) | YES  |     | NULL    |                || jobTitle       | varchar(50) | NO   |     | NULL    |                || employee_birth | datetime    | YES  |     | NULL    |                || sex            | char(1)     | NO   |     | NULL    |                |+----------------+-------------+------+-----+---------+----------------+8 rows in set (0.00 sec)mysql>

10、mysql增加字段

在数据表employees中新增字段favoriate_activity,并设置数据类型为varchar(100)

mysql> alter table employees add favoriate_activiry varchar(100);Query OK, 0 rows affected (0.03 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> desc employees;+--------------------+--------------+------+-----+---------+----------------+| Field              | Type         | Null | Key | Default | Extra          |+--------------------+--------------+------+-----+---------+----------------+| employeeNumber     | int          | NO   | PRI | NULL    | auto_increment || lastname           | varchar(50)  | NO   |     | NULL    |                || firstname          | varchar(50)  | NO   |     | NULL    |                || officeCode         | int          | NO   | MUL | NULL    |                || mobile             | varchar(25)  | YES  |     | NULL    |                || jobTitle           | varchar(50)  | NO   |     | NULL    |                || employee_birth     | datetime     | YES  |     | NULL    |                || sex                | char(1)      | NO   |     | NULL    |                || favoriate_activiry | varchar(100) | YES  |     | NULL    |                |+--------------------+--------------+------+-----+---------+----------------+9 rows in set (0.00 sec)mysql>

11、mysql删除表

删除数据表offices

需要注意的是数据表offices中存在着外键,所以我们删除该数据表的时候,需要先删除外键关系,然后再删除这个数据表方可。

(1)mysql删除外键

先看下外键的名字叫啥;

mysql> show create table employees\G;*************************** 1. row ***************************       Table: employeesCreate Table: CREATE TABLE `employees` (  `employeeNumber` int NOT NULL AUTO_INCREMENT,  `lastname` varchar(50) NOT NULL,  `firstname` varchar(50) NOT NULL,  `officeCode` int NOT NULL,  `mobile` varchar(25) DEFAULT NULL,  `jobTitle` varchar(50) NOT NULL,  `employee_birth` datetime DEFAULT NULL,  `sex` char(1) NOT NULL,  `favoriate_activiry` varchar(100) DEFAULT NULL,  PRIMARY KEY (`employeeNumber`),  KEY `office_fk` (`officeCode`),  CONSTRAINT `office_fk` FOREIGN KEY (`officeCode`) REFERENCES `offices` (`officeCode`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.00 sec)ERROR:No query specifiedmysql>

可以看到外键名字是office_fk,然后我们删除employees数据表的外键约束office_fk

mysql> alter table employees drop foreign key office_fk;Query OK, 0 rows affected (0.02 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> show create table employees\G;*************************** 1. row ***************************       Table: employeesCreate Table: CREATE TABLE `employees` (  `employeeNumber` int NOT NULL AUTO_INCREMENT,  `lastname` varchar(50) NOT NULL,  `firstname` varchar(50) NOT NULL,  `officeCode` int NOT NULL,  `mobile` varchar(25) DEFAULT NULL,  `jobTitle` varchar(50) NOT NULL,  `employee_birth` datetime DEFAULT NULL,  `sex` char(1) NOT NULL,  `favoriate_activiry` varchar(100) DEFAULT NULL,  PRIMARY KEY (`employeeNumber`),  KEY `office_fk` (`officeCode`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.00 sec)ERROR:No query specifiedmysql>

可以看到employees数据表中的外键已经删除了,然后我们再返回来删除数据表offices

(2)mysql删除数据表

mysql> drop table offices;Query OK, 0 rows affected (0.03 sec)mysql> show tables;+-------------------+| Tables_in_company |+-------------------+| employees         |+-------------------+1 row in set (0.00 sec)mysql>

我们删除了数据表之后查询确实只有一个表了。

12、mysql修改存储引擎

employees数据表的存储引擎修改为myisam

(1)查看数据表employees现在的存储引擎是什么;

mysql> show create table employees\G;*************************** 1. row ***************************       Table: employeesCreate Table: CREATE TABLE `employees` (  `employeeNumber` int NOT NULL AUTO_INCREMENT,  `lastname` varchar(50) NOT NULL,  `firstname` varchar(50) NOT NULL,  `officeCode` int NOT NULL,  `mobile` varchar(25) DEFAULT NULL,  `jobTitle` varchar(50) NOT NULL,  `employee_birth` datetime DEFAULT NULL,  `sex` char(1) NOT NULL,  `favoriate_activiry` varchar(100) DEFAULT NULL,  PRIMARY KEY (`employeeNumber`),  KEY `office_fk` (`officeCode`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.00 sec)ERROR:No query specifiedmysql>

(2)修改数据表的存储引擎;

mysql> alter table employees engine=myisam;Query OK, 0 rows affected (0.06 sec)Records: 0  Duplicates: 0  Warnings: 0mysql>

(3)再次查看数据表的存储引擎;

mysql> show create table employees\G;*************************** 1. row ***************************       Table: employeesCreate Table: CREATE TABLE `employees` (  `employeeNumber` int NOT NULL AUTO_INCREMENT,  `lastname` varchar(50) NOT NULL,  `firstname` varchar(50) NOT NULL,  `officeCode` int NOT NULL,  `mobile` varchar(25) DEFAULT NULL,  `jobTitle` varchar(50) NOT NULL,  `employee_birth` datetime DEFAULT NULL,  `sex` char(1) NOT NULL,  `favoriate_activiry` varchar(100) DEFAULT NULL,  PRIMARY KEY (`employeeNumber`),  KEY `office_fk` (`officeCode`)) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.00 sec)ERROR:No query specifiedmysql>

13、mysql修改表名

将数据表employees的数据表名修改成employees_info

mysql> alter table employees rename employees_info;Query OK, 0 rows affected (0.01 sec)mysql> show tables;+-------------------+| Tables_in_company |+-------------------+| employees_info    |+-------------------+1 row in set (0.00 sec)mysql>

至此,本文结束。

更多内容请转至VX公众号 “运维家” ,获取最新文章。

------ “运维家” ------

------ “运维家” ------

------ “运维家” ------

系统运维工程师面试,运维工程师优秀员工提名词,tr运维工程师,特来电运维工程师工作日常,IT运维工程师高级;

智能制造运维工程师培训课程,远程办公的运维工程师,迈瑞医疗运维工程师工资待遇,后台运维工程师是做什么的;
风力运维工程师怎样,浪潮云运维工程师,医疗设备运维工程师证书样本,运维工程师男朋友,运维工程师暴躁。

转载地址:http://zvbfk.baihongyu.com/

你可能感兴趣的文章
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>