2

DDL is often used in database operations, such as database additions, deletions, and changes.

create database
CREATE database db_test
or
CREATE database if not exists db_test;
show all databases
SHOW databases;
Modify the database

 ALTER database db_test character set gbk;

delete database
DROP database db_test;
or
DROP database if exists db_test ;
Select database to operate
USE db_test


After selecting the database, we can create the data table:
Creating a data table <br>There are many details in creating a data table. The following is an example directly:

 CREATE TABLE students(
    -> stu_num char(8) not null unique,
    -> stu_name varchar(20) not null,
    -> stu_age int not null,
    -> stu_tel char(11) not null unique,
    -> stu_qq varchar(11) unique
    -> );

View the structure of the data table
desc students;
The results are as follows:

 +-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| stu_num   | char(8)     | NO   | PRI | NULL    |       |
| stu_name  | varchar(20) | NO   |     | NULL    |       |
| stu_age   | int         | NO   |     | NULL    |       |
| stu_tel   | char(11)    | NO   | UNI | NULL    |       |
| stu_qq    | varchar(11) | YES  | UNI | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

delete data table
DROP TABLE if exists <tablename> ;
Modify data table <br>Modify data table name
ALTER TABLE students rename to stus;
Modify character set
ALTER TABLE stus character set utf8;
add field
ALTER TABLE stus add stu_remark varchar(200);
Modify field names and types and not-null constraints

 ALTER TABLE stus change stu_nbame stu_name varchar(20) not null;

delete field
ALTER TABLE stus drop stu_remark;

Reference: bilibili


LiberHome
409 声望1.1k 粉丝

有问题 欢迎发邮件 📩 liberhome@163.com