Tables Introduction

sql-tutorial

Many DBMS have a graphic user interface that makes it easier to create tables and insert data into them.

However, sometimes, most often in programs, it is impossible to use graphic user interface and code must be written.

The following table shows some types of information that can be stored in a database and data type used to store it. There are many more types than just these stated here.

Data we want to store Type of data that can be used
Number int, bigint, float, decimal
Text char, varchar, nvarchar
Date date, datetime, datetimeoffset
Picture Image
Currency Money

Every column in a table must have two parts: (1) column name (2) data type stored in a column. Every table can have multiple columns. When we define data type we often use constraints. This is represented as a number in brackets. It means how many digits/letters data can have. For example, char (10) means that we want to store text that has a maximum of 10 letters.

CREATING TABLES

The first step in developing a database is creating tables. This can be done using graphic user interface. However, for this tutorial we will use code.

The statement used for creating tables is CREATE TABLE. The statement is used as follows:

CREATE TABLE “tableName”
(“columnName” “datatype”(constraint))

Multiple columns can be added at once.

Let's create a table we will use in this tutorial. We will use a table that contains information about students in a school. We want to have: name, surname, age, city, main subject. The constraint is the limit of character a word or number can have.

We will start by adding one column.

CREATE TABLE Student
(Name char)

We will get table like this:

table SQL

EXERCISE:

  1. Add all other columns into table.
  2. Set name column to have a maximum of 10 characters, and surname to have a maximum of 20 characters.
  3. Set main subject to have less than 15 characters and city to have a maximum of 20 characters.
CREATE TABLE Student
(Name char(10)
Surname char(20)
Age int
City char(20)
Main_subject char(14))

INSERTING DATA INTO TABLE

After creating table next step is to insert data into it. Inserting data makes it easier for us later to find what we need. The statement used for inserting data is INSERT INTO. The statement is used as follows:

INSERT INTO “tableName” (“columnName“)
VALUES (“value”)
Multiple information can be added into multiple columns.

Let's add name “Ann” into table

INSERT INTO Student (Name)
VALUES ('Ann')

We will get table like this:

table SQL

NULL in a table means that there is no data in that field.

Let's add the following student into table: John Smith from New York, age 23

INSERT INTO Student (Name, Surname, City, Age)
VALUES ('John', 'Smith', 'New York', 23)

We will get table like this:

table SQL

EXERCISE:

Insert the following information into table:

  1. John McKey from Chicago, age 19 has main subject Physics
  2. INSERT INTO Student (Name, Surname, City, Age,Main_subject)
    VALUES ('John', 'McKey', 'Chicago', 23,'Physics')
  3. Ann Mary Sue from Los Angeles, age 21 has main subject Math
  4. INSERT INTO Student (Name, Surname, City, Age,Main_subject)
    VALUES ('Ann Mary', 'Sue', 'Los Angeles', 21,'Math')
  5. 24 year old Susie Black from New Jersey has main subject Philosophy
  6. INSERT INTO Student (Name, Surname, City, Age,Main_subject)
    VALUES ('Susie', 'Black', 'New Jersey', 24,'Philosophy')
  7. William Creek, 18, Washington, Music
  8. INSERT INTO Student (Name, Surname, City, Age,Main_subject)
    VALUES ('William', 'Creek', 'Washington', 18,'Music')
  9. Angelina Knight, 21, Detroit, Physics
  10. INSERT INTO Student (Name, Surname, City, Age,Main_subject)
    VALUES ('Angelina', 'Knight', 'Detroit', 21,'Physics')

READING DATA FROM TABLE

When we are finished with inserting data into a table we often want to read some data from it. The statement used for reading data from table is SELECT FROM. The statement is used as follows:

SELECT “columnName”
FROM “tableName“
WHERE (condition)

WHERE part of statement is used to select only data that meet some condition. It is not needed to use WHERE part if we want to choose entire column.

Conditional selections used in WHERE clause when we work with numbers are:

  1. < less than
  2. > greater than
  3. = equal
  4. <= less than or equal
  5. >= greater than or equal
  6. <> not equal

When we work with text we use LIKE clause. If we want to get all data from a table, we can use character *.

SELECT *
FROM Student
table SQL

Let's select only age of all students

SELECT Age
FROM Student
table SQL

Let's select only names of students who are 19 years old.

SELECT Name
FROM Student
Where Age = 19
table SQL

Let's find all students from Chicago.

SELECT *
FROM Student
WHERE City LIKE 'Chicago'
table SQL

If we want to get data that begins with specific letter we use character %. If we want all students from city beginning with letter B in LIKE part of a statement we would insert 'B%'.

EXERCISE:

  1. Select name and surname of all students.
  2. SELECT Name, Surname
    FROM Student
  3. Select all students from Chicago.
  4. SELECT *
    FROM Student
    WHERE City LIKE 'Chicago'
  5. Select all students younger than 20 years.
  6. SELECT *
    FROM Student
    WHERE Age < 20
  7. Select city of all students with name John.
  8. SELECT City
    FROM Student
    WHERE Name LIKE 'John'
  9. Select surname of students whose name begins with letter A.
  10. SELECT Surname
    FROM Student
    WHERE Name LIKE 'A%'
  11. Select all students whose main subject begins with PH.
  12. SELECT *
    FROM Student
    WHERE Main_subject LIKE 'Ph%'
  13. Select age of all students where city name begins with “New“
  14. SELECT Age
    FROM Student
    WHERE City LIKE 'New%'
  15. Select everything but age from students where main subject is Physics.
  16. SELECT Name, Surname, City
    FROM Student
    WHERE Main_subject LIKE 'Physics'

UPDATING TABLE

Sometimes after inserting data into a table we realize that we made a mistake. We do not want to delete everything what we entered, but we just want to change one field. We do this by using an UPDATE statement. The statement looks like:

UPDATE “tableName“
SET “columnName” = “newValues”
WHERE “columnName”
OPERATOR “value”

Operators used in WHERE is part of an UPDATE statement are the same that we used in WHERE part of the SELECT statement.

Let's add Art as main subject for John Smith.

UPDATE Student
SET Main_subject = 'Art'
WHERE Surname = 'Smith'
table SQL

We can update same column for several rows or several columns at once. Let's increase age of everyone for 1 year.

UPDATE Student
SET Age = Age + 1
table SQL

EXERCISE:

  1. Susie married William and changed her surname into the Creek. Write this in table.
  2. UPDATE TABLE Student
    
    SET Surname = 'Creek'
    WHERE Name LIKE 'Susie'
  3. Everyone that has surname Creek lives in Washington. Write this in table.
  4. UPDATE TABLE Student
    
    SET City = 'Washington'
    WHERE Surname LIKE 'Creek'
  5. Everyone who is older than 23 years changed their main subject to Chemistry. Write this in table.
  6. UPDATE TABLE Student
    
    SET Main_subject = 'Chemistry'
    WHERE Age > 23

DELETING FROM TABLES

Sometimes we do not need some information stored in our database, and we want to delete it. In our case it would be when a student graduates. We can delete data using the DELETE statement. It looks like:

DELETE FROM “tableName“
WHERE “columnName“
OPERATOR “value“

Operators used in WHERE part of the DELETE statement are the same that we used in WHERE part of SELECT statement.

Looking at our table, we see that for Ann we only have name and none of the other data. Let's delete that row.

DELETE FROM Student
WHERE Name LIKE 'Ann'
table SQL

Let's remove all data about persons that have name John or surname Creek.

DELETE FROM Student
WHERE Name LIKE 'John'
OR
Surname LIKE 'Creek'
table SQL

IMPORTANT:

If we do not put WHERE part of statement all data in the table will be deleted. Here is result of statement DELETE FROM Student

table SQL

DROPPING TABLE

When we want to remove not just data, but entire table from the database we use the DROP TABLE statement. Here is how the statement looks like:

DROP TABLE “tableName“

In our case it would be

DROP TABLE Student

The difference between DELETE and DROP statements seems small but it's big. DELETE statement removes all data from a table but leaves the table in the database along with all column and constraint definition. DROP statement removes table entirely from the database along with all columns, constraints and any link connecting it.