jchornsey
jchornsey

Reputation: 605

Adding Columns to Multiple Tables in SQL

I just created a database and then added a couple of hundred tables with a script like this:

CREATE TABLE CapBond
(
    [timestamp] varchar(50),
    [Reward] varchar(50),
    [Award] varchar(50),
    [Fact] varchar(50)
)

CREATE TABLE Values
(
    [timestamp] varchar(50),
    [Name] varchar(50),
    [Test] varchar(50),
    [Read] varchar(50),
    [Parameters] varchar(50)
)

I realize I forgot to add two columns to each table. One for the PK and one for an FK that points back to a 'master' table.

Is there an easy way to insert columns without having to drop the DB and recreate it? Preferably with the columns inserted as the first two columns in the table?

Upvotes: 0

Views: 1217

Answers (1)

Sándor Jankovics
Sándor Jankovics

Reputation: 898

Yes. In mysql you have the alter table command for this purpose. Check out this page for more detailed explanation https://www.sqlservertutorial.net/sql-server-basics/sql-server-alter-table-add-column/ .

And here is the solution for the ordering of the columns https://www.mysqltutorial.org/mysql-add-column/

Upvotes: 1

Related Questions