DBNotes Updated
DBNotes Updated
What is Database?
DB is collection of data in a format that can be easily accessed (digital).
A Software application used to manage our DB is called DBMS (database management
system).
Type of DataBase –
1. Relational (RDMS) – Data stored in table and we use sql to interact. Some ex. Of
relational db are – MySQL, ORACLE, Microsoft SQL SERVER, and PostgreSQL.
What is SQL?
SQL (Structured Query Language) is programming language use to interact with relational
database.
SEQUEL and SQL both are same.
It is use to perform CRUD operation.
C - Create
R – Read
U – Update
D – Delete
1
Queries (queries are not case sensitive) -
1. Create Database –
2. Show Databases-
Syntax – SHOW DATABASES;
3. Drop Database –
Syntax – DROP DATABASE db_name;
2
This query is to drop/delete the existing database present in MySQL.
Here from show database query we have checked whether the database we wanted
To drop is dropped or not.
4. Use Database -
Syntax – USE db_name;
5. Create Table –
Syntax – CREATE TABLE table_name (
Column_name1 datatype constraint,
Column_name2 datatype constraint,
Column_name3 datatype constraint
);
3
This query is used to create table under the chosen database.
In this syntax we use data types and constraint to create table.
About constraint we will discuss later in detail.
6. Show fields–
Syntax – SHOW fields FROM table_name;
SHOW columns FROM table_name;
This query is used to see all the field, data types, and the constraints.
4
Here we have inserted to 2 column using this query. Remember Column
constraint containing Primary key should be different to avoid ERROR.
8. Show table –
Syntax –SELECT * FROM table_name;
This query is to get all the row and column from the table.
‘*’ means all in SELECT related queries.
1. DDL (Data Definition Language): Create, alter, rename, truncate & drop.
2. DQL (Data Query Language) : Select
3. DML (Data Manipulation Language) : , insert, update & delete
4. DCL (Data Control Language): grant & revoke permission to users
5. TCL (Transaction Control Language): start transaction , commit, rollback
1. If Not Exists-
Syntax – CREATE DATABASE IF NOT EXISTS db_name;
2. If Exists-
Syntax – DROP DATABASE IF EXISTS db_name;
This used to avoid error in MySQL but it shows warning as you can see in above image
This is count as good practice in MySQL.
6
Keys:
1. Primary keys – It is a column or set of column in a table that identifies each row.(a
unique id). There is only 1 Primary key and it should be NOT null.
2. Foreign keys- A foreign key is a column or set of columns in a table that refers to the
primary key in different table. There can be multiple FKs and can have duplicate and
null values. (iska use samj aur sikh nhi paya)
Constraints:
7
3. PRIMARY KEY: makes a column unique and not null used only for one.
4. FOREIGN KEY: prevent actions that would destroy links between tables.
5. DEFAULT: sets the default value of a column.
8
As you can see we use check as a constrainst in age and it act as a limit to enter data in table.
When i try to cross the limit it shows the error and didn’t insert the values.
SELECT COMMAND :
9
DISTINCT is used to select only the unique ones.
Syntax: SELECT DISTINCT col_name FROM table_name;
10
AND & OR Operator : AND is used when both the condition satisfy. OR is used when any
one of the conditions we want to get satisfy.
BETWEEN,IN &NOT: BETWEEN selects for a given range.IN matches any value in the list
If not return nulls .NOT to negate the given condition.
11
LIMIT CLAUSE:
Use to limit the value to select.We can use the WHERE condition in it as well.
ORDER BY CLAUSE:
AGGREGATE FUNCTIONS :
12
MAX Func. Return max value.
GROUP By Clause :
It collects data from multiple records and groups that result by one or more
column.Generally we use group by with some aggregation func.
Having Clause:
Similar to WHERE i.e applies some condition on row. Used when we want to apply
any cond. After grouping.
13
.General Order :
14
As it deleted a row you can see the id 3 is deleted.
CASCADING IN FOREIGN KEY: Basically means it we update or delete from one table
(parent table) it should reflect in other table (child table) also.
Like you can see we use cascade in FK. Let represent both the tables –
15
Now we delete the deptId which is equal to 1 and that will affect the other table.
Add Column :
16
Here we have added an extra Column for the table called stu_detail.
Drop Column:
Rename Table :
17
Basically renamed the table.
Modify Column :
18
12.TRUNCATE QUERY(table related):
Truncate query basically delete all the data present in the table.
Diff. between Drop and truncate Query is Drop deletes the table but in truncate
the table exist but all the value flushes out.
The values of the table called student deleted. But the table still exists.
19
Joins in SQL
Joins is used to combine rows from two or more tables, based on a related column between
them.
Venn diagram:
Inner Join: Return records that have matching value in both tables.
Now, we will create a second table called courses have a common column student_id.
20
Now, we will perform Inner Join with this tables.
Alias is used for alternate name in MySQL. Use in place where we have large table name
To shorten its name. Remember both table names are correct you can use any one of them.
Left Join: return all records from the left table, and the matches records from the
right table.
21
Just Remember the Sequence of table in this join method because the data of tableA
with appear fully with some common/overlap data of tableB.
We have created the table you have seen in the above join method .So, we will
directly perform the query.
co
The result of tables is shown above you can see the table students represent full data
but in courses the overlapped value appear and in case of absent value printed as
NULL.
Right Join: returns all records from the right table, and the matched records from the
left table.
The resulted table is shown above we can see that the course/right side table appear
represent full data and in student table the overlapped data is printed and apart from that
prints NULL.
22
Full Join: returns all records when there is a match in either left or right table.
Syntax:
SELECT * FROM tableA
LEFT JOIN tableB
LEFT JOIN
ON tableA.Col_name=tableB.Col_name
UNION
UNION
RIGHT JOIN
SELECT * FROM tableA
RIGHT JOIN tableB
ON tableA.Col_name=tableB.Col_name
MySQL don’t have full join so we perform using union. Oracle mei hota hai.
Self-Join meko sammj nhi aaya but it exists that is why I mentioned here.
To use it:
Every SELECT should have same no. of columns.
Columns must have similar data types.
Columns in every SELECT should be in same order.
23
Syntax:
SELECT column(s) FROM tableA
UNION
SELECT column(s) FROM tableB;
T
The result print all the data from both the table.
SELECT
Query
FROM
WHERE (Sub Query) Sub Query
24
Let’s perform a task to know better about sub query.
Like in a classroom we have to give the name of student who scored more than avg.. . So our
query will be:
Suppose a condt. Arrive where we the student score got a mistaken marks given by the
teacher then the overall avg. will be disturb. So, we have to create a dynamic process.
That firstly takeout the avg. of class then perform the selection.
MySQL Views
Views can be basically understand as show up data. For ex. There are multiple data in
database but we show the only data that is required.
25
Syntax: CREATE VIEW view1
SELECT column(s) FROM table_name;
View is the virtual form of table. But table and its data operations are real.
THANK YOU
26