i have a table which has two columns i'd fill one of the columns by selecting other table column data but how can i fill the next column cause i can't use VALUE. Here's the code
INSERT INTO Numbers(number, val) SELECT LaptopID FROM Laptop WHERE Laptop.Pid = 2
as you can see the "val" column left empty how can i fill that?
How do you use data from certain columns of an existing table to populate a new table with a matching structure? Place a SELECT command within an INSERT command. When would you usually specify primary key constraints? You add a primary key in the CREATE TABLE command.
If you want to add data to your SQL table, then you can use the INSERT statement. Here is the basic syntax for adding rows to your SQL table: INSERT INTO table_name (column1, column2, column3,etc) VALUES (value1, value2, value3, etc); The second line of code is where you will add the values for the rows.
Overview of SQL ADD COLUMN clauseFirst, specify the table to which you want to add the new column. Second, specify the column definition after the ADD COLUMN clause.
Use NULL
if the column allows it:
INSERT INTO Numbers(number, val)
SELECT LaptopID, NULL
FROM Laptop WHERE Laptop.Pid = 2
Or use the intended (hardcoded) value that you want.
If number:
INSERT INTO Numbers(number, val)
SELECT LaptopID, 2
FROM Laptop WHERE Laptop.Pid = 2
or if text:
INSERT INTO Numbers(number, val)
SELECT LaptopID, 'val'
FROM Laptop WHERE Laptop.Pid = 2
If you don't have a corresponding value that needs to go into number; then you can just put zero or NULL:
Somethign like this---
INSERT INTO numbers (number, val)
SELECT NULL, laptopid
FROM laptop
WHERE laptop.pid = 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With