Chapter XXI: create and manipulate tables

@author: Tobin
@date: 2019/11/6 18:25:04

Create a table

CREATE TABLE IF NOT EXISTS 表名
(
    cust_id int NOT NULL AUTO_INCREMENT,
    cust_address char(50) NOT NULL,
    PRIMARY KEY(cust_id)
)ENGINE=InnoDB;

Each column is either NULL, or NOT NULL, empty string is not equal to NULL.
Use only the primary key column does not allow NULL values. NULL values can not be allowed as a unique identifier.
Each table only allows a AUTO_INCREMENT column. SELECT last_insert_id () to get the last AUTO_INCREMENT value.
Use DEFAULT xx, later on NOT NULL, prompt defaults.
engine.

  • InnoDB: reliable transaction processing engine that does not support full-text search
  • MEMORY: data stored in the memory, not disks, fast, suitable for temporary tables
  • MyISAM: high performance, support for full-text search, does not support transactions

Foreign key can not be cross-engine, i.e. engine using a reference table can not have a foreign key of the table using different engines.

Update table

# 添加一个列,明确其数据类型
ALTER TABLE vendors
ADD vend_phone CHAR(20);

# 删除列
ALTER TABLE vendors
DROP COLUMN vend_phone;

Complex table structures change process.

  • Create a new table with a new table layout
  • Use INSERT SELECT statement, to copy data from the old table a new table, transfer function, and calculated fields can be used
  • The new table contains the test data required
  • Rename the old table, you can delete
  • With the original name rename the new table the old table
  • If necessary, re-create the triggers, stored procedures, indexes and foreign keys

Delete table

DROP TABLE customer2; # 永久删除,不可撤销

Rename Table

RENAME TABLE customer2 TO customers,
                            backup_products TO products;

Guess you like

Origin www.cnblogs.com/zuotongbin/p/11814175.html