Lecture 3 - SQL Part I
Lecture 3 - SQL Part I
SQL Part I
Roi Yehoshua
Agenda
The SQL Query Language
DDL commands
DML commands
Select commands
Set operations
Aggregate functions
Grouping
Join operations
Source: KDnuggets
SQL Commands
Example:
CREATE TABLE employees (
employee_id INT,
first_name VARCHAR(20),
last_name VARCHAR(40),
email VARCHAR(50),
birth_date DATE,
salary DECIMAL(10, 2)
);
By default, the starting value of the field is 1, and increments by 1 for each new row
You can change the seed value by writing AUTO_INCREMENT=x at the end of CREATE TABLE
The foreign key columns must match the number and data types of the columns in the parent table
Foreign keys enforce referential integrity of the data
A value in the foreign key column must be one of the values contained in the parent table
We can now define a foreign key from the employees to the departments table:
CREATE TABLE employees (
employee_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
email VARCHAR(50) UNIQUE,
birth_date DATE,
salary DECIMAL(10, 2) CHECK (salary >= 0),
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);
It’s faster than DELETE if you want to delete all the records from the table
Provide values for all the columns in the table, without specifying the column names
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
The order of the values must be in the same order as the columns in the table
You can use NULL to skip a value for a specific column
You can use DEFAULT for an auto-increment field or to use the column’s default value (if defined)
or equivalently
INSERT INTO employees (first_name, last_name, email, birth_date, salary)
VALUES ('King', 'Robert', 'king.b@gmail.com', '1980-10-25', 80000);
You can view the data in the table by right clicking on it and select Rows – Limit 1000
In this syntax, instead of using a single list of values, you use multiple comma-
separated lists of values for insertion
Be sure to set this back with SET SQL_SAFE_UPDATES = 1 when you’re done
To select all the columns in the table, use the following syntax:
SELECT *
FROM table_name;
In general, you should avoid using SELECT *
Produces unnecessary I/O and network traffic between the database server and the application
May expose sensitive information to unauthorized users
The data returned is stored in a result table called the result set
Example: Find the names of all instructors in the Comp. Sci. department
Conventions:
The alias name should be the first letter of each word in the table’s name
If there is already an alias with the same name then append a number (e.g., t1, t2)
Always include the AS keyword – makes it easier to read the query
Beware of unrelated attributes with same name that may get equated incorrectly
97 Roi Yehoshua, 2024
Self-Join
A self-join is a join of a table with itself
It allows you to compare rows within the same table
You must use the rename operator in a self-join
SELECT column_name(s)
FROM table1 AS t1
JOIN table1 AS t2
ON t1.column_name = t2.column_name;