Lab 2
Lab 2
Introduction
The Structured Query Language (SQL) is a database query language introduced by IBM in the
earlier 1970’s and standardized by ANSI later. The purpose of the language is to create a
platform to query and modify database structures. SQL is a version of Relational Calculus, a
high level, declarative query language.
DDL commands:
✴ CREATE
✴ ALTER
✴ DROP
✴ RENAME
✴ TRUNCATE
A. Create Table
The syntax for creating a table in SQL:
Let's break down the syntax:
In this example, the "employees" table has six columns: "id", "first_name", "last_name", "email",
"hire_date", and "salary". The column definitions include data types and constraints, such as
primary key, not null, unique, etc.
Remember to adjust the column names, data types, and constraints according to your specific
table requirements.
This query will display a list of all tables present in the current database.
This query will provide the structure and details of the "employees" table, including column
names, data types, and any constraints applied to the columns.
Please note that the syntax and commands may vary slightly depending on the specific database
management system (DBMS) you are using. The examples provided above are commonly used
in MySQL and MariaDB. For other DBMSs, the commands might differ.
✴ ISERT
✴ UPDATE
✴ DELETE
✴ SELECT
A. Insert
The INSERT Command helps to insert a new record onto the database. We can insert one or
more records using parameters to the existing table structures .Here are the INSERT statements
with syntax and examples for different scenarios in SQL:
Example:
This query inserts a new row into the "employees" table with the specified values for the
"first_name", "last_name", and "email" columns.
Example:
This query inserts a new row into the "employees" table with the specified values for all
columns. The values must be provided in the same order as the columns in the table.
Example:
This query allows you to use parameterized values instead of specific values. The actual values
can be provided when executing the query, allowing for repeated insertions.
Example:
This query allows you to use parameterized values for all columns in the table. The actual values
can be provided when executing the query.
Please note that the specific syntax may vary slightly depending on the database management
system (DBMS) you are using. The examples provided above are applicable to most SQL
databases, but you may need to adjust them based on your specific DBMS and table structure.
B. Select
The Select Command helps in retrieving one or more records by specifying or not specifying the
filtering condition.
The syntax for the SELECT statement with a WHERE condition, along with an example:
The SELECT statement is used to retrieve data from a database table.
Syntax:
Example:
Let's assume we have a table called "employees" with columns "id", "first_name", "last_name",
and "salary". We want to select the first name and last name of employees whose salary is
greater than 5000.
This query retrieves the "first_name" and "last_name" columns from the "employees" table
where the "salary" is greater than 5000. It will return the names of employees who meet the
specified condition.
You can modify the column names, table name, and condition to suit your specific requirements.
The WHERE clause allows you to add various conditions such as equality (`=`), inequality (`<>`
or `!=`), comparison operators (`>`, `<`, `>=`, `<=`), logical operators (`AND`, `OR`, `NOT`),
and more to filter the rows based on your desired criteria.
When you want to select all columns from a table, you can use the asterisk (`*`) as a shorthand
notation. Here's the syntax for the SELECT statement with the asterisk for all column selection:
Syntax:
Example:
Let's consider the "employees" table again. If you want to select all columns for employees
whose salary is greater than 5000, you can use the following query:
This query will retrieve all columns (`*`) from the "employees" table where the "salary" is
greater than 5000. It will return all the columns and values for employees who meet the specified
condition.
Using `SELECT *` can be convenient when you want to retrieve all columns without specifying
them individually.
You can modify the table name and condition to match your specific scenario.