one-hand-octopus
one-hand-octopus

Reputation: 2743

How to insert into a table when UUID is used to generate a column ID?

I created a table "Centres" with CentreID defined as UUID:

CREATE TABLE `Centres` (
    `CentreID` VARCHAR(36) NOT NULL UNIQUE,
    `CentreName` VARCHAR(45),
    `Address1` VARCHAR(45),
    `Address2` VARCHAR(45), 
    `City` VARCHAR(45),
    CONSTRAINT `PK_Centres` PRIMARY KEY (`CentreID`)
);
INSERT INTO Centres VALUES(UUID(), 'my centre', 'address1', NULL, 'London');

It created CentreID as ccdd2852-b9de-11ec-a791-f4ee08b2b85a.

Now I'd like to create another table "OpeningTime", with CentreID as one of its columns.

CREATE TABLE `OpeningTime` (
    `OpeningTimeID` VARCHAR(36) NOT NULL UNIQUE,
    `Centres_CentreID` VARCHAR(36)
);

When inserting data, should I just do:

INSERT INTO OpeningTime VALUES(UUID(), 'ccdd2852-b9de-11ec-a791-f4ee08b2b85a');

Or is there a better way to insert the UUID without the long VARCHAR?

Upvotes: 0

Views: 3290

Answers (1)

P.Salmon
P.Salmon

Reputation: 17640

Or is there a better way to insert the UUID without the long VARCHAR, ANOTHER way would be insert..select..where centre name is known and unique.

INSERT INTO OpeningTime 
select UUID(), centres.centreid
from centres
where name = 'bromsgrove';

Upvotes: 1

Related Questions