SQL
SQL
What is an Application?
(i) Accuracy
(ii) Consistency
(iii) Efficiency
(iv) Security
Types of Application:
(ii) Web application: Any application that is accessed through browser is known
as web application.
What is Data?
Data is a raw fact that describes the attributes of the entity.
What is a Database?
Database is a place/ medium to store the data in systematic and organized way.
We can perform four operations that can be performed in the database they are:
(i) Create/ Insert
(ii) Read/ Retrieve
(iii) Update/ Modify
(iv) Delete/ Drop
These operations are known as CRUD operations.
Types of DBMS:
RDBMS S/W:
MySQL
Oracle Database
MS SQL Server
Postgre SQL
SQLite
SQL Developer
IBM DB2
Amazon Aurora
Amazon RDS
Google Cloud
Maria DB
Velneo
Vantage
Scalway
Harper DB
Conzept 16
DB Vizualizer
SQL Server
Non-RDBMS S/W:
Mongo DB
Cassandra
Couchbase
Hbase
Amazon Dynamo DB
Neo 4j
Intersystem IRIS
The RDBMS S/W use SQL whereas the Non-RDBMS S/W uses NO-SQL(not-only
SQL) i.e., Scripting languages such as Java script, Type script,etc.
RDBMS and SQL was invented by IBM and currently maintained by Oracle.
RDBMS is a type of DBMS software used to maintain and manage the database.
The two main features of RDBMS are: Security, Authorization.
In RDBMS, we store the data in the form of tables.
To communicate with the RDBMS we use Structured Query Language(SQL).
Difference b/w DBMS and RDBMS:
DBMS RDBMS
DBMS stands for Database Management RDBMS stands for Relational Database
System. Management System.
DBMS is a software used to maintain RDBMS is a type of DBMS software
and manage the database. used to maintain and manage the
database.
In DBMS, we are storing the data in file In RDBMS, we are storing the data in
format. the table format.
-+-To communicate with the DBMS, we To communicate with the RDBMS, we
are using Query language. are using Structured Query Language.
Relational Model:
Structure of Table:
History of SQL:
Rules of E.F.Codd:
Rule 1: All the data entered into the cell must be single valued(atomic).
Rule 2: In RDBMS, all the data should be stored in the form of tables including the
meta data.
Rule 3: In RDBMS, data can be stored in multiple tables, if required we can establish
the connection between the tables with the help of key attributes.
Rule 4: All the data entered into the table must be validated, if required we can
validate the data by two methods
(i) By assigning data types
(ii) By assigning constraints
Data types:
Data types are used to specify what type of data or what kind of data can be stored in
the particular memory location. Data types are classified as:
(i) Char()
(ii) Varchar()/ Varchar2()
(iii) Number()
(iv) Date
(v) Timestamp
(vi) Large object:
(a) Character large object(CLOB)
(b) Binary large object(BLOB)
Varchar():
Varchar2():
CHAR() VARCHAR()
It follows fixed length memory allocation It follows variable length memory allocation
system. system.
The unused memory in char() is wastage The unused memory in varchar() is free
memory. memory.
It can store upto 2000 characters. It can store upto 4000 characters.
Number():
Date:
Timestamp:
Large object:
Character large object(CLOB): This data type is used to store the huge amount of
characters upto 4GB. Syntax: clob.
Binary large object(BLOB): This data type is used to store the binary values of
images, documents, mp3(audio files), mp4(videos) upto 4GB. Syntax: blob.
Constraint:
Unique:
Unique is a constraint which is used to specify that the column should not accept
duplicate or repeated values.
Not null:
Not null is a constraint which is used to specify that the column should not accept the
null values or it is used to specify the mandatory field.
Check():
Default:
This constraint is used to set a default value for a column when the value is not
specified.
Auto Increment:
Binary:
It is a constraint which is used to specify the column to take only the binary values(0’s
and 1’s).
Primary key:
Primary key is a constraint which is used to identify the records uniquely from the
table.
Primary key will not accept duplicate or repeated values.
Primary key will not accept null.
Primary key is a combination of unique and not null constraints.
We can have only one primary key in a table.
Primary key is not mandatory, but highly recommended.
Foreign key:
Foreign key is a constraint which is used to establish the connection between the
table.
Foreign key can accept duplicate or repeated values.
Foreign key can accept null values.
It is not a combination of unique and not null.
We can have any number of foreign key in the table.
Foreign key is present in the child table but actually belongs to the parent table.
To be a foreign key, it is mandatory that it should be the primary key in the parent
table.
Foreign key is also known as the Referential integrity constraint.
Until the column is present in the child table, we cannot destroy the parent table.
Overview of SQL:
This language is used to question the database (or) it is used retrieve the data from the
database.
There are 4 statements in DQL, they are:
1) Select: This statement is used to retrieve the data from the table and
display the result.
2) Projection: The process of retrieving the data from the table by selecting
only the columns is known as projection. In projection, all the data under
the column will be selected by default.
3) Selection: The process of retrieving the data from the table by selecting
both rows and columns is known as selection.
4) Joins: The process of retrieving the data from the multiple table
simultaneously is known as joins.
Projection:
The process of retrieving the data from the table by selecting only the columns is
known as projection.
In projection, all the data under the column will be selected by default.
Syntax:
Select */ [Distinct] Column_name/ Expression [Alias]
From Table_name;
Order of execution:
1) From
2) Select
From clause will start the execution.
From clause will execute first, it will go to the database and search for the table. If the
table is present, it will put the table under execution.
To the from clause we can pass the table name as an argument.
Select clause will execute after the execution of from clause.
Work of select clause is to retrieve the data from the table and display the result.
To the select clause we can pass three arguments:
1) Asterisk (*)
2) Column name
3) Expression
Asterisk (*):
It is used to retrieve all the data from the table (or) it is used to select all the columns.
It is used to specify the end of the statement (or) end of the query.
Basic Commands:
Tab is a meta table which consist of the details of all the tables present in the database.
Expression:
Expression is a combination of operator and operands (or) Any statement which gives
the result is known as expression.
Expression can be written in 2 formats:
1) Col_name operator value
2) Col_name operator col_name
Access Operator:
Whenever we are using * it should be used alone, along with * we cannot pass any
other argument.
But whenever we need to retrieve all the data from the table along with some
additional details, we should use
Table_name . *
Alias:
Alias is an alternative name given to the column name or expression in the result
table.
Alias name can be written with or without using the keyword as.
Alias name should always be single string (or) separated by underscore.
If we want to write multiple string as alias name, it should be written or enclosed
within double quotes.
Distinct Clause:
Distinct is a clause which is used to remove the duplicate or repeated values form the
result table.
Distinct clause should be always the first argument in the select clause.
We can pass multiple arguments to the distinct clause, it will remove the record if the
combination of the arguments are repeated/ duplicated.
Selection:
The process of retrieving the data from the table by selecting both rows and columns
is known as selection.
Where Clause:
Literals:
Operators: