
准备工作登录格式mysql-u用户名-h服务器的主机地址-p密码显示信息解释Commands end with; or\gmysql命令是以分号;或“\g”来结束的遇到这个结束符就开始执行命令Your MySQL connection id is 22数据库的连接次数Server version: 8.0.37 Source distribution 版本Type help; or \h for help输入”help;“或者”\h“可以看到帮助信息[rootserver~]# systemctl start mysqld#必须先启动服务否则报错如下所述报错信息[rootserver~]# mysql -u root -pEnterpassword:ERROR2002(HY000): Cant connect to local MySQL server through socket/var/lib/mysql/mysql.sock (2)mysql-u用户名-h服务器的主机地址-p密码[rootserver~]# mysql -uroot -pEnterpassword:Welcometothe MySQL monitor. Commandsend with;or\g.Your MySQLconnectionidis16Serverversion:8.0.37MySQL CommunityServer-GPLCopyright(c)2000,2024, Oracleand/or its affiliates.Oracleisa registered trademark of Oracle Corporationand/or itsaffiliates. Other names may be trademarks of their respectiveowners.Typehelp;or\hfor help. Type\ctoclearthecurrentinput statement.Type \c to clear the current input statement表示遇到”\c“就清除前面的命令注意不是清屏退出exit quit \q查看数据库创建数据库切换数据库#新建数据库create database数据库名;#设为当前数据库use数据库名;#查看当前使用的数据库select database();#显示所有数据库show databases;#删除数据库drop database数据库名;创建表查看表删除表#新建表create tables表名(列名1类型 列名2类型……);#查看表结构desc表名;#修改表名alter table旧表名rename新表名;#表中添加新列alter table表名add新列名 列类型[after|first]列名;after为之后#删除列alter table表名DROP列名;#修改列名alter table表名change旧列名 新列名 列类型;#修改列类型alter table表名modify列名 列类型;删除的注意事项Caution1.删除数据库表之前一定要备份数据以免数据丢失。2.在删除数据库表之前要确保该表没有被其他的表所引用否则可能会破坏外键约束。3.在删除数据库表之前要确保该表的数据已经备份并且不再需要使用。4.在删除数据库表之前要确保该表的数据已经被清空否则会破坏外键约束。5.在删除数据库表之前要确保该表的所有索引都已经被删除。如果还存在索引可能会导致删除失败。6.在删除数据库表之前要确保该表的所有相关程序已经停止运行以免影响其他程序的正常运行。