You are on page 1of 21

SQL &

Database
Management
SQL
Data Types

DDL & DML

Constraints

Operators

Clauses

Functions

Stored Procedures

Subquery Types

Indexing
Data Types

INT, BIT, DECIMAL, DATE, TIME, DATE TIME. CHAR, VARCHAR,


NUMERIC, MONEY,
FLOAT, REAL
Data Definition Language (DDL)
1) CREATE – is used to create the database or its objects (like table, index, function, views,
store procedure and triggers).

2) DROP – is used to delete objects from the database.

3) ALTER-is used to alter the structure of the database.

4) TRUNCATE–is used to remove all records from a table, including all spaces allocated for the
records are removed.

5) COMMENT –is used to add comments to the data dictionary.

6) RENAME –is used to rename an object existing in the database.


Data Manipulation Language (DML)
1) INSERT – is used to insert data into a table.
INSERT INTO Student (ROLL_NO, NAME, Age) VALUES (‘5′,’PRATIK’,’19’);

2) UPDATE – is used to update existing data within a table.


UPDATE Student SET NAME = 'PRATIK', ADDRESS = 'SIKKIM' WHERE ROLL_NO = 1;

3) DELETE – is used to delete records from a database table.


DELETE FROM Student WHERE NAME = 'Ram’;

4) SELECT – is used to select records from a database table.


SELECT FROM Student WHERE NAME = ‘Mridul';
Constraints – Part 1
o SQL constraints are used to specify rules for data in a table.

o Create Table Constraint - specified with CREATE or ALTER TABLE statements.

CREATE TABLE table_name (

column1 datatype NOT NULL UNIQUE,

column2 datatype UNIQUE,

column3 datatype NOT NULL,

);
Constraints – Part 2
 NOT NULL - Ensures that a column cannot have a NULL value

 UNIQUE - Ensures that all values in a column are different

 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. uniquely identifies each table
row
 FOREIGN KEY - Prevents actions that would destroy links between tables

 CHECK - Ensures that the values in a column satisfies a specific condition

 DEFAULT - Sets a default value for a column if no value is specified

 CREATE INDEX - Used to create and retrieve data from the database very quickly
Operators
Arithmetic Comparison Logical

+ : Adds both operands = : if two operands are equal AND : allow multiple conditions in
SQL statements.
- : Subtract the right-hand from the != & <> : if two operands are
left-hand operand unequal BETWEEN : search for values within a
range.
* : Multiply both operands > : if left operand is more than right
IN : compare if a value exists in a list of
/ : Divide the left-hand by the right- < : if left operand is less than right values.
hand operand >= : if left operand is more than or EXISTS : search for a row's presence in
equal to right a table.
% : Divide the left-hand operand by
the right-hand operand and returns <= : if left operand is less than or LIKE : compare a value to similar
remainder. equal to right operand values using wildcard operator.
Clauses
GROUP BY HAVING ORDER BY

Used to arrange identical data Used to specify a search Sorts the result in ascending or
into groups condition for a group or an descending order.
aggregate.
Follows the WHERE in SELECT Ascending order is default.
statement Used in a GROUP BY.
DESC is used for sorting in
Precedes the ORDER BY in If not using GROUP BY then descending order and ASC is
SELECT statement. you can use HAVING used for sorting in ascending
function like a WHERE order
Used with aggregate function.
clause.
Functions
Aggregate Scalar
Return a single value, calculated from values in a column. Return a single value, based on the input value
AVG() - Returns the average value specified.

COUNT() - Returns the number of rows UCASE() - Converts a field to upper case

FIRST() - Returns the first value LCASE() - Converts a field to lower case

LAST() - Returns the last value MID() - Extract characters from a text field

MAX() - Returns the largest value LEN() - Returns the length of a text field
MIN() - Returns the smallest value ROUND() - Rounds numeric field to certain decimals
SUM() - Returns the sum NOW() - Returns the current system date and time
Stored Procedures
o If you have an SQL query that you want to write & execute over and over again, save it as a
stored procedure, and then just call it.
o You can also pass parameters to a stored procedure, so that the stored procedure can act based
on the parameter value(s) passed.

CREATE PROCEDURE SP_PROC_NAME AS

SELECT * FROM Customers

GO;

EXEC SP_PROC_NAME;
Subquery Types
Single Row Multiple Row Correlated

Returns single row output. Returns multiple row output. Depend on data provided by the
outer query
Single row comparison operators, Multiple row comparison
with WHERE conditions. operators like IN. Use EXISTS operator to

SELECT name, dept_id FROM SELECT EMPLOYEE_ID,


SELECT dept_id, MIN (salary)
tblEmployees WHERE dept_id salary, dept_id FROM
FROM tblEmployees GROUP
IN (SELECT dept_id FROM tblEmployees E WHERE salary >
BY dept_id HAVING MIN
tblDept WHERE LOCATION (SELECT AVG(salary) FROM
(salary) < (SELECT AVG tblEmployees T WHERE
(salary) FROM tblEmployees) = ‘Delhi’)
E.dept_id = T.dept_id)
Indexing
• Indexes are used to retrieve data from the table faster.

• Users cannot see them, they are just there to speed up searches/queries.

• Updating a table with indexes takes more time than a table without

• So, only create indexes on columns that will be frequently searched against.

CREATE [UNIQUE] INDEX index_name

ON table_name (column1, column2, ...);


• With UNIQUE keyword, duplicate values are not allowed
Database Management
Joins in DBMS

First Normal Form

Second Normal Form

Star Database Schema

Snowflake Database Schema

Security Measures

Best Practices
Joins in DBMS
First Normal Form (1NF)
 Contains atomic, unique value.

 Table attribute cannot hold multiple values. Must be only single-valued.

BEFORE AFTER
Second Normal Form (2NF)
 Must be in 1NF.

 All non-key attributes are fully functional dependent on primary key.

BEFORE AFTER
Star Database Schema
Every dimension is represented with only one-
dimension table.

Dimension table is joined to fact table using


foreign key

Dimension tables are not joined to each other

Fact table would contain only key and measure

Star schema is easy to understand and provides


optimal disk usage

Dimension tables are not normalized


Snowflake Database Schema
The snowflake schema uses
smaller disk space.

Due to multiple tables


query performance is
reduced

The need to perform more


maintenance because of the
more lookup tables.

Requires many joins to


retrieve the data
Security Measures
 Regularly update.

 Continuously monitor.

 Protect against SQL injection.

 Keep taking backups.

 Apply restrictions.

 Isolate the server.

 Manage logins.
Best Practices
 Follow naming conventions.

 Normalize as necessary.

 Well-commented code.

 Password encryption.

 Avoid hard deletion.

 Sequential ID as primary key.

 Avoid Select *, use columns

You might also like