0% found this document useful (0 votes)
3 views30 pages

SQL Basics

Uploaded by

vedantb062
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
3 views30 pages

SQL Basics

Uploaded by

vedantb062
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 30

Normalization

NORMALIZATION is a database design technique that organizes


tables in a manner that reduces redundancy and dependency
of data. Normalization divides larger tables into smaller tables
and links them using relationships. The purpose of
Normalization is to eliminate redundant (useless) data and
ensure data is stored logically.
EMP_ID EMP_NAME EMP_PHONE EMP_STATE

14 John 7272826385, UP
9064738238
20 Harry 8574783832 Bihar
12 Sam 7390372389, Punjab
The decomposition of the EMPLOYEE table into 1NF8589830302
has been shown below:

EMP_ID EMP_NAME EMP_PHONE EMP_STATE

14 John 7272826385 UP
14 John 9064738238 UP
20 Harry 8574783832 Bihar
12 Sam 7390372389 Punjab
SQL

SQL is a standard language for storing, manipulating and


retrieving data in databases.
DDLCommandsinSQL

DDL is a Data Definition Language.


The DDL Commands in Structured Query Language are used to create and modify the
schema of the database and its objects. The syntax of DDL commands is predefined for
describing the data. The commands of Data Definition Language deal with how the data
should exist in the database.

Following are the five DDL commands in SQL:


1.CREATE Command
2.DROP Command
3.ALTER Command
4.TRUNCATE Command
5.RENAME Command
CREATE Command

Syntax to create a new table:


CREATE TABLE table_name
(
column_Name1 data_type ( size of the column ) ,
column_Name2 data_type ( size of the column) ,
column_Name3 data_type ( size of the column) ,
column_NameN data_type ( size of the column ),
);
1. CREATE TABLE Student
2. (
3. Roll_No. Int ,
4. First_Name Varchar (20) ,
5. Last_Name Varchar (20) ,
6. Age Int ,
7. Marks Int ,
8. );
SQLDROPTABLE

A SQL DROP TABLE statement is used to delete a table definition and all data from a table.

syntax to drop the table from the database.


DROP TABLE "table_name";
SQL DELETE TABLE
The DELETE statement is used to delete rows from a table. If you want to
remove a specific row from a table you should use WHERE condition.

DELETE FROM table_name [WHERE condition];


SQL RENAME Database

Rename Database statement in SQL is used to change the name of the


existing database.

Syntax of Rename Database in SQL

RENAME DATABASE old_database_name TO new_database_name;


ALTER DATABASE old_database_name MODIFY NAME = new_database_name;

EX- ALTER DATABASE Department MODIFY NAME = Company ;


SQL TRUNCATE TABLE

A truncate SQL statement is used to remove all rows (complete data) from a
table. It is similar to the DELETE statement with no WHERE clause.

TRUNCATE TABLE table_name;


SQLALTERTABLE

The ALTER TABLE statement in Structured Query Language allows


you to add, modify, and delete columns of an existing table. This
statement also allows database users to add and remove various SQL
constraints on the existing tables.

Syntax of ALTER TABLE ADD Column statement in SQL

ALTER TABLE table_name ADD column_name column-definition;

EX- ALTER TABLE Cars ADD Car_Model Varchar(20);


DML Commands in SQL

The DML(Data Manipulation Language). commands in Structured Query Language


change the data present in the SQL database. We can easily access, store,
modify, update and delete the existing records from the database using DML
commands.

Following are the four main DML commands in SQL:


1.SELECT Command
2.INSERT Command
3.UPDATE Command
4.DELETE Command
SELECT Command

Syntax of SELECT DML command

SELECT * FROM table_name;

INSERT DML Command

INSERT INTO table_name


VALUES (value1, value2, value3, ...);
SQL UPDATE Statement

UPDATE Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Example

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
DELETE DML Command

Syntax of DELETE Command

DELETE FROM Table_Name WHERE condition;

EX-

DELETE FROM Product WHERE Product_Id = 'P202' ;


DCL commands

DCL (Data Control Language):

DCL includes commands such as GRANT and REVOKE which mainly


deal with the rights, permissions, and other controls of the database
system.
List of DCL commands:
GRANT: This command gives users access privileges to the
database.
REVOKE: The REVOKE statement can be used to remove granted
permissions, and the DENY statement
TCL commands:

• COMMIT: Commits a Transaction.

• ROLLBACK: Rollbacks a transaction in case of any error occurs


• .
• SAVEPOINT:Sets a savepoint within a transaction.

• SET TRANSACTION: Specify characteristics for the transaction


Sno Char VarChar/VarChar2

It is used to store It is used to store


2 character string of fixed character string of
length variable length

It has a Maximum Size of It has a Maximum Size of


3
2000 Bytes 4000 Bytes
SQL Operators

SQL Arithmetic
Operators
Operator Description Example

+ Add Try it
- Subtract Try it
* Multiply Try it
/ Divide Try it
% Modulo
SQL Bitwise Operators

Operator Description

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR
SQLComparison Operators

Operator Description Example

= Equal to Try it
> Greater than Try it
< Less than Try it
>= Greater than or equal to Try it
<= Less than or equal to Try it
<> Not equal to
SQL Logical Operators
Operator Description Example

ALL TRUE if all of the subquery values meet the condition Try it

AND TRUE if all the conditions separated by AND is TRUE Try it

ANY TRUE if any of the subquery values meet the condition Try it

BETWEEN TRUE if the operand is within the range of comparisons Try it

EXISTS TRUE if the subquery returns one or more records Try it

IN TRUE if the operand is equal to one of a list of expressions Try it

LIKE TRUE if the operand matches a pattern Try it

NOT Displays a record if the condition(s) is NOT TRUE Try it

OR TRUE if any of the conditions separated by OR is TRUE Try it

SOME TRUE if any of the subquery values meet the condition


SQL BETWEEN Operator

The SQL BETWEEN Operator


The BETWEEN operator selects values within a given range. The values can be numbers, text,
or dates.
The BETWEEN operator is inclusive: begin and end values are included.

BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Ex-
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
pattern matching operator
• LIKE clause is used to perform the pattern matching task in SQL.
• A WHERE clause is generally preceded by a LIKE clause in an SQL query.
• LIKE clause searches for a match between the patterns in a query with the pattern in the
values present in an SQL table. If the match is successful, then that particular value will be
retrieved from the SQL table.
• LIKE clause can work with strings and numbers.

SELECT * FROM course WHERE course_name LIKE 'd%';

You might also like