22/01/2024
SQL
S
● QL - standard query language
● Manipulate, store and retrieve data
● DR EF Codd proposed the database concept -1970
○ Consists of collection of objects and relations
○ Set of operators to perform on the relations
○ Data integrity and consistency
● Relational model is 2D rows and columns
● Entity is the table itself (the name of the table), they are uppercase - column (vertical)
● Attributes are description of an entity, they are lowercase - rows (horizontal)
● Each row can be uniquely identified by a primary key
● Foreign keys are used to relate data from multiple tables
● The two keys share a parent-child relationship
● Field is basically a cell
● When there is an absence of value, we call it null
● Composite key is similar to primary key but it has more than one column to identify a
row
● Table has a relationship with itself- hierarchical relationship
● DML:
○ Select
○ Insert
○ Update
○ Delete
○ Merge
● DDL:
○ Create
○ Alter
○ Drop
○ Rename
○ Truncate
○ Comment
● DCL:
○ Grant:
○ Revoke:
● TC:
○ Commit:
○ Rollback:
○ Savepoint:
● Default value is used when there is no value given
● Where clause is used to filter records, specify a condition
● Data Types
○ Exact Numeric: int, smallint, tinyint, bigint, decimal, numeric
○ Approx Numeric: float,real
○ Date, time, datetime
○ Character and string: char,varchar, text, nchar,nvarchar,ntext
B
○ inary: binary, image
○ Miscellaneous: clob,blob,xml,cursor,table
DBMS and Features over File Systems
R
● Rdbms>> files, as data consistency, and redundancy is maintained, easily shareable
● Stores data in a structured manner
● Define relationships between tables and entities
● Consistency
● Data integrity
● Better security
● Data: Any information that can be given a meaningful definition.
○ It can be text, videos or images etc
○ Something that can be modified, analyzed etc
○ Collection of unorganized facts
● Information: data that can be organized and is presented in a meaningful context.
● Database: Storing data; organized collection of data
● DBMS: Manages data Bases
● RDBMS: data is represented in terms of rows and columns and each cell shows the
relationship it shares with the other cells
● ACID: Atomicity, Consistency, Isolation, Durability
● OLAP: Online Analytical Processing
● OLTP: Online Transaction Process: immediate transaction
Introduction to SQL and Sub-Languages (DDL, DML, DCL and TCL)
● D ML: Data manipulation language, they are not auto committed, and can be rolled
back
● They are used to add, modify and remove
○ Select: retrieves data from table
○ Insert: insert new data into table -- insert into table_name(column/row name)
then mention values();
● & is a place holder for the variable value which gives us a
prompt
○ Update: update the existing table
● Update table_name set column= value where ;
○ Delete: deletes data
● Delete from table_name where
○ Merge: merges source and target table with the combination of insert, delete
and update
● DDL:
○ C reate -- create table
○ Alter: modify existing or add a new attribute, alter structure of db
■ ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
■ ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
D
○ rop: delete both structure nd data
○ Rename: ALTER TABLE table_name RENAME TO new_table_name;
○ T runcate: removes all rows leaving the structure of the table empty, cannot be
rolled back
● Truncate table table_name;
○ Comment: - Single Line Comment (--)
- Multiline Comment (/*......*/)
● DCL:
○ Grant:it is used to provide privileges to database objects for a user
○ Revoke: operations opposite to the grant
● TC:
○ Commit: Commit is used to permanently save the changes done in the
transaction
● An automatic commit is when a dcl or ddl statement is issued
○ Rollback: Rollback is used to undo the transactions that have not been saved
○ Savepoint: is used to mark a point in a transaction that can be rolled back to
without affecting the previous work
● A database transaction consists of one ddl, dcl, dml statement that constitute one
consistent change to the data
● Read consistency guarantees constant view of data at all times
○ Changes made by one user does not conflict with the changes made by
another user.
● Lock table is used to lock one or more tables and aren't unlocked until you commit or
rollback
● FOR UPDATE: locks rows identified by the select query -- multiple users accessing
same data
erify the table structure - DESC/DESCRIBE
V
● DESC table_name;
It shows the structure of table and what data is stored in each column
●
Usage of DEFAULT
○ used to specify a default value for a column. This default value will be
automatically inserted into the column for any new records that do not provide
an explicit value for that column.
○ Ex:
ALTER TABLE Customers
ALTER City SET DEFAULT 'New York';
Data Filtering - WHERE Clause
T
○ he WHERE clause is used to filter records.
○ It is used to extract only those records that fulfil a specified condition.
Implementation and Detailed Explanation of Constraint Concepts
○ - PRIMARY KEY: uniquely identifies each row, not null
○ - CHECK: ensures values satisfies a condition
○ - NOT NULL: column cannot have a null value
○ - FOREIGN KEY: prevents actions that would destroy links between tables
○ - UNIQUE: all the values in a column are different
■ is to make sure that the information is different in the column
● It doesn’t accept duplicate values
● Can accept only one null value
○ Constraints are rules
Maintains the integrity of the database
○
○ Constraint constraint_name constraint_type
Data Retrieval/ Selection - SELECT :
○ SELECT column1, column2, ...
FROM table_name;
○ The SELECT statement is used to select data from a database.
Data Selection - DISTINCT Clause
○ SELECT DISTINCT column1, column2, ...
FROM table_name;
○ To delete or show duplicate values only once then use the DISTINCT keyword
Data Filtering - WHERE Clause
○ SELECT column1, column2, ...
FROM table_name
WHERE condition;
○ WHERE clause helps in bringing only certain rows and columns from the table
■ It follows the FROM clause
UAL Table : The DUAL table is a special one-row, one-column table that is
D
automatically created and maintained by Oracle Database.
Column Aliasing (AS): SQL aliases are used to give a table, or a column in a table, a
temporary name.
○ Column Alias: it renames the column heading and is useful for calculations
○ We can use the AS keyword
○ We can also use ""
Concatenation Operator(||):
○ Concatenation operator links columns or character strings to other columns
○ Represented by (||)
○ Concat()
○ Concat(concat()) in order to give a space use ' '
○ Numeric data is justified to right and character data is justified to left
○ Mysql uses more than 2 parameters
○ In oracle it only uses 2 parameters
A literal is included in the SELECT , they are enclosed with '', more english friendly
●
○ Apostrophe returns error
○ Use q'[]' for quote operator, you can also use ? Or ! Etc but not & -- alternative
quote operator
NVL Function : NVL function is used to replace the null values
○ It has 2 parameters, which are the number of the column and the 2nd
parameter is the number you want the null to be replaced with
ingle Row Functions (CONCAT, LOWER , UPPER , INITCAP ,SUBSTR , INSTR
S
,TRANSLATE, REPLACE)
○ Single row functions: results one result per row
○ Character functions:
■ Case conversion
○ LOWER: changes the letters to lower case
○ UPPER: changes the letters to upper case
○ INITCAP: makes only the first letter as capital
■ Character manipulation
○ CONCAT
○ SUBSTR: substring
○ LENGTH
○ INSTR: occurrence of the character, select the specific
position of a character
○ TRIM: remove character, use leading and trailing
○ REPLACE: replaces all occurrences of a substring
within a string with a new substring
○ LPAD | RPAD : add character to increase the length of
the string
■ Single row functions can be nested to any level
■ Numeric functions
○ Round
○ Trunc
○ Ceil (rounds up)
○ Floor (rounds down)
○ Mod (remainder)
○ Months_between
○ Add_months
○ Next_day
○ Last_day
○ Round
○ Trunc
Comparison Operators:
○ =,>,<,>=,<=,<> (not equal to), BETWEEN…AND…,IN(set)(or),LIKE,IS NULL
○ Like operator is used to perform wildcard searches
■ % denotes zero or more characters
■ _ denotes one character
■ Like %a is used for names that ends with a
■ Like a% that starts with a
■ Like '___'; at least three characters
Logical Operators - AND, OR, NOT