π Table Creation
Welcome to the Selecting Data module! This foundational learning path is designed to help you master the basics of querying data, particularly focusing on how to retrieve specific information from databases effectively.
π Creating SQL Tableβ
In SQL, when creating a table, each column must have a data type that defines what kind of data it can hold. Common data types include:
INTEGERβ whole numbersREALβ decimal (floating-point) numbersTEXTβ strings of characters
Other types like
BLOBandNULLexist but are not covered in this tutorial.
π οΈ Example: Creating a Tableβ
Lesson Overviewβ
Letβs create a simple table named Directory, which holds the floor number and company name.
This CREATE TABLE statement defines a new table called Directory with two columns:
floorβ stores the floor number as anINTEGER
companyβ stores the company name asTEXT
- SQL Code
- Output
CREATE TABLE Directory (
floor INTEGER,
company TEXT
);
Query OK, table created successfully.
β Tip: SQL keywords like CREATE TABLE, INTEGER, and TEXT are not case-sensitive, but using uppercase for SQL keywords improves readability.
π§Ύ Inserting Data into a Tableβ
Once a table structure is created using CREATE TABLE, the next step is to insert data into it. You can insert as many rows as you want, at any time.
Weβll continue with the Directory table from earlier. Letβs insert a couple of company records.
- SQL Code
- Output
CREATE TABLE Directory (
floor INTEGER,
company TEXT
);
INSERT INTO Directory (floor, company)
VALUES (1, 'Acme Inc.');
INSERT INTO Directory (floor, company)
VALUES (2, 'Homeflix');
| floor | company |
|---|---|
| 1 | Acme Inc. |
| 2 | Homeflix |
β Tip: Always match the order of columns in your INSERT INTO statement with the order of values inside VALUES().
π« Avoiding Duplicate Table Creation When running a CREATE TABLE command, trying to create a table that already exists will usually result in an error. To avoid this, we can use the IF NOT EXISTS clause.
CREATE TABLE IF NOT EXISTS Directory (
floor INTEGER,
company TEXT
);
β What You Have Learned
In this module, you learned the fundamentals of creating tables in SQL, including:
-
Defining Table Structure
How to use theCREATE TABLEstatement to define a new table and specify columns with appropriate data types. -
Common Data Types
The purpose of data types likeINTEGERfor numbers andTEXTfor strings. -
Inserting Data
How to add rows to a table using theINSERT INTOstatement. -
Preventing Duplicate Tables
UsingIF NOT EXISTSwithCREATE TABLEto avoid errors if the table already exists. -
Best Practices
Writing SQL keywords in uppercase for readability and matching column order inINSERT INTOstatements.
π Quiz: Test Your Knowledgeβ
1. How can you ensure a table is only created if it doesn't already exist, to avoid errors?β
Answer
- By adding
IF NOT EXISTSto theCREATE TABLEstatement:
CREATE TABLE IF NOT EXISTS Directory (
floor INTEGER,
company TEXT
);
2. Complete the column definition for the Tickets table creation syntax with the appropriate column data types.β
CREATE TABLE Tickets (
qty _____,
email _____
);
Answer
- Specify
qtyasINTEGERandemailasTEXT:
CREATE TABLE Tickets (
qty INTEGER,
email TEXT
);
3. Which of the following code can delete all the values from the given table?β
DELETE FROM toys WHERE price < 10DELETE FROM toys
Answer
DELETE FROM toyswill delete all rows from thetoystable.
DELETE FROM toys;
4. What happens if you call UPDATE with a WHERE clause that does not match any existing row?β
- SQLite will update the next closest row
- Nothing will change
- SQLite will automatically create a new row with the provided data
Answer
- Nothing will change β If no rows match the
WHEREclause, no rows are updated.
5. Write an SQL statement to update the streams count for the song "Paradise" by "The Jets" to 130.β
| songname | artist | streams | certifications |
|---|---|---|---|
| Paradise | The Jets | 120 | |
| My Way | Stevie J | 100 | |
| Purple Sunrise | Harry | 90 | |
| California Highway | Joan & Jerry | 85 | |
| My Sister Meg | Couches | 70 |
Answer
- Use the
UPDATEstatement with aWHEREclause to target the correct row:
UPDATE Songs
SET streams = 130
WHERE songname = 'Paradise';
6. Which of the following is not a requirement for defining a SQL tableβs columns?β
- Column name
- Column constraints
- Column data type
Answer
- Column constraints β This is not a requirement for defining a column in a SQL table.
7. Which of the following is not a valid SQL column data type?β
- REAL
- BOOLEAN
- TEXT
Answer
- BOOLEAN β Standard SQL does not define
BOOLEANas a column data type. Some databases support it as an alias, but it is not part of the SQL standard.