SQL_DDL_ operation database/table

Operating database: CRUD

1. C(Create): Create

  • Create a database:
    * create database database name;
  • Create a database, determine that it does not exist, and then create:
    * create database if not exists database name;
  • Create a database, and specify the character set
    * create database database name character set character set name;

Exercise: Create a db4 database, determine whether it exists, and set the character set to gbk
* create database if not exists db4 character set gbk;
Insert picture description here

2. R(Retrieve): query

  • Query the names of all databases:
    * show databases;
  • Query the character set of a database: query the creation statement of a database
    * show create database database name;
    example:

Insert picture description here
Quietly Mimi spit out, this capitalization is enough!

3. U(Update): modify

  • Modify the character set of the
    database * alter database database name character set character set name;

Insert picture description here

**4. D(Delete):删除**
  • Delete database
    * drop database database name;
  • Determine that the database exists, and then delete it
    if it exists* drop database if exists database name;
    Insert picture description here

5. Use the database

  • Query the name of the database currently in use
    * select database();

  • Use database
    * use database name;

    Insert picture description here

Operation table

1. C(Create): Create

  1. grammar:
create table 表名(
						列名1 数据类型1,
						列名2 数据类型2,
						....
						列名n 数据类型n
				);
  • Note: There is no need to add a comma (,) in the last column

  • Database type:
    1. int: integer type
    * age int,
    2. double: decimal type
    * score double(5,2)
    3. date: date, only contains year, month and day, yyyy-MM-dd
    4. datetime: date, Contains year, month, day, hour, minute, and second yyyy-MM-dd HH:mm:ss
    5. timestamp: Time error type includes year, month, day, hour, minute, and second yyyy-MM-ddHH:mm:ss
    * If you don’t assign a value to this field in the future, or assign a value null, the current system time is used by default to automatically assign the value
    6. varchar: string
    * name varchar(20): name maximum 20 characters
    * zhangsan 8 characters Zhang San 2 characters

    • Create table
	create table student(
					id int,
					name varchar(32),
					age int ,
					score double(4,1),
					birthday date,
					insert_time timestamp
				);

Insert picture description here

  • Copy table:
    * create table table name like the name of the table being copied;
    Insert picture description here

2. R(Retrieve): query

  • Query all the table names in a database
    * show tables;
    * Query table structure
    * desc table name;

Insert picture description here
Insert picture description here

3. U(Update): modify

  1. Modify the table name
    alter table table name rename to the new table name;
    Insert picture description here

  2. Modify the character set of the
    table alter table table name character set character set name;
    Insert picture description here
    Insert picture description here

  3. Add a column of
    alter table table name add column name data type;
    Insert picture description here

  4. Modify column name type
    alter table table name change column name new column name new data type;
    alter table table name modify column name new data type;
    Insert picture description here
    Insert picture description here

  5. Delete column
    alter table table name drop column name;

Insert picture description here

	**4. D(Delete):删除**

drop table 表名;
drop table if exists 表名 ;
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44664432/article/details/109260309