Home  >  Article  >  Database  >  How to operate the data table of MySQL database with DDL data definition language

How to operate the data table of MySQL database with DDL data definition language

王林
王林forward
2023-06-02 15:55:07685browse

1. Create a data table

Data table: A two-dimensional table. A table is composed of multiple columns. Each category in the table is called a field of the table

How to operate the data table of MySQL database with DDL data definition language

Take the above student information table as an example to create a table in the MySQL database with the following statements (note: the corresponding database should be selected before creation):

create table students(
stu_num char(8) not null unique,
stu_name varchar(20) not null,
stu_gender char(2) not null,
stu_age int not null,
stu_tel char(11) not null unique,
stu_qq varchar(11) unique
);

2. Query the data table

Query all tables in the current database:

show tables;

Query the specified table structure in the current database:

desc 表名字;

How to operate the data table of MySQL database with DDL data definition language

3. Delete the data table

## 删除数据表
drop table 数据表名;

## 当数据表存在时删除数据表
drop table if exists 数据表名;

4. Modify the data table and fields

## 修改表名
alter table 旧表名 rename to 新表名;

## 数据表也是有字符集的,默认字符集和数据库⼀致
alter table 表名 character set utf8;

## 添加列(字段)
alter table 表名 add 字段名 varchar(200);

## 修改列(字段)的列表和类型
alter table 表名 change 旧的字段名 新字段名 字段类型;

## 只修改列(字段)类型
alter table 表名 modify 字段名 新的字段类型;

## 删除列(字段)
alter table stus drop 字段名;

The above is the detailed content of How to operate the data table of MySQL database with DDL data definition language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete