Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views14 pages

SQL PPT

Uploaded by

Dore A.M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views14 pages

SQL PPT

Uploaded by

Dore A.M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

SQL

 WORK WITH DATABASE


database is a collection of information that is stored digitally in a
computer, on a server, or in the cloud. Databases can be very simple or
quite complicated, depending on the structure and organization of the
data.
 ACCESS
An access definition specifies the tables, relations traversal, and
selection criteria for the data you want to extract. It also provides
parameters for Action-user-defined SQL statement executed at
predefined points in an Extra or Insert Process.
 MANIPULATING
In SQL, manipulating refers to the process of modifying or
changing data stored in the database. This includes actions such as
inserting new data into tables, updating existing data, or deleting data
from tables.

Common SQL commands used for manipulating data include:


- INSERT: Adds new records into a table.
- UPDATE: Modifies existing records in a table.
- DELETE: Removes records from a table.
 DBMS TOOLS
DBMS tools, short for Database Management System tools, are
software applications used to interact with databases. Some popular
DBMS tools include MySQL Workbench, Microsoft SQL Server
Management Studio, Oracle SQL Developer, and PostgreSQL pgAdmin.
 QUERY
In general terms, a query in SQL is a request to databases to
fetch (or retrieve) the information. We use a common language - SQL, to
query our databases. It is used whenever companies have a ton of data
that they want to manipulate.
SQL COMMANDS
 Data Definition Language [DDL]
 Data Manipulation Language [DML]
 Data Control Language [DCL]
 Transaction Control Language [TCL]

DDL:
- Create:
Create new database objects like tables, views, or produces.
- Alter:
Modifies the structure of a database object.
- Drop:
Deletes a database object like a tables and view.
- Truncate:
It is used to delete all records from a table.
- Rename:
It is used to change the existing database object.

DML
- Select:
Retrieves data from a database.
- Insert:
Adds new record into a database table.
- Update:
Modifies existing records in a database table
- Delete:
Removes records from database table.
DCL
- Revoke:
Revoke previously granted permission from users.
- Grant:
Allows users to access specific database objects or perform certain.
TCL
- Commit:
Saves all the changes made during the transaction.
- Rollback:
Undoes the change made during the transaction.

DATA TYPES

1. *Numeric Data Types*:


- INT (integer)
- DECIMAL/NUMERIC (fixed-point numbers)
- FLOAT/REAL (floating-point numbers)

2. *Character String Data Types*:


- CHAR(n) (fixed-length character string)
- VARCHAR(n) (variable-length character string)
- TEXT (variable-length character string with large storage capacity)

3. *Date and Time Data Types*:


- DATE (date)
- TIME (time)
- TIMESTAMP (date and time)

4. *Boolean Data Type*:


- BOOL/BOOLEAN (boolean value - true or false)

5. *Binary Data Types*:


- BLOB (binary large object)
- BINARY(n) (fixed-length binary string)
- VARBINARY(n) (variable-length binary string)

CREATE
Creating a database in SQL involves defining a new database
schema that will store tables, views, indexes, and other database
objects. To create a database, you typically use the CREATE DATABASE
statement in SQL. This statement specifies the name of the new
database you want to create.

For example, to create a new database named "mydatabase" in SQL, you


would use the following command:
SQL
CREATE DATABASE mydatabase;
 CREATE TABLE
Creating a table in a database is like defining a structure to
store specific types of data. When you create a table in a database, you
specify the columns that will hold different types of data and define the
properties of those columns. This process is done using the CREATE
TABLE statement in SQL.

For example, to create a table named "students" with columns for


student ID, name, and age in SQL, you would use the following
command:
SQL
CREATE TABLE students (
student_id INT,
name VARCHAR(50),
age INT
);
 ALTER DATABASE
In SQL, the ALTER DATABASE statement is used to modify
an existing database. This statement allows you to make changes to the
database properties, such as changing the database name, setting
options, or modifying file properties.

For example, if you want to rename a database named


"mydatabase" to "new database," you can use the ALTER DATABASE
statement like this:
SQL
ALTER DATABASE mydatabase RENAME TO new database;
 FILTERING DATABASE
Filtering data is a crucial aspect of database querying,
especially in SQL. By using the WHERE clause in a SELECT statement, you
can specify conditions to retrieve specific records that meet your criteria.

For instance, if you have a table named "employees" and you want to
fetch only the employees who work in the IT department, you can use
SQL like this:
sql
SELECT * FROM employees WHERE department = 'IT';
 READING DATABASE
Reading data from a database involves retrieving
information stored in the database tables. In SQL, you typically use the
SELECT statement to read data from a database table.

For example, if you have a table named "students" with columns like
"student_id," "name," and "age," and you want to read all the data from
this table, you can use the following SQL query:
SQL
SELECT * FROM students;
 UPDATE DATABASE
When we talk about an "updated database," it typically
refers to a database that has been modified or changed in some way.
Updating a database can involve various operations such as adding new
records, modifying existing records, or deleting outdated information.

If a new student named John joins the school and needs to be added to
the database, you can update the database by inserting a new record for
John. Here's an example SQL query to add John to the "students" table:
SQL
INSERT INTO students (student_id, name, age) VALUES (101, 'John', 20);

In this query, we are updating the database by adding a new record for
John with a student ID of 101, name 'John', and age 20.
 DELETE DATABASE
Deleting in a database means removing specific data or
records from the database. It is a crucial operation to eliminate outdated
or unnecessary information from the database.

For example, if you have a database of employees and an employee has


left the company, you might want to delete their record from the
database. Here's a simple example using SQL to delete an employee
record:

If you want to delete an employee with the employee_id of 101 from the
"employees" table, you can use the following SQL query:
SQL
DELETE FROM employees
WHERE employee_id = 101;
 DELETE TABLE
Deleting a table in a database means removing the entire
table structure along with all the data it contains. This operation is
irreversible and permanently eliminates the table and its contents from
the database.

For example, if you have a table named "customers" in your database


and you want to delete it, you can use the following SQL query:
SQL
DROP TABLE customers;
 DELETE THE DATA INSIDE THE TABLE
To delete all the data inside a table without removing the
table structure itself, you can use the SQL command DELETE FROM
table_name;. This command will remove all the records from the
specified table while keeping the table structure intact.

For example, if you want to delete all the data from a table named
"students," you can use the following SQL query:
SQL---- DELETE FROM Student;
SQL CONSTRAINTS
SQL constraints are rules applied to columns in a table that help maintain
the integrity and accuracy of the data within the database. Here are
some common SQL constraints along with their meanings:

1. NOT NULL: This constraint ensures that a column cannot have a NULL
value. It requires a value to be present in the column whenever a new
record is inserted or updated.

2. UNIQUE: The UNIQUE constraint ensures that all values in a column


are unique and different from each other. It does not allow duplicate
values within the column.

3. PRIMARY KEY: The PRIMARY KEY constraint uniquely identifies each


record in a table. It combines the NOT NULL and UNIQUE constraints,
making sure that each value in the column is unique and not NULL.

4. FOREIGN KEY: The FOREIGN KEY constraint establishes a relationship


between two tables. It ensures that the values in a column match the
values in another table's column, typically the primary key in the
referenced table.

5. CHECK: The CHECK constraint defines a condition that each row in a


table must satisfy. It allows you to specify a condition that must be true
for the data to be valid.

6. DEFAULT: The DEFAULT constraint provides a default value for a


column when no value is specified during an INSERT operation. It helps
to ensure that a column always has a value, even if one is not explicitly
provided.
SQL constraints errors

Primary key: A primary key error occurs when there is an


issue with the primary key constraint in a database table.
This error typically indicates that there is a violation of the
primary key constraint, such as attempting to insert a
duplicate primary key value or inserting a null value into a
column that is part of the primary key.
Error looks like

Not null: Looks like A "not null" error means that you are
trying to insert a null value into a column that has been
defined as not allowing null values. This error occurs when
the database expects a value for a column that cannot be
empty, but you are trying to insert a null value instead.
Default: The "DEFAULT" constraint in SQL is used to provide a
default value for a column when no explicit value is specified
during an INSERT operation. This means that if a row is
inserted into a table without specifying a value for a column
with a DEFAULT constraint, the default value specified for
that column will be used instead

Check: When you encounter a "check error" in MySQL, it


usually refers to a constraint violation related to the CHECK
constraint. This error occurs when a condition specified in
the CHECK constraint is not met during an insert or update
operation. It means that the data being inserted or updated
does not satisfy the conditions defined in the CHECK
constraint for that column.
SUB QUERY
A subquery in MySQL is a query nested within another query. It is enclosed in
parentheses and can be used in various places, such as in SELECT, INSERT,
UPDATE, or DELETE statements, or within the WHERE, FROM, or HAVING
clauses of another query. Subqueries are useful for breaking down complex
queries into simpler parts, improving readability and maintainability.
Inner and outer queries:
inner and outer queries are not types of subqueries; instead, they describe the
relationship between the queries in a subquery structure.
>>>The inner query is the query inside the subquery that executes first, and its
result is passed to the outer query for further processing.
Syntax: (SELECT department_id FROM departments WHERE department_name
= 'Sales')
>>>The outer query is the main query that contains the subquery and uses the
result from the inner query to perform additional operations.
Syntax: SELECT employee_id, first_name, last_name FROM employees WHERE
department_id = ...
Aggregate Function:
In SQL, you can use aggregate functions like SUM, AVG, COUNT, MIN, and MAX
in subqueries to perform calculations on the result set returned by the inner
query. Here's an example using the SUM aggregate function in a subquery:
SELECT column_name
FROM table_name
WHERE column_name = (SELECT SUM(sales_amount) FROM sales_table);

In this example:
- The inner query calculates the total sales amount using the SUM function.
- The outer query then retrieves the column_name from the table where the
column_name matches the total sales amount obtained from the inner query.
Types:
In SQL, there are different types of subqueries based on where they can be
used and what they return:

1. Single-Row Subquery:
Highest value we retrieve from the column.
Syntax
Select*from table name where column name=(select max (column name) from
table name);

2. *Multiple-Row Subquery*:
In MySQL, a multi-row subquery is a query that can return multiple rows of
results within another SQL query.
(,=,!=,all,in,any)
In: Select*from table name where column name in (select column name from
table name where column name =values);
All: Select*from table name where column name all< (select column name
from table name where column name =values);
Select*from table name where column name all>(select column name from
table name where column name =values);

Any: Select*from table name where column name any< (select column name
from table name where column name =values); Select*from table name where
column name any> (select column name from table name where column name
=values);
In MySQL, "joins" are used to combine data from two or more tables based on a related column
between them. There are different types of joints in MySQL that serve different purposes:

1. Inner Join: Returns rows when there is a match between the columns in
both tables. It only includes rows that have matching values in both tables.
2. Left Join (or Left Outer Join): Returns all rows from the left table and the
matched rows from the right table. If there is no match, NULL values are used
for the columns from the right table.
3. Right Join (or Right Outer Join): Returns all rows from the right table and the
matched rows from the left table. If there is no match, NULL values are used for
the columns from the left table.
4. Full Join (or Full Outer Join): Returns all rows when there is a match in one
of the tables. It includes all rows from both tables, even if there is no match.

Here's an example to illustrate these types of joins:

Let's consider two tables: "employees" and "departments."

1. Inner Join Example:


SQL
SELECT employees.employee_id, employees.employee_name,
departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id =
departments.department_id;

This query will return employee details along with their respective department
names where there is a match between the department IDs in both tables.
2. Left Join Example:
SQL
SELECT employees.employee_id, employees.employee_name,
departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id =
departments.department_id;

3. Right Join Example for "employee" table:


SQL
SELECT employee.employee_id, employee.employee_name,
department.department_name
FROM employee
RIGHT JOIN department ON employee.department_id =
department.department_id;

4. Full Join Example for "employee" table:


SQL
SELECT employee.employee_id, employee.employee_name,
department.department_name
FROM employee
FULL JOIN department ON employee.department_id =
department.department_id;
Aggregate Functions:

In SQL, aggregate functions are used to perform calculations


on sets of values to return a single value as a result. These
functions operate on a group of rows and return a single
value that summarizes the data. Some common aggregate
functions in SQL include:

1. COUNT(): This function counts the number of rows that


match a specified condition.

2. SUM(): Calculates the sum of values in a column.

3. AVG(): Computes the average of values in a column.

4. MIN(): Returns the minimum value in a column.

5. MAX(): Returns the maximum value in a column.

These aggregate functions are often used in conjunction with


the SELECT statement and GROUP BY clause to perform
calculations on groups of data. They are helpful for
generating summary statistics and insights from the
database.

You might also like