Try this
ALTER TABLE users
ADD COLUMN `count` SMALLINT(6) NOT NULL AFTER `lastname`,
ADD COLUMN `log` VARCHAR(12) NOT NULL AFTER `count`,
ADD COLUMN `status` INT(10) UNSIGNED NOT NULL AFTER `log`;
check the syntax
ALTER TABLE table_name
ADD COLUMN column_name datatype
correct syntax
ALTER TABLE `WeatherCenter`
ADD COLUMN BarometricPressure SMALLINT NOT NULL,
ADD COLUMN CloudType VARCHAR(70) NOT NULL,
ADD COLUMN WhenLikelyToRain VARCHAR(30) NOT NULL;
check syntax
There is no automatic way to do this. Instead, construct the SQL queries and then run them. Something like this:
select concat('alter table ', t.table_name,
' add created_by varchar(255), add modified_by varchar(255)'
)
from information_schema.tables t;
Then copy the code into the appropriate tool and execute it (use use ).prepare
I would also recommend that you at and created_at, if these are not already present.modified_at
You cannot create nested table. And the thing on your mind is not a good idea to design table like that. You should have two tables (exactly three which holds the description if the category). One is for the and the second table holds the category for each product. Example design would look like this,product
CREATE TABLE Product
(
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50) UNIQUE
);
CREATE TABLE Category
(
CategoryID INT PRIMARY KEY,
CategoryName VARCHAR(50) UNIQUE
);
CREATE TABLE Product_Category
(
RecordD INT AUTO_INCREMENT PRIMARY KEY,
CategoryID INT,
ProductID INT,
CONSTRAINT tb_uq UNIQUE(CategoryID, ProductID)
);
and Populate Sample Records
INSERT Category VALUES (1, 'Fruit');
INSERT Category VALUES (2, 'Vegetable');
INSERT Product VALUES (1, 'Apple');
INSERT Product VALUES (2, 'Banana');
INSERT Product VALUES (3, 'Cabbage');
INSERT Product VALUES (4, 'Squash');
INSERT Product VALUES (5, 'Tomato');
INSERT Product_Category (CategoryID, ProductID) VALUES (1,1);
INSERT Product_Category (CategoryID, ProductID) VALUES (1,2);
INSERT Product_Category (CategoryID, ProductID) VALUES (2,3);
INSERT Product_Category (CategoryID, ProductID) VALUES (2,4);
INSERT Product_Category (CategoryID, ProductID) VALUES (1,5);
INSERT Product_Category (CategoryID, ProductID) VALUES (2,5);
sample queries
-- NORMAL QUERY
SELECT a.ProductName, c.CategoryName
FROM Product a
INNER JOIN Product_category b
ON a.ProductID = b.ProductID
INNER JOIN Category c
ON b.CategoryID = c.CategoryID
ORDER BY ProductName;
-- If you want catgoryName to be comma separated
SELECT a.ProductName, GROUP_CONCAT(c.CategoryName) CategoryList
FROM Product a
INNER JOIN Product_category b
ON a.ProductID = b.ProductID
INNER JOIN Category c
ON b.CategoryID = c.CategoryID
GROUP BY ProductName
ORDER BY ProductName;