SQL DDL-表操作-修改
2023-09-12 本文已影响0人
大范de学习笔记
1、DDL-表操作-修改-添加字段
为emp增加add一个字段-nickname-varchar (20)
用alter table
备注:回忆第一节课我们学习的创建表 create table

2、修改数据类型;修改字段名和字段类型
修改数据类型
alter table 表名 modify 字段名 新数据类型(长度)
修改字段名和字段类型
alter table 表名 旧字段名 新字段名 类型(长度) comment 'XX'

3、删除字段
把username 字段删除
alter table emp drop username;

4、修改表名
使用语法
alter table 表名 rename to 新表名
案例:把emp表名改成employee

5、删除表、删除制定表,并重新创建该表
删除表:drop tables (if exists) 表名
删除制定表,并重新创建该表:truncate table 表名;


总体上删除直接用drop table 表名即可
总结:
1、DDL--数据库操作
show databases;
creat databases 数据库名
use数据库名;
select databases ();
drop database 数据库名;
2、DDL--表操作
show tables;
create table 表名 (username varchar(10), age int);
desc 表名;
alter table 表名 (add, modify,change,drop,rename to;
drop table 表名