Interactive SQL Tutorial: Learn SQL Commands - Nguyen Cong Trinh

in #sql7 years ago

Lecture 4: Insert
In the previous section, you had a look at the CREATE TABLE command. In this section, you will examine the clause INSERT.

The command INSERT is used to insert new rows or records into the table.

Now take a look at the INSERT INTO statement below:

INSERT INTO employees (id, name, age)
VALUES (1, 'Andrew', 33);
Let's examine it:

Command INSERT INTO will add a new row to the table.
The table name is employees.
Parameters (id, name, age) will examine the column names and will add data to the columns.
VALUES is also a command. This clause will identify the information inserted into the row.
Parameters (1, 'Andrew', 33) will check the stored data. Data stored in the parameter will have their separate columns. For example:
1st column - id. Integer 1 will be added.
2nd column - name. Text Andrew will be added.
3rd column - age. Integer 33 will be added.
Tasks

  1. Start this task by adding more employees to the database. Use this code:

INSERT INTO employees (id, name, age)
VALUES (1, 'Andrew', 33);
INSERT INTO employees (id, name, age)
VALUES (2, 'Adam', 27);
INSERT INTO employees (id, name, age)
VALUES (3, 'Julia', 22);
INSERT INTO employees (id, name, age)
VALUES (4, 'Jeff', 65);
Let's finish the task and add the SELECT statement. Write the command under your previous code. Type:

SELECT name FROM employees;

Lecture 5: Select
The command SELECT is used in statements to select certain data from the database. In the example above the command will access the employees table and return all of the data from the name column.

SELECT name FROM employees;
Let's break down this query:

SELECT is a query statement. Use this command whenever you want to retrieve data from the database.
name is used to define the column to inquire data from.
FROM is used to define the table that will be accessed to return data.
Here, the program will query data from the table called employees.
Let's take a look at another example:

SELECT * FROM employees;
In this statement, a wildcard character is used to retrieve data from all the columns without naming each one of them separately. The data will be stored in a new table called result-set.

The asterisk (*) is a wildcard character. It represents one or more characters.
SELECT statements always return a new table called result-set.
In this example, the result-set table will contain data from all of the columns.

Tasks

  1. Edit the rows in the table by updating them. While in emulator type:

UPDATE employees
SET age = 30
WHERE id = 1;
Let's also select the table to see the changes:

SELECT * FROM employees;
Do not worry, you will be introduced to the UPDATE statements with the SET and WHERE clauses soon.
nguyen trinh.jpg
Lecture 6: Alter
Take a look at the ALTER TABLE statement.

ALTER TABLE is used to change the structure of the table. You will mostly use it to create, delete or edit columns.

ALTER TABLE employees
ADD COLUMN job_title TEXT;
Let's break down this statement:

ALTER TABLE is a clause. With this command, you can make changes in the table.
employees is the table that the program will change.
ADD COLUMN is a command that is used to add new columns. Here, SQL will add a new column called job_title with a data type of TEXT.
At the moment, the rows in the column named job_title are empty and have a value of NULL.

NULL is a special marker in SQL that signifies no value. It is used to represent missing or non-existing data.

Tasks

  1. Add a new column job_title to the table. Use the emulator and type:

ALTER TABLE employees ADD COLUMN job_title TEXT;
Let's also select the table to see the changes:

SELECT * FROM employees;

Coin Marketplace

STEEM 0.05
TRX 0.28
JST 0.043
BTC 68008.38
ETH 1966.45
USDT 1.00
SBD 0.38