Learn online DBMS “Insert Command in MySQL” Complete Lesson 11
INSERT COMMAND – This command is used to insert a tuple in a relation. We must specify the name of the relation in which tuple is to be inserted and the values. The values must be in the same order as specified during the Create Table command. For example, consider the following table Teacher:
CREATE TABLE Teacher
(
Teacher_Namevarchar(20) NOT NULL,
Teacher_IDinteger,
Dept_Nointeger DEFAULT 234,
Subject varchar(20)
);
To insert a tuple in the Teacher table INSERT command can be used as shown below:
Insert Into Teacher
Values (“Shanaya”, 345, 5, “Computer Science”);
Note that the values listed above are in the same order of the attributes as specified in the Create Table command.
Another form of INSERT command is used to insert a tuple in which the ordering of values is done by explicitly specifying the attribute names as shown below:
Insert Into Teacher (Teacher_ID, Teacher_Name, Subject)
Values (2345, “Alice”, “History”);
We have used the Select Command to display all the rows of the Teacher Table. This command is explained later in detail.
Note that the values entered are ordered with respect to the attributes mentioned. The attributes that have not been specified are those with NULL allowed or DEFAULT values. Thus in the above example Dept_No is not specified hence it will take its default value i.e 234 as given in the table creation command.