Page 253 - PYTHON-12
P. 253
7. Inserting Data into a Table
The INSERT INTO command is used to insert a new record/row/tuple in a table.
It is possible to write the INSERT INTO statement in the following different forms:
(a) Inserting the data (for all the columns) into a table: In the first method, it does not
specify the column names where the data will be inserted, only their values.
Syntax for SQL INSERT is:
Syntax:
INSERT INTO table_name
VALUES (value1, value2, value3...);
For example, INSERT INTO student
VALUES (1,“Raj Kumar”, ‘M’, 93, ‘2000-11-17’);
While inserting a row, if we are adding value for all the columns of the table, we need not
specify the column(s) name in the SQL query. But we need to make sure that the order of
the values is in the same order as the columns represented in the structure of the table.
The following points should be kept in mind while inserting data in a relation:
• When values are inputted using INSERT INTO command, it is termed as single row
insert since it adds one tuple at a time into the table.
• The INTO clause specifies the target table and the VALUES clause specifies the data
to be added to the new record of the table.
• The argument/values of character datatype are always enclosed in double or single
quotation marks.
• Column values for the datatype of a column are provided within curly braces { } or
single quotes.
• NULL values are stored and displayed as NULL only without any quotes.
• If the data is not available for all the columns, then the column-list must be included
following the table name.
CTM: In SQL, we can repeat or re-execute the last command typed at SQL prompt by typing “/” key and
pressing enter.
(b) Inserting the data directly into a table: The second form specifies both the column
names and the values to be inserted.
Syntax: INSERT INTO table_name (column1,column2,columnN,...)
VALUES (value1,value2,valueN,...);
Relational Database and SQL
Here, column1, column2, ...columnN—the names of the columns in the table for which
you want to insert data.
For example, INSERT INTO student(RollNo, Name, Gender, Marks, DOB)
VALUES(2,‘Deep Singh’, ‘M’, 98, ‘1996-08-22’);
CTM: When adding a row, only the characters or date values should be enclosed within single quotes.
12.23