我是新手,我正在使用mysql。我想从现有的表客户创建一个表产品,但我只想在新表中创建客户表的选定列,并且该列也具有相同的结构。例如,在客户表中有一个名为DATE的列,其默认值为 CURRENT_TIMESTAMP和ON UPDATE CURRENT TIMESTAMP。所以我希望这个结构也在新表中创建。
我看了其他答案,但他们没有复制结构。请帮我
Here is what you can do
CREATE TABLE new_table LIKE old_table
syntax to create a new table with the same structureALTER TABLE new_table DROP COLUMN unnecessary_column
syntax to drop unnecessary columns in the new tableINSERT INTO new_table ... SELECT ... FROM old_table
syntax to copy dataLet's say your customer
table looks like
CREATE TABLE customer
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(5),
created DATETIME,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
other_column INT
);
And you want to create a new table customer_new
but without other_column
CREATE TABLE customer_new LIKE customer;
ALTER TABLE customer_new DROP COLUMN other_column;
INSERT INTO customer_new (id, name, created, modified)
SELECT id, name, created, modified
FROM customer;
Here is SQLFiddle demo
用这个
CREATE TABLE NEW-TAB SELECT NAME,DATE FROM OLD-TAB;
请根据您的要求更改 COLUMN/TABLE 名称
尝试这个 :
create table product(DATE CURRENT_TIMESTAMP,ON UPDATE CURRENT TIMESTAMP );
INSERT INTO product (DATE,ON) SELECT DATE,ON FROM customer ;
或者
CREATE TABLE product (DATE CURRENT_TIMESTAMP,ON UPDATE CURRENT TIMESTAMP )
SELECT DATE,ON FROM customer;