Common MySQL Commands and Basic Database/Table Operations
This article presents a concise guide to the most frequently used MySQL commands, covering how to list databases and tables, create and drop databases and tables, and perform essential CRUD operations such as inserting, querying, updating, and deleting records.
Common MySQL Commands
1、显示数据库列表:
show databases;
2、显示库中的数据表:
show tables;
3、显示数据表的结构:
describe 表名;
4、建库:
create database 库名;
5、建表:
create table 表名 (字段设定列表);
6、删库和删表:
drop database 库名;
drop table 表名;
7、将表中记录清空:
delete from 表名; // content cleared, auto‑increment remains
truncate table users; // removes all rows and resets auto‑increment
8、显示表中的记录:
select * from 表名;Basic Database Operations
1. 创建数据库:
mysql> create database ceshi;
2. 连接数据库:
mysql> use ceshi;
3. 查看当前使用的数据库:
mysql> select database();
4. 查看当前数据库包含的表信息:
mysql> show tables;
5. 删除数据库:
mysql> drop database ceshi;Basic Table Operations
一、建表
1. 命令: create table <表名> (<字段名1> <类型1>, ..., <字段名n> <类型n>);
1.1 例子:
mysql> create table Class(
id int(4) not null primary key auto_increment,
name varchar(25) not null,
age int(4) not null default '0'
);
二、获取表结构
2. 命令: desc 表名; 或 show columns from 表名;
2.1 例子:
mysql> desc Class;
mysql> describe Class;
mysql> show columns from Class;
三、插入数据
3. 命令: insert into <表名> [(<字段名1>, ...)] values (值1), (值2), ...;
3.1 例子:
mysql> insert into Class values(1,'Wrry',26),(2,'ZJW',28);
四、查询表中的数据
4. 查询所有行:
mysql> select * from Class;
4.1 查询前几行数据:
mysql> select * from Class limit 0,3;
或
mysql> select * from Class order by id limit 0,3;
五、删除表中数据
5.1 命令: delete from 表名 where 表达式;
5.2 例子: 删除表 Class 中 id 为 1 的记录
mysql> delete from Class where id=1;
六、修改表中数据
6. 命令: update 表名 set 字段=新值, ... where 条件;
6.1 例子:
mysql> update Class set name='AI' where id=1;
七、在表中增加字段
7. 命令: alter table 表名 add 字段 类型 其他;
7.1 例子: 在表 Class 中添加字段 sex,类型为 varchar(25),默认值为 '0'
mysql> alter table Class add sex varchar(25) default '0';
八、更改表名
8. 命令: rename table 原表名 to 新表名;
8.1 例子: 将表 Class 重命名为 MClass
mysql> rename table Class to MClass;
九、删除表
9. 命令: drop table <表名>;
9.1 例子: 删除表 MClass
mysql> drop table MClass;After reviewing the article, readers are encouraged to like and share the content as a sign of support.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
