RCS classes
SQL notes
Database: It can be defined as collection of interrelated data stored
together to serve multiple applications.
Benefits of DBMS
➢ No data redundancy.
➢ No data inconsistency .
➢ Secured and shareable data.
DBMS
➢ A Data Bare Management System refers to a software that is
responsible for storing, maintaining and utilizing databases.
➢ A database along with a DBMS is referred to as a Database
System.
Data Redundancy: Duplication of data is known as Data Redundancy.
Data Inconsistency: Mismatched multiple copies of same data is known
as Data inconsistency.
Relational Database Model
➢ In this model, the data is organized into tables (i.e. rows and
columns)
➢ These tables are called Relations.
➢ Rows of relations are generally referred to as Tuples.
➢ Columns of relations are generally referred to as Attributes.
➢ The values stored in a relation are called Data item.
➢ Domain is a pool of values from which the actual values appearing
in a given column are drawn.
➢ Degree means number of Columns/Attributes.
➢ Cardinality means number of Rows/Tuples.
Properties of a Relation:-
➢ All rows of a relation must be distinct.
➢ Ordering of rows and columns are immaterial.
➢ Each row of the relation can be uniquely identified by its contents.
➢ In any given column of a table, all items are of same type.
Primary Key: a primary key is a set of one or more attributes that
uniquely identify tuples within a relation.
➢ It should be NOT NULL and Unique.
➢ A table can have only one primary key.
➢ It does not have a duplicate values.
Candidate Key: a candidate key is a column or a group of columns,
which uniquely identify each record of a table.
OR
The columns which are able to become primary key are called
candidate key.
➢ It should be NOT NULL and Unique.
➢ It should be a minimal set of attributes.
Alternate Key: the candidate key which is not selected as a primary key
is called alternate key.
➢ A table can have multiple alternate keys.
Foreign Key: a non key attribute, whose values are derived from the
primary key of some other table is known as foreign key in current table.
➢ It is used to represent the relationship between two tables.
SQL: Structured Query Language
MySQL: It is an Open-Source RDBMS.
Features:
➢ It is very fast and easy to use.
➢ It is an open source software so it is free of cost.
➢ It supports Query Language.
➢ Variety of data types can be stored.
➢ It is very secure database and it is scalable.
MySQL is a database, in order to access the data from this database we
use SQL language.
SQL is the set of commands that is recognised by all the RDBMS.
Difference between MySQL and SQL
➢ SQL is a language which is used to operate your database
whereas MySQL was one of the first open source database
available in the market.
➢ SQL is used in the accessing, updating, and manipulation of data
in a database while MySQL is an RDBMS that allows keeping the
data that exists in a database organised.
➢ SQL is a structured query language and MySQL is a RDBMS to
store, retrieve, modify and administrate a database.
➢ SQL is a query language while MySQL is a database software.
Categories of SQL commands
➢ DDL(Data Definition Language):- It allows us to perform tasks
related to the structure of a table.
❖ CREATE TABLE
❖ ALTER TABLE
❖ DROP TABLE
➢ DML(Data Manipulation Language):- It enables us to access or
manipulate data from the table.
❖ SELECT
❖ INSERT into
❖ DELETE
❖ UPDATE
➢ DCL(Data Control Language):- it includes commands such as
GRANT and REVOKE which mainly deals with rights, permissions
and other controls of the database system.
Data types in MYSQL
MySQL COMMANDS
CREATE database:- This command is used to create your own
database.
Syntax:-
SHOW database:- This command shows all the databases created
already.
Syntax:-
USE database:- This command is used access the specific database.
Syntax:-
SHOW tables:- This command is used to display all the tables for the
selected database.
Syntax:-
CREATE tables:- This command is used to create a table in a selected
database.
Syntax:-
DESC command:- This command is used to see the structure of a table.
Syntax:-
INSERT command:- This command is used to insert rows in a table.
➢ Data values are in the same order as the column names in a table.
Syntax:-
➢ The columns that are not listed in the insert command, will have
their default values, if it is defined for them, otherwise NULL
values.
Syntax:-
➢ To insert NULL value in a specific column, type NULL without
quotes.
Syntax:-
➢ Dates are by default entered in ‘YYYY-MM-DD’ format. All this is
enclosed in single quotes.
Syntax:-
SELECT command:- This command is used to retrieve a subset of rows
or columns from one or more tables.
➢ The order of columns in select command determines the order of
columns in the output.
Syntax:-
➢ If you want to see the entire table, i.e. every column of table, you
need to give a complete list of columns.
➢ This (*) can be substituted to display all columns.
Syntax:-
WHERE clause:- The WHERE clause in SELECT statement specifies
the criteria for selection of rows to be returned.
Relational Operator Syntax:-
Logical Operator Syntax:-
Putting Text in query output
Syntax:-
Scalar expression
Syntax:-
Column Alias
Syntax:-
DISTINCT Command:- This command is used to remove redundant
data.
➢ If there are multiple NULL values, then the NULL will appear only
one time.
Syntax:-
BETWEEN Command
➢ This Operator defines the range of values that the column values
must fall in to make the condition True.
➢ Boundary values are also included.
➢ The range includes both the upper value and lower value as well.
Syntax:-
NOT BETWEEN Command
➢ Reverse of BETWEEN operator.
➢ Rows not satisfying the BETWEEN condition are retrieved.
Syntax:-
In Operator:- This operator selects values that match any value in a
given list of values.
Syntax:-
Not Interested Operator:- Reverse of IN Operator.
Syntax:-
LIKE Operator:- patterns are described using two special wild card
characters:
➢ (%) → The % character matches any substring.
➢ (_) → The _ character matches only one character.
Syntax:-
❖ Display the name of students whose name starts with N.
❖ Display the name of students whose name ends with a.
❖ Display the name of students in which second character is i.
❖ Display the name of students in which second last character is h.
❖ Display the name of students who have 5 characters in their name.
NOT LIKE Operator:- Reverse of LIKE Operator.
Syntax:-
❖ Display the name of students who don’t have 5 characters in their
name
IS NULL Operator:- This operator is used to search NULL value in a
column.
➢ To search non-al values we can use NOT NULL.
Syntax:-
ALTER Command:- The new column will be added with null values for
all rows.
Original Table:
Functions:
➢ Add the columns to an existing table.
Syntax:-
➢ Delete a column from an existing table.
Syntax:-
➢ Modified the definition of existing column.
Syntax:-
➢ Change the name of an existing column.
Syntax:-
UPDATE Command:- this command is used to update or change the
existing values in a table.
Syntax:-
➢ We can update multiple rows also.
Syntax:-
DELETE Command:- This command helps to delete the row of a table.
➢ If we use DELETE command without condition, then it will delete
all the rows.
➢ But it's structure remains intact.
Syntax:-
DROP Command:- This command is used to delete the entire table
including its structure and contents.
Syntax:-
AGGREGATE FUNCTIONS( ):- AGGREGATE functions or GROUP
functions work upon group of rows, rather than on single row.
SUM( ):- This function returns the sum of values in a given column or
expression.
Syntax:-
AVG( ):- This function computes the average of given data.
Syntax:-
MAX( ):- This function returns the Maximum Value from a given column
or expression.
MIN( ):- This function returns the Minimum Value from a given column or
expression.
Syntax:-
COUNT( ):- This function counts the number of NOT NULL values.
COUNT(*):- This function counts the number of rows in a given column
or expression including NULL and duplicate values.
Syntax:-
ORDER BY Clause:- This clause allows sorting of query results by one
or more columns.
Syntax:-
GROUP BY Clause:- this clause combines all those records that have
identical values in a particular field. Grouping can be done with
aggregate functions.
Syntax:-
HAVING Clause:- This clause places conditions on groups in contrast to
WHERE clause that places conditions on individual rows.
Syntax:-
JOINS
➢ A join is a query that combines rows from two or more tables.
➢ In Join-Query, more than one tables are listed in FROM clause.
Syntax:-
EQUI JOIN
➢ SQL, EQUI JOIN performs a JOIN against equality or matching
columns values of the associated tables. An equal sign (=) is
used as comparison operator in the WHERE clause to refer
equality.
➢ You may also perform EQUI JOIN by using JOIN keyword followed
by ON keyword and then specifying names of the column along
with their associated tables to check equality.
Syntax:-
SELECT * FROM Emp JOIN Dept [ON ([Link] = [Link])]
Syntax:-
Table Aliases
➢ A table aliases is a temporary label given along with the table
name in FROM clause.
➢ To cut down the amount of typing required in your queries you can
use a aliases for table names in SELECT and WHERE clauses.
Syntax:-
NATURAL JOIN
➢ The SQL, NATURAL JOIN is a type of EQUI JOIN and it is
structured in such a way that, columns with the same name of
associated tables will appear once only.
Syntax:-
MySQL Constraints
➢ Constraints are the rules enforced on the data columns of a
table.
➢ These are used to limit the type of data that can go into a table.
➢ This ensures the accuracy and reliability of the data in the
database.
PRIMARY KEY
➢ The PRIMARY KEY constraint uniquely identifies each record in a
table.
➢ Primary keys most contain UNIQUE values and cannot contain
NULL values.
➢ A table can have only ONE primary key; and in the table, this
primary key can consist of single or multiple columns (fields).
ALTER TABLE (ADD and REMOVE PRIMARY KEY)
➢ We can add PRIMARY KEY either by using CREATE TABLE
command or by using ALTER TABLE command on a column.
Syntax:-
➢ We can add the PRIMARY KEY constraint on a column, using
ALTER COMMAND, ONLY when that particular column does not
violet the rules of PRIMARY KEY.
Syntax:-
➢ We can remove PRIMARY KEY constraint from a column of an
existing table by using DROP keyword along with the ALTER
TABLE statement.
Syntax:-
➢ We cannot add more than one PRIMARY KEY.
Syntax:-
UNIQUE KEY Constraint
➢ The purpose of a UNIQUE KEY is to ensure that the information in
the column for each row must be unique.
➢ UNIQUE KEY constraint can have NULL values.
NOT NULL Constraint
➢ The column having NOT NULL constraint cannot contain NULL
values.
DEFAULT Constraint
➢ DEFAULT constraint is used to assign the default value to a
column, when user does not provide any value.
➢ However, if a user provides any value, then it will be overwrite.
CHECK Constraint
➢ The check constraint ensures that all the values in a column
satisfies certain conditions.
Syntax:-
The End