0

我想通过将一个表分成两个来重构我的数据库。我想将一些现有的列移动到一个新表中。例如,假设我想将address下表中的字段移动Employee到新Address表中。

如何使用 sqlite(或 SQL,如果等效)完成此操作?

前:

Employee Table
   employee_id (Primary)
   name
   address
   city

后:

Employee Table
   employee_id (Primary)
   name
   address_id

Address Table
   address_id (Primary)
   address
   city
4

3 回答 3

1
create table employee_new(employee_id,name,address_id);
insert into employee_new select employee_id,name,employee_id from employee;
create table address(address_id,address,city);
insert into address select employee_id,address,city from employee; 
alter table employee rename to employee_old;
alter table employee_new rename to employee;
于 2012-09-01T18:53:15.793 回答
1

试试这个:

  CREATE TABLE Employee(
  emp_id int,
  name nvarchar(50),
  address nvarchar(50),
  city nvarchar(50),
  PRIMARY KEY (emp_Id));

ALTER TABLE Employee
ADD address_id int NOT NULL IDENTITY;

INSERT INTO EMPLOYEE(emp_id,name,address,city) VALUES (111,'JOHN','XXX STREET','XYZ');

CREATE TABLE Address(
   address_id int NOT NULL,
   address nvarchar(100),
   city nvarchar(100),
   PRIMARY KEY (address_id)
  );

INSERT INTO Address (address_id, address, city)
SELECT address_id, address, city
FROM Employee;

ALTER TABLE Employee
DROP COLUMN address;

ALTER TABLE Employee
DROP COLUMN city;

在这里检查

于 2012-09-01T18:56:45.577 回答
0

1:在您的第一个表中创建 AddressID 列并分配一个数字。
2:新建空表。
3:传输数据(类似这样)
INSERT INTO ADDRESS (ADDRESS_ID, ADDRESS, CITY)
SELECT ADDRESS_ID, ADDRESS, CITY FROM EMPLOYEE
4:从第一个表中删除旧列并创建外键

于 2012-09-01T18:29:00.190 回答