MySQL表的操作

1.创建表

CREATE TABLE table_name (
field1 datatype,
field2 datatype,
field3 datatype
) character set 字符集 collate 校验规则 engine 存储引擎;
fields:表示列名
datatype:数据名
character set:表示字符集
collate :校验规则
engine:存储引擎
字符集 (charset) 决定存什么字节;校验规则 collation 决定怎么比较、排序字符。
MySQL 是插件式架构,存储引擎是 MySQL 负责数据怎么存储、读取、索引实现的底层模块。 SQL 解析、优化器是上层;真正落地读写磁盘、事务、锁、索引,由存储引擎完成。
mysql> create table users(
-> id int,
-> name varchar(20) comment '用户名',
-> password char(32) comment '密码',
-> birthday date comment '生日'
-> )character set utf8 engine MyISAM;

2.查看表结构

desc 表名;
例子:

mysql> desc users;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+

3.修改表

(1)添加列

ALTER TABLE tablename ADD (column datatype DEFAULT expr,column datatype...);
mysql> alter table users add ( weight int);

Query OK, 0 rows affected (0.05 sec)
mysql> alter table users add age int after birthday;

Query OK, 0 rows affected (0.06 sec)

Records: 0 Duplicates: 0 Warnings: 0

(2)更改列

ALTER TABLE tablename MODIfy (column datatype DEFAULT expr,column datatype...);
mysql> alter table users modify weight float;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

(3)删除列

ALTER TABLE tablename DROP (column);
mysql> alter table users drop weight;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0

(4)修改表名

mysql> alter table users rename to students;

(5)修改列名

mysql> alter table students change name xingming varchar(60);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0

(6) 删除表

mysql> drop table students;
Query OK, 0 rows affected (0.03 sec)

相关推荐
lbb 小魔仙1 小时前
时序数据库是什么?2026 年五款工业时序数据库横评与场景解析
数据库·时序数据库
零基础1231 小时前
权限管理怎么做?从“资格”与“空间”双维度设计一套可落地的权限体系
java·服务器·数据库
小小龙学IT1 小时前
PostgreSQL 服务端机制与 SQL 实战深度解析:从 MVCC 到查询优化器
数据库·sql·postgresql
l1t2 小时前
DeepSeek总结的DuckDB 从 JSON 字符串到类型化列
数据库·sql·json
Raas1002 小时前
AI网关能做调用审计吗?MAI Gateway(魔芋企业级AI网关)实战能力深度解读
java·服务器·网络·数据库·gateway·prompt·企业级产品
foolishlee2 小时前
openGauss同步提交
数据库
bksczm2 小时前
MySQL进阶篇之备份(下半篇)
数据库·mysql
计算机就业还在扩大2 小时前
Redis中的setnx是原子性的?
数据库·redis·缓存
updayday8542 小时前
供应商补交资质文件时,采购难以核对缺件和新版本 - Ping64 文件收集
大数据·网络·数据库·安全·智能路由器