2

我是新手,我正在使用mysql。我想从现有的表客户创建一个表产品,但我只想在新表中创建客户表的选定列,并且该列也具有相同的结构。例如,在客户表中有一个名为DATE的列,其默认值为 CURRENT_TIMESTAMPON UPDATE CURRENT TIMESTAMP。所以我希望这个结构也在新表中创建。

我看了其他答案,但他们没有复制结构。请帮我

4

3 回答 3

4

Here is what you can do

  1. use CREATE TABLE new_table LIKE old_table syntax to create a new table with the same structure
  2. use ALTER TABLE new_table DROP COLUMN unnecessary_column syntax to drop unnecessary columns in the new table
  3. use INSERT INTO new_table ... SELECT ... FROM old_table syntax to copy data

Let'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

于 2013-11-12T05:28:21.943 回答
2

用这个

CREATE TABLE NEW-TAB SELECT NAME,DATE FROM OLD-TAB;

请根据您的要求更改 COLUMN/TABLE 名称

于 2013-11-12T05:16:07.207 回答
0

尝试这个 :

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;  
于 2013-11-12T05:18:52.087 回答