一:前置条件
在配置了系统环境变量path之后(添加MySQL的bin路径),通过在cmd界面输入以下指令mysql -u root -p,并输入正确的密码后,如果出现下图的 mysql> ,则表明环境变量配置成功。
C:\Users\DELLofXTQ>mysql -u root -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.37 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
二:创建并查看数据库指令
mysql> create database if not exists student;
mysql> show databases;
三:创建并查看table
通过use student指令,表明下面创建的table是在student数据库中创建的。
mysql> create table stu(
-> id int,
-> name varchar(20),
-> gender char(1),
-> birthday date,
-> score double(5,2),
-> addr varchar(50),
-> status tinyint);
通过show tables查看table:
四:向table中进行数据的增删改查操作
4.1插入数据
mysql> insert stu values(1,'jim','男','2025-10-02',97.82,'无锡市新吴区',1);
mysql> insert stu values(1,'merry','女','2025-07-05',97.82,'无锡市滨湖区',1);
mysql> select * from stu;
4.2删除数据
SQL格式:
delete from 表名 [where 条件];
如将addr为无锡市滨湖区的数据删除
mysql> delete from stu where addr='无锡市滨湖区';
4.3修改数据
SQL格式:
update 表名 set 列名1=值1,列名2=值2...[where 条件];
如将merry的id修改为2,XuChaos的id修改为3
mysql> update stu set id='2' where name='merry';
mysql> update stu set id='3' where name='XuChao';
4.4查询数据
在4.1,4.2,4.3的截图中已可见
五:修改表、删除表
修改表名称:
alter table 表名 rename to 新的表名;
增加一列:
alter table 表名 add 列名 数据类型;
修改数据类型:
alter table 表名 modify 列名 新数据类型;
修改名称和数据类型:
alter table 表名 change 列名 新列名 新数据类型;
删除列:
alter table 表名 drop 列名;
比如:
修改表stu的名称为stu_information,
mysql> alter table stu rename stu_information;
给表student增加一列tel显示学生的联系电话,
mysql> alter table stu_information add tel char(15);
修改联系电话tel列的数据类型为varchar,
mysql> alter table stu_information modify tel varchar(20);
同时修改列名称和数据类型,
mysql> alter table stu_information change addr address varchar(60);
删除电话列
mysql> alter table stu_information drop tel;