Professional Documents
Culture Documents
Database
Management
SQL
Data Types
Constraints
Operators
Clauses
Functions
Stored Procedures
Subquery Types
Indexing
Data Types
4) TRUNCATE–is used to remove all records from a table, including all spaces allocated for the
records are removed.
);
Constraints – Part 2
NOT NULL - Ensures that a column cannot have a NULL value
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
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.
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
• 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.
Security Measures
Best Practices
Joins in DBMS
First Normal Form (1NF)
Contains atomic, unique value.
BEFORE AFTER
Second Normal Form (2NF)
Must be in 1NF.
BEFORE AFTER
Star Database Schema
Every dimension is represented with only one-
dimension table.
Continuously monitor.
Apply restrictions.
Manage logins.
Best Practices
Follow naming conventions.
Normalize as necessary.
Well-commented code.
Password encryption.