Inserting Rows
INSERT
clause is used to insert new rows in a table.Syntax
SQL
Any number of rows from 1 to n can be inserted into a specified table using the above syntax.
Database Let's learn more about the INSERT clause by going hands-on on the
player
andmatch_details
tables that store the details of players and matches in a tournament respectively. - playertable stores the name, age and score of players.
- match_detailstable stores name of team, opponent team name, place, date and match result
Examples
- Insertname,ageandscoreof 2 players in theplayertable.
SQL
Upon executing the above code, both the entries would be added to the
player
table.Let's view the added data!
We can retrieve the inserted data by using the following command.
SQL
We shall know more about retrieving data in further cheat sheets.
- Similarly, let's insert the details of 2 matches in the match_details table.
SQL
Note
- Boolean values can be either given as (TRUE or FALSE) or (1 or 0). But in the database, the values are stored as 1 or 0.
- Date object is represented as: ‘YYYY-MM-DD’
- Datetime object is represented as: ‘YYYY-MM-DD HH:MM:SS’
Possible Mistakes
Mistake 1
The number of values that we're inserting must match with the number of column names that are specified in the query.
SQL
SQL
Mistake 2
We have to specify only the existing tables in the database.
SQL
SQL
Mistake 3
Do not add additional parenthesis
()
postVALUES
keyword in the code.SQL
SQL
Mistake 4
While inserting data, be careful with the datatypes of the input values. Input value datatype should be same as the column datatype.
SQL
Warning
If the datatype of the input value doesn't match with the datatype of column, SQLite doesn't raise an error.
Try it Yourself!
- Three new players have joined the tournament. Try inserting the players' data in theplayertable.
name | age | score |
---|---|---|
Ram | 28 | 70 |
Sita | 25 | 30 |
Ravi | 30 | 53 |