xxxxxxxxxx
-- if you want to insert values in all column
-- values must be assigned to that column
INSERT INTO table_name
VALUES
(value1, value2, value3, );
-- ^ ^ ^ ^
-- (column1, column2, column3, )
-- the same arrangement
xxxxxxxxxx
--sql insert quest example
INSERT INTO table_name (column1, column2, column3, )
VALUES (value1, value2, value3, );
xxxxxxxxxx
It is possible to write the INSERT INTO statement in two ways:
1. Specify both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, )
VALUES (value1, value2, value3, );
2. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the INSERT INTO syntax would be as follows:
INSERT INTO table_name
VALUES (value1, value2, value3, );
xxxxxxxxxx
--SQL Insert Example with real data example
INSERT INTO Customers (CustomerName, ContactName)
VALUES ('Cardinal', 'Tom B. Erichsen');
xxxxxxxxxx
INSERT INTO Campus (Campus_Name, Address, Zip, Phone_No)
VALUES
('Nairobi Campus', '123 Moi Avenue, Nairobi', '00100', 0712345678),
('Mombasa Campus', '456 Kilindini Road, Mombasa', '80100', 0723456789),
('Eldoret Campus', '789 Main Street, Eldoret', '30100', 0734567890),
('Kisumu Campus', '987 Lake Road, Kisumu', '40100', 0745678901),
('Nakuru Campus', '654 Kenyatta Avenue, Nakuru', '20100', 0756789012);
xxxxxxxxxx
-- the following statement inserts values value1, value2 and value3
-- into columns column1, column2, column3 respectively
INSERT INTO table_name (column1, column2, column3)
VALUES(value1, value2, value3);
-- the following statement inserts the values in order into
-- all the columns of the table
INSERT INTO table_name
VALUES(value1, value2, value3);
xxxxxxxxxx
INSERT INTO Customers(customer_id, first_name, last_name, age, country)
VALUES
(5, 'Harry', 'Potter', 31, 'USA');
xxxxxxxxxx
INSERT INTO tableName
VALUES (‘anydata’, ‘anydata’, ‘anydata’, ‘anydata’, NULL,
NULL);
xxxxxxxxxx
Add new rows to a table.
Example: Adds a new vehicle.
INSERT INTO cars (make, model, mileage, year)
VALUES ('Audi', 'A3', 30000, 2016);
xxxxxxxxxx
INSERT INTO table_name (column1, column2, column3, )
VALUES (value1, value2, value3, );
/* or if you want to add all the fields*/
INSERT INTO table_name
VALUES (value1, value2, value3, );