Before manipulating the data in the table, now it is manipulating the table itself.
INDEX
- Create a multi-column primary key
- Provisions for automatic growth
- View the last inserted auto-increment id
- Try to replace NULL with the default value
- Foreign keys cannot cross engines
- Delete table and modify table name
Very neat. . Exemplary script:
CREATE TABLE customers ( cust_id int NOT NULL AUTO_INCREMENT, cust_name char(50) NOT NULL , cust_address char(50) NULL , cust_city char(50) NULL , cust_state char(5) NULL , cust_zip char(10) NULL , cust_country char(50) NULL , cust_contact char(50) NULL , cust_email char(255) NULL , PRIMARY KEY (cust_id) ) ENGINE=InnoDB;
To create a primary key made up of multiple columns
Simply specify the column names as a comma delimited list, as seen in this example:
CREATE TABLE orderitems ( order_num int NOT NULL , order_item int NOT NULL , prod_id char(10) NOT NULL , quantity int NOT NULL , item_price decimal(8,2) NOT NULL , PRIMARY KEY (order_num, order_item) ) ENGINE=InnoDB;
Provisions for automatic growth
CREATE TABLE `manga` ( `manga_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '漫画id', `manga_name` varchar ( 40 ) NOT NULL COMMENT ' manga name ' , `manga_discription` varchar ( 120 ) DEFAULT NULL COMMENT ' manga description ' , `manga_status` tinyint ( 4 ) NOT NULL DEFAULT ' 0 ' COMMENT ' Comment description ' , PRIMARY KEY (`manga_id`) ) ENGINE = InnoDB AUTO_INCREMENT = 1012 DEFAULT CHARSET = utf8 COMMENT = ' comic table '
Only one auto-incrementing column is allowed per table, and it must be indexed (for example, set it as the primary key)
View the auto-increment id of the last insertion,
Must be self-incrementing! Custom inserts do not count!
mysql> INSERT INTO manga -> (manga_name) VALUES ('what'); Query OK, 1 row affected (0.00 sec) mysql> SELECT last_insert_id(); +------------------+ | last_insert_id() | +------------------+ | 1012 | +------------------+ 1 row in set (0.00 sec)
Using DEFAULT Instead of NULL Values
Many database developers use DEFAULT values instead of NULL columns, especially in columns that will be used in calculations or data groupings.
Foreign Keys Can't Span Engines
There is one big downside to mixing engine types. Foreign keys (used to enforce referential integrity, as explained in Chapter 1, "Understanding SQL") cannot span engines. That is, a table using one engine cannot have a foreign key referring to a table that uses another engine.
Add and delete fields & define foreign keys
ALTER TABLE vendors ADD vend_phone CHAR(20);
ALTER TABLE Vendors DROP COLUMN vend_phone;
Modifying a table is often used to define foreign keys:
ALTER TABLE orderitems ADD CONSTRAINT fk_orderitems_orders FOREIGN KEY (order_num) REFERENCES orders (order_num); ALTER TABLE orderitems ADD CONSTRAINT fk_orderitems_products FOREIGN KEY (prod_id) REFERENCES products (prod_id); ALTER TABLE orders ADD CONSTRAINT fk_orders_customers FOREIGN KEY (cust_id) REFERENCES customers (cust_id); ALTER TABLE products ADD CONSTRAINT fk_products_vendors FOREIGN KEY (vend_id) REFERENCES vendors (vend_id);
Syntax: ALTER TABLE table_name ADD CONSTRAINT fk_id FOREIGN KEY (foreign key field name) REFERENCES table indicates (the corresponding primary key field name in the foreign table);
FK_ID is the name of the foreign key. For more information about foreign keys, please refer to Foreign Key Constraints
Modification of complex table structure
Complex table structure changes usually require a manual move process involving these steps:
- Create a new table with the new column layout.
- Use the INSERT SELECT statement (see Chapter 19, "Inserting Data," for details of this statement) to copy the data from the old table to the new table. Use conversion functions and calculated fields, if needed.
- Verify that the new table contains the desired data.
- Rename the old table (or delete it, if you are really brave).
- Rename the new table with the name previously used by the old table.
- Re-create any triggers, stored procedures, indexes, and foreign keys as needed.
Delete table and modify table name
DROP TABLE customers2;
RENAME TABLE backup_customers TO customers, backup_vendors TO vendors, backup_products TO products;