Operate the database

Operate the database

1. DDL: The database definition language operates on the database and table structure to build a database, build a table, modify the table structure, delete a database, delete a table, etc.

(SQL statements are not case sensitive)

1. Create a database

 CREATE DATABASE [IF NOT EXISTS] dbname  [charset=utf8 collate utf8_general_ci];
	create database student;

Insert picture description here

2. Show all databases

  SHOW DATABASES;

Insert picture description here

3. Switch to the database to be used, USE database name;
Insert picture description here

4.4. Delete the database!

DROP DATABASE [IF EXISTS] 数据库名;
drop database student;

Insert picture description here

5. Build a table

5.建表
  CREATE TABLE [IF NOT EXISTS] tablename(字段名 字段类型,....)[charset=utf8];
  create table if not exists student(id integer,name varchar(20),age integer);
  charset=utf8;

Insert picture description here

6. Display all tables in the current database

SHOW TABLES;

Insert picture description here

7. Display the table building statement

SHOW CREATE TABLE 表名;
show create table student;

Insert picture description here

8. Add a column to the table

  ALTER TABLE 表名 ADD 字段名 字段类型;
  ALTER TABLE 表名 ADD (字段名 字段类型,...)
  alter table student add sex varchar(5);

Insert picture description here

9. Modify the column definition

  ALTER TABLE 表名 MODIFY 字段名 新字段类型;
  alter table student modify sex text;


  ALTER TABLE 表名 CHANGE 旧列名 新列名 新列定义;
  alter table student change sex 性别 varchar(5);

  modify无法修改列名,change可以修改列名

Insert picture description here

10.Delete columns

ALTER TABLE 表名 DROP 列名;
alter table student drop 性别;

Insert picture description here

11. Delete table

 DROP TABLE [IF EXISTS] 表名;
drop table student;

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45874107/article/details/114989854