I am trying to use ALTER to add a new column (authorID) to the tests table, but I want to have a specific value (which I don't know).
I am trying to run the following, but getting SQL errors. Is this possible, if so where have I gone wrong please?
SELECT id FROM authors WHERE emailAddress = 'UNASSIGNED' into @unassignedID;
ALTER TABLE Tests ADD COLUMN authorID INT NOT NULL DEFAULT @unassignedID;
Authors table:
CREATE TABLE authors
(
ID INT NOT NULL AUTO_INCREMENT,
emailAddress VARCHAR(100) NOT NULL UNIQUE,
regionID INT NOT NULL,
PRIMARY KEY(ID)
);
Tests Tables:
CREATE TABLE Tests
(
testID INT NOT NULL AUTO_INCREMENT,
Title VARCHAR(100) DEFAULT NULL,
PRIMARY KEY(testID)
);
you can use prepared
statement
:
SELECT id into @unassignedID FROM authors WHERE emailAddress = 'UNASSIGNED' limit 1;
SET @sql = CONCAT('ALTER TABLE Tests ADD COLUMN authorID INT NOT NULL DEFAULT ',@unassignedID);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.