0% found this document useful (0 votes)
21 views

Lab 2

SQL is a language used to create and manage databases. It includes four categories of statements: data definition language (DDL) for defining database schema; data manipulation language (DML) for working with data; data control language (DCL) for managing permissions; and transaction control language (TCL) for managing transactions. DDL statements like CREATE, ALTER, and DROP are used to define and modify database objects. DML statements like SELECT, INSERT, UPDATE, and DELETE allow users to query and modify the data within tables.

Uploaded by

ansarmuhajir96
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Lab 2

SQL is a language used to create and manage databases. It includes four categories of statements: data definition language (DDL) for defining database schema; data manipulation language (DML) for working with data; data control language (DCL) for managing permissions; and transaction control language (TCL) for managing transactions. DDL statements like CREATE, ALTER, and DROP are used to define and modify database objects. DML statements like SELECT, INSERT, UPDATE, and DELETE allow users to query and modify the data within tables.

Uploaded by

ansarmuhajir96
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL( Structured Query Language)

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.

There are four categories of Query Language, namely

1. Data Definition Language (DDL)

2. Data Manipulation Language (DML)

3. Data Control Language (DCL)

4. Transaction Control Language (TCL)

1. Data Definition Language (DDL)


The Data Definition Language (DDL) statements are used to create or modify database structure
or schema:

DDL commands:

✴ CREATE
✴ ALTER
✴ DROP
✴ RENAME
✴ TRUNCATE

A. Create Table
The syntax for creating a table in SQL:
Let's break down the syntax:

 `CREATE TABLE` is the SQL statement used to create a new table.


 `table_name` is the name you give to the table you're creating. Choose a descriptive
name that represents the entity or concept the table represents.
 Within the parentheses, you define the columns of the table.
 `column1`, `column2`, ..., `columnN` represent the names of the columns in the table.
 `datatype` specifies the data type for each column. Examples of data types include `INT`
(integer), `VARCHAR(n)` (variable-length string of length `n`), `DATE` (date),
`FLOAT` (floating-point number), etc.
 `constraints` define additional rules or restrictions for the columns. Common constraints
include `PRIMARY KEY`, `NOT NULL`, `UNIQUE`, `FOREIGN KEY`, etc.

Here's an example to create a table called "employees" with a few columns:

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.

To check the tables created:

1. Show all tables in a database:

This query will display a list of all tables present in the current database.

2. Describe the "employees" table:

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.

1. Data Manipulation Language (DML)


Data Manipulation Language comprises the 'SQL-data change' statements, which modify stored
data but not the schema or database objects. In other words, DML statements are used to
manipulate the data in the database. This is done either by retrieving information from existing
rows, entering new rows, changing existing rows or removing unwanted rows from tables in the
database.

Therefore the DML commands are

✴ 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:

1. INSERT INTO with selective columns and values:

Example:

This query inserts a new row into the "employees" table with the specified values for the
"first_name", "last_name", and "email" columns.

2. INSERT INTO with all columns and values:

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.

3. INSERT INTO with selective columns and parameters:


;

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.

4. INSERT INTO with all columns and parameters:

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:

 `SELECT` specifies the columns you want to retrieve.


 `FROM` indicates the table from which you want to retrieve data.
 `WHERE` is used to specify a condition that filters the rows based on certain criteria.

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.

You might also like