MySQL
MySQL
Database concepts
Database : A database is a tabular organization of data which comprises of
rows (record) and columns (attributes)
Each record contains values for the corresponding attributes.
💡 Abstraction
Data abstraction in dbms refers to the process of hiding irrelevant
details from the user.
Database concepts 1
Database management system (DBMS)
DBMS : it is a set of programs that enables the users to define, create and
maintain the database and provide controlled access to this database.
Advantages :
Sharing of data
Improved data integrity : Data integrity refers to the validity and consistency
of stored data. The system itself checks for the correct information to be
entered by the user in the correct format.
Database concepts 2
There are various data models and one of them is RDBMS or relational
database model.
Domain of a relation : it is the set of all possible values an attribute can have.
For example : in the table student, the attribute gender has a domain value of
two as it can have only two possible values : male or female.
Database keys
Primary keys : A primary key is an attribute or a collection of attributes which
uniquely identify tuples within the table.
Candidate keys : refers to all the attributes in a table that are capable of
becoming a primary key or are candidates.
Database concepts 3
Alternate Keys: A candidate key that is not a primary key or in other words, any
attribute that is a candidate for the primary key but is not a primary key , is an
alternate key.
Out of the multiple candidate keys , only one is selected to be the primary key.
The rest are alternate keys.
Foreign key : it’s a key found in a table whose value is derived from the primary
table of another table. It is the primary key of a table that is placed into a
related table to present the relationship between these tables.
MySQL :
MySQL is an open-source and freely available relational database management
system (RDBMS) that uses Structured Query Language (SQL). It is the most
common language used to create, operate, update, manipulate and
communicate with a database.
Advantages of SQL:
Easy to use : very easy to learn and use
No coding required.
SQL datatypes:
Example: INT(11)
occupies 4 bytes.
Database concepts 4
DECIMAL: Stores exact numeric values (fixed-point numbers), often used
for financial data to avoid rounding issues.
This data type stores x number of characters in the string which has a fixed
length.
VARCHAR: Variable-length strings. Efficient for storing text that can vary in
size.
Example: TIME(14:23:00)
CHAR VARCHAR
It is used when columns are expected to be It is used when columns are expected to
of fixed length. vary considerably in size.
Database concepts 5
It takes memory space of 1 byte per
It takes memory space of 1 byte per
character , +2 byte to hold variable
character.
length information.
Constraints in SQL:
Constraints are set of rules created to apply data validations. They ensure
accuracy, correctness and reliability of data.
Constraint Description
Primary key The field or column which uniquely identifies a primary key.
SQL statements:
A keyword refers to an individual SQL element that has special meaning in SQL.
All statements in SQL terminates with a semicolon (;). Also SQL, is not case
sensitive.
Database concepts 6
CREATE: Creates a new table or database.
→ Create table is the most extensively used DDL command.
USE students;
SHOW TABLES;
SHOW DATABASES;
DESCRIBE : or desc is used to view the structure of the table along with
the name of the columns, data type of the columns and constraints
applied to the column.
DESCRIBE student;
Database concepts 7
ALTER TABLE <table name> ADD (<column name><data type>
[size] );
mysql> ALTER TABLE STUDENT ADD (MOBILE_NO INTEGER);
d) Renaming a column :
e) Removing a column :
Database concepts 8
Data Manipulation Language (DML): Used to manipulate (modify) data in
the database.
#updating to null
update student set brain=NULL where id=1;
Data Query Language (DQL): Used to retrieve data from the database.
Database concepts 9
SELECT: Fetches records from one or more tables.
Asterisks “*” means all. Select * means display all columns from a
relation
Select * from studenets;
DDL stands for data definition language DML stands for data manipulation language
examples : create, alter, drop etc: examples : insert into , update, delete etc:
SQL Operators:
Operation & function Description
Database concepts 10
Operation & function Description
Usage :
select * from students where name like “a%”;
select * from students where name like “a__d
”;
AGGREGATE FUNCTIONS
Database concepts 11
📖 NOTE:
Comments in SQL:
Single line: use # or - -
Multiline : use /* */
Illustration :
SELECT ROLLNO,NAME,STREAM
/* THIS STATEMENT SHALL DISPLAY ALL THE RECORDS OF THOSE ST
UDENTS WHO ARE IN SCIENCE AND
HAVE MARKS MORE THAN 75% */
FROM STUDENT #STUDENT TABLE IN USE
WHERE STREAM='SCIENCE' AND MARKS>75; --CONDITION FOR SELECT
ION
SQL Aliases:
SQL aliases are used to give a temporary name to a database table or column in
a table.
→ Column Aliases : gives column headings in the query result set.
→ Table Aliases : shorten table name by giving an easy alternate name, making
it easier to perform joins.
Syntax:
Select <column1> as <alias> from <table name> as <alias>;
Database concepts 12
If the alias name contains spaces, you must enclose it in quotations and the
alias is only valid within the scope of the SQL statement.
📖 SORTING IN SQL:
Can be done by using the order by clause.
select * from student order by roll <asc/desc>;
Default is ascending.
Group By:
Group by clause can be used in a select statement to collect data across
multiple records and group the results by one or more columns.
Usage:
select name, stream from student where marks>90 group by stream;
📖 Having clause :
It is used in combination with the group by clause and it is used to
allow aggregate functions to be used along with the specified
condition.
Where vs Having :
Database concepts 13
SQL Joins
Cartesian Product / Cross Product : It is a binary product and is denoted by (x).
Degree of the two relations is the sum of the degrees of two relations on which
it was performed.
Number of tuples in the new relation is the product of the cardinality of two
relations on which it was performed.
Syntax :
Equi join : it is a simple SQL join which uses the equal to sign as a comparison
operator for defining a relationship between the two tables on the basis of a
common field, i.e primary and foreign key.
Syntax :
Natural Join : It is similiar to Equi join expect that duplicate columns are
eliminated in Natural Join that would otherwise appear in Equi Join. In this the
join condition is not required and it automatically joins based on the common
value.
Syntax :
Database concepts 14
Database concepts 15