[SQL] Creation of database, creation, update and deletion of tables

The content of this article refers to the book "SQL Basic Tutorial". For beginners, please give me some advice.

1. Creation of database

1. Create database statements

CREATE DATABASE <Database name>;

2. Example

CREATE DATABAST shop;

 2. Creation of table

Before creating a table, you must first create the database in which the table will be stored.

1. Statement to create table

Statement to create table:CREATE TABLE  <Table name>

2. Example

The first column is shop, the second column: is the column name of the created table, the third column: data type, for example, CHAR(4) means that the longest string is 4, the fourth column: NOT NULL means the information of this column It cannot be empty, which is a constraint.

3. Specification of data type

(1) CHAR type

Character type, fixed-length string. You can specify the length (maximum length) of the string that the column can store in parentheses like CHAR(10) or CHAR(200).

(2) INTEGER type

Number type, stores integers, cannot store decimals.

(3) VARCHAR type

String type, variable length string. You can also specify the length (maximum length) of the string through the number in brackets.

(4) DATE type

Date type, stores dates.

3. Deletion of table

1. Statement to delete table

DROP TABLE <表名>;

2. Example

If you want to delete the Product table, enter the following statement, but once the table is deleted, it cannot be restored.

DROP TABLE Product;

4. Table update

1. Statement to add columns

ALTER TABLE <表名> ADD COLUMN <列的定义>;

2. Example

ALTER TABLE Product ADD COLUMN product_name_pinyin VARCHAR(100);

Among them, Produce is the table name, product_name_pinyin is the newly added column, and product_name_pinyin is the data type of the newly added column.

 3. Statement to delete columns

ALTER TABLE <表名> DROP COLUMN <列的定义>;

4. Example

ALTER TABLE Product DROP COLUMN product_name_pinyin;

 5. SQL statements to insert data into the Product table

The BEGIN TRANSACTION statement is the instruction statement to start inserting rows, and the COMMIT statement at the end is the instruction statement to determine the inserted rows.

 6. Change table name

ALTER TABLE <Original table name> RENAME TO <New table name>;

7. After-class exercises

1. Create table

2. Update table

Enter \d to view the created tables. Two tables have been created.

 3. Delete table

Guess you like

Origin blog.csdn.net/xing09268/article/details/130084956