The Sql Insert Into Statement
- The INSERT INTO statement is used to insert new data(row) in a table.
- We can insert one or more record one time.
- We can insert record from another table with select statement
- If There is autoincrement column we don’t write data for that column
Syntax
The general use:
1 |
Insert into tableName(columnNames) values(values) |
- If there are two or more colums then column names are separated by commas.
- If you are writing data to all column, you do not need to write column names like below
1 |
Insert into TableName Values(values) |
This situation we must write values table’s column order
TO DOWNLOAD THE SAMPLE LİBRARY DATABASE CLICK
SQL INSERT INTO Examples on Library Database
Sample 1: Add record to autors table that autor name = John surname=Hawk autor_id auto increment
1. Method
1 |
Insert into autors values('John','Hawk') |
2. Method (with column Names)
1 |
Insert into Autors(name,surname) values('John','Hawk') |
3. Method (with column Names rewerse)
1 |
Insert into Autors(surname,name) values('Hawk','John') |
If autor_id don’t become auto increment, we must write data because it’s primary key.
Sample 2: Add record to autors that autor name Jenny;
Not: Surname column must be null able.
1. Method
1 |
Insert into Autors(name) values('Jenny') |
2. Method
1 |
Insert into Autors values('Jenny',Null) |
Sample 3: Insert three records to autors table one query
1.Method
1 |
Insert into Autors(name,surname) values('Jane','Austen'),('Geoffrey','Chaucer'),('Charles','Dickens') |
2.Method(unless column names)
1 |
Insert into Autors values('Jane','Austen'),('Geoffrey','Chaucer'),('Charles','Dickens') |
Sample 4: Insert into autors table, Random selected five students
1 2 |
Insert into autors(name,surname) Select top 5 name,surname from students order by newid() |
Notes about this sample:
- “Value” word should not be written to the query.
- Insert into statement’s column count and select statement’s column count must be the same
- The column’s Data types must be same at same order