8. Add
To enter our data we must use the reserved words INSERT INTO followed by the name of the table, the word VALUES and the data in parentheses.
INSERT INTO [tabla] VALUES (valor1, valor2, …), (valor1, valor2, …), …;
If we need to include the flamenco genre we would have to indicate the 2 existing columns in the Genre table (GenreId, Name).
INSERT INTO Genre VALUES (NULL, 'Flamenco');
The word NULL is to indicate that I do not want to enter anything into GenreId. It is an auto-incrementing column (the number grows each time I add a row).
Every table has a special column called Primary Key. It is a number that keeps growing and is used to tell one row apart from another.
Multiple values
If I separate the parentheses with commas, I can insert all the values I need at the same time.
INSERT INTO Genre VALUES (NULL, 'Flamenco'), (NULL, 'Ska'), (NULL, 'Rock nacional');
Specifying columns
If my table has many columns, let's say 20, indicating what will go in each one can be torture. That is exactly why SQL lets us be flexible.
INSERT INTO [tabla] (columna1, columna2, …) VALUES (valor1, valor2, …), (valor1, valor2, …), …;
Let's take the case of the Customer table. It has 13 columns.
- CustomerId
- FirstName
- LastName
- Company
- Address
- ...
If I only want to add a customer with their FirstName, LastName and Email, I should declare it with the columns I need after the name of the table.
INSERT INTO Customer (FirstName, LastName, Email) VALUES ('Mickey', 'Mouse', 'mickey@disney.com');
Do I want to add many customers at once in the same statement? Separated by commas, just like before.
INSERT INTO Customer (FirstName, LastName, Email) VALUES ('Mickey', 'Mouse', 'mickey@disney.com'), ('Pato', 'Donald', 'donald@disney.com'), ('Peter', 'pan', 'peter@disney.com');
Activity 1
- Add 5 artists you like to the
Artisttable. - Enter the
MediaTypeWav. - Create 2 records related to the artists you have created.
This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.
Building SPAs with Django and HTML Over the Wire: Learn to build real-time single page applications with Python
The HTML over WebSockets approach simplifies single-page application (SPA) development and lets you bypass learning a JavaScript rendering framework such as React, Vue, or Angular, moving the logic to Python. This web application development book provides you with all the Django tools you need to simplify your developments with real-time results.
Buy the bookHelp me keep writing
Every coffee gives me a push toward the next article.
Sure, it's on me!
Comments
There are no comments yet.