0% found this document useful (0 votes)
5 views10 pages

Understanding Databases and SQL Concepts

The document provides an overview of databases, database management systems (DBMS), and relational database management systems (RDBMS), including their structures, types, and functions. It explains SQL (Structured Query Language) and its sub-languages for data manipulation, definition, and querying, along with various SQL commands, constraints, and operations. Additionally, it covers concepts like normalization, joins, subqueries, and the advantages of indexing in databases.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Understanding Databases and SQL Concepts

The document provides an overview of databases, database management systems (DBMS), and relational database management systems (RDBMS), including their structures, types, and functions. It explains SQL (Structured Query Language) and its sub-languages for data manipulation, definition, and querying, along with various SQL commands, constraints, and operations. Additionally, it covers concepts like normalization, joins, subqueries, and the advantages of indexing in databases.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

database:-

-------------

data:-
--------
information (It can be any information)
eg:-
car info , student info, passenger info...etc

database:-
--------------
collection of similar information.

eg:-
------
train database:-
-------------------
It will stroe information about statation, train,coach details.

company database:-
-----------------------
employee deatails.
department details.
.....

dbms:- database management system.


------
storing the data in the system.

adv:-
----
multi user login
back up the data,

RDBMS:- Relational Database Management System


---------
In RDBMS, the records are stored in the form of [Link] tables will have
some kind of relationship.

different types of relationship:-


---------------------------------------
1:1 --> student - studentdetails

1:m -> student - contactdetails

m:1 --> contactdeatils - student

m:m --> customer - product

some of the RDBMS software are,


[DATABASE SOFTWARE NAME]
oracle.
mysql.
sql server.
IBM db2.
postgres sql.
maria db.

what is SQL:-
----------------
->structure Query language.
->It is used to interact with database software, for inserting, updateing,
deleting,
reteriveing the data from database

sql vs mysql
------------------- - ------------------------------
It is structured query language,
By using sql
we can interact with database
software.
eg:-intercat with mysql, oracle
sql server,....

It is a language It is a software
===================================================================================
what is SQL:-
----------------
->Structured Query Language

why we use sql:-


----------------
It is used to interact with database software, for performing
operation on database software like, insert, update, delete and reterive.

->Initially it was called as SEQUEL (structured english query language).


->Learning sql is easy, It learning like a english language.
->It is a command based language
eg:-
create
delete
drop
insert

every command has meaning to the compiler.

english:- sql command


---------- ------------
go insert
come update
sit delete
play

===================================================================================
==
SQL sub language:-
---------------------
DDL:- Data definintion language.
DML:- Data Manipulation language.
DQL:- Data Query language.
TCL:- Transcation Control language
DCL:- Data control language.
[Link]:- Data definintion language.
----------------------------------------
It is used for table structure

[Link]

d-drop ---> it will delete structure + record


r-rename ---> It will rename table name
c-create ---> It is used to create a new table
a-alter ---> It has used to modify table structure,
alter add --> It is used to add a new column
alter drop --> it is used to drop the column
alter rename --> It is used to rename the column name
alter modify --> It is used modify datatype / size.

t-truncate --> delete all the records , but table structure will remain.

[Link] Data manipulation language.

It is used for inserting , updating and deleteing the data into table.

insert
update
delete

3. DQL Data Query Language


reteriving the data from the table.

select

sql operators
sql clause
where
order by
group by
having
sql functions
sql joins
sql sub query

4. Transcation Control language.


commit :- on successfull transaction, it will do commit
rollback :-failed transaction will be rollback.
savepoint :- rollback back to some state. (instead of going to previous
commit)

transaction:-
-----------------
+ and -

sender receiver
---------- -----------
balance:40000 balance: 15000

-10000 + 10000
------- --------
30000 250000
------- --------
5. DCL data controll language:-
----------------------------------
Grant :- giving the permission.
Revoke :- taking out the permission.

===================================================================================
==
installation video:-
------------------------

[Link]

-------------------------------------------------------------------
practicle:-
-----------
database creation:-
---------------------
create database database_name

inside database, It will have collection of table.

========================================================================
open sql command line:-
---------------------------
window search -> mysql command line client
enter the password : your server password( during installation
time we have given)

mysql datatype:-

mysql operators:-
-------------------
[Link] operator:-
+
-
*
/

[Link] operator/comparison operator


>
<
>=
<=
==
!= <>

[Link] operator
and
or
not
in (subqueries)
any (subqueries)
all (subqueries)
exists
between
like
SQl Clauses:-
----------------
where
order by
group by
having

where clauses:-
---------------
-> where clauses is used to filter the record based on the condition.
condition --> (it can be one or more condition)

order by clause:-
------------------
-> by using order by clause, we can able to sort the records in ascending or
descending
order based on given column condition.

by default it will sort in ascending order.

asc --> ascending


desc --> descending

Group By Clauses:- (prerequisite --> sql aggregate function)


-------------------
-> It is used to group the similar records into one,
-> when ever we use group by, select only that column which we are doing group.
-> when we are doing group by clauses, it is necessary to use some aggegrate
function.

Having clauses:-
-------------------
-> It is used to filter the group by result.

===================================================================================
====
Sql functions:-
-------------------
It is used to perform some operations.
In Sql We have 2 types of functions.
1. scalar function.
2. aggregate function.

scalar function:- (for each row it will given output)


-----------------
It will take one row as input and given one row as a output

Aggregate function:- (for multiple row it will give single output)


---------------------
It will take one or more row as a input and gives single row as a
output.
===================================================================================
=====

DDL command:-
------------------
It is used for table structure.
[Link]

d-drop.
r-rename.
c- create.
a-alter.
alter add
alter drop
alter rename
alter modify

t-truncate.

difference between truncate, drop and delete.

drop--> It will delete structure + records


truncate -> delete all the records
It will delete records , but table structure remain
delete -> delete one or more records / all records
we can apply where clause, based on condition we can delete.
==================================================================================
SQL constraints:-
------------------ (rules given to the table)
giving condition to the table.
(or)
applying the condition to the table.

---> different types of constraints.


1. primary key
2. not null
3. unique key
4. check
5. default
6. foreign key

two ways to apply the constraint:-


-------------------------
1. table level constraint.
2. column level constraint.

table level constraint:-


------------------------
applying the constraint after declaring all the column name.

column level constraint:-


----------------------------
applying the constraint immediately after declaring the column name.

-----------------------------------------------------------------------------------
-
normilization in sql:- madhulate and akshya
different types of key in sql: durga and trupti

-----------------------------------------------------------------------------------
-
primary key constarint:- (id)
-------------
->It will uniquely identifies the record.
->It cannot be null,
->a table can have only one primary key.
-> It will not allow duplicate.

not null constarint:-


------------
The value of the column cannot be null, we need to pass some values.

unique key constarint:- (mobileno, email)


------------
->It uniquely identifies the record.
-> a table can have one or more unique key column.
->It will allow one null values.

default constarint:-
-------------------
if will take default values to the column, when value is not passed.

check constarint:-
-------------------
check(age>18)
it will check the value, before inserting into the table.
only valid condition value will be inserted.

foreign key constraint:-


-------------------------
It is used to make relationship with other tables.

=============================================================================
task:-
---------
1. write a query to find all the employee whose salary is > 50000.

ans:- select * from employee where salary > 50000.

2. write a query to find all the employee whose salary is greather than sachin
salary.
ans:-
------
here we dont know sachin salary, to find sachin salary we need to find
sub query.

===========================================================================
subquery:-
-----------
query inside another query
(or)
nested query.

when to use subquery..? / why we want sub query..?


-----------------------------------------------------
write a query to find all the employee whose salary is greather than sachin
salary.

outer query ( ------------------> second execution


inner query() --------------> first execution
)

first inner query will execute, the output of inner query is used by
outer query.

types of subquery:-
--------------------
2 types of sub query.
1. single row sub query
2. multi row sub query.

1. single row sub query:-


-------------------------
the inner query returns single row as a output.

2. multi row sub query:-


--------------------------
the inner query returns mutiple row as a output.(one or more).
==================================================================================
salary => 10,20,30,40,50,60,70,80

in, any , all,


wehere salary = 50 ===> 50
where salary in (30,50, 60) ===> 30,50,60 (equal to)

where salary > any(30,40,50) ===>40,50,60,70,80


(should be > any one one value)

where salary > all(30,40,50) ==> 60,70, 80.

===========================================================================
operator cont..

between :- for range for value.

like:- It is used for pattern search.


It has 2 operator,
_ (underscore) --> represent a single character
% (percentage) --> represent zero, one or more charcater.

sql joins:-
----------------
by using joins, we can reterive the data from different tables.

types of joins:-
-------------------
[Link] join
2. outer join:-
---------------
left outer join.
right outer join.
full outer join.

3. cross join: -
cartesian product

===============================================================
Sql set operator:-
------------------------
union
union all
interset
minus
===============================================================
sql functions:-
----------------
2 types of functions,
1. aggregate function
2. scalar function.

aggregate functions:-
----------------------
It gives single row as a output.
eg:-
----
avg,sum,count,min,max,

scalar function:-
----------------
for each row, It gives the output.

3 types:-
----------
1. character function , length, upper, lower
2. numeric function , round, trunc,...
3. date function ,adddate, subdate,....

DDL:- table structure,


DML:- manipulation,
DQL:- reterive,
TCL:- transaction

TCL:- Transaction Control Language.


------
tranfer fund:-
2 queries will fire
-------> 2 update queries will fire,
sender ---> minus
recerive ---> plus,
if both executes, then it is a successful transaction.

commit:- commit means, permanent saved


rollback:-if transaction fails, we can rollback.
savepoint:-instead of going to last commited state,we can go temperary
savepoint.
SQL View:-
----------
It is like a virtual table,
when to use view,
----> when we have some complex queries, then we can use view.
----> when we want to hide some fields from the table to the end user.

syntax:-
--------
create view viewname
as
queries (complex queries).

SQL Index:-
-------------
-> index is used, increase the efficency of the search.

eg:-
-----
text book.

index page:-
---------------
[Link] chapter pageNo
1 java 1
2 oops 9
3 sql 23
4 python 50
(searching will improve, It becomes faster).

keys:-
------
primary key
unique key
by default it has a index.

adv of index:-
---------------
->It improves the searching,
->by default all primary key and unique key has a index.
=================================================================================

Common questions

Powered by AI

SQL transactions refer to a sequence of operations performed as a single logical unit of work. Atomicity is a key concept in transactions that ensures that each transaction is fully completed or not done at all. This all-or-nothing approach guarantees database reliability by preventing partial updates that could leave the database in an inconsistent state. For instance, during a fund transfer between accounts, both debit and credit operations must succeed together to maintain accurate account balances. If one operation fails, atomicity will roll back all operations to the state before the transaction began, ensuring that no changes are applied partially .

SQL's Data Definition Language (DDL) and Data Manipulation Language (DML) serve different purposes. DDL is utilized for defining and modifying the database structure. It includes commands like CREATE, ALTER, DROP, and TRUNCATE, which are used to create and modify tables and schemas. On the other hand, DML is used for manipulating data within those tables, encompassing commands such as INSERT, UPDATE, and DELETE. DML allows users to add, modify, and remove data from the database, whereas DDL changes the structure that hosts this data .

SQL subqueries are essential in data retrieval as they allow for complex queries by using the result of one query inside another, known as the outer query. Single-row subqueries return exactly one row of results to be used by the outer query, which is ideal for operations expecting a singular value, such as comparing a single salary. Multi-row subqueries, however, can return multiple rows and are used in situations where lists of values are compared, such as retrieving all employees earning more than a specified employee's salary .

SQL operators such as "LIKE" and "IN" significantly enhance query versatility by providing pattern matching and set-based searching capabilities. The "LIKE" operator allows searches for patterns within fields using wildcard characters like '%' for multiple characters or '_' for a single character, enabling flexible string matching such as finding all phone numbers starting with '(555)'. The "IN" operator simplifies queries that involve comparing a column to a set of specified values, like retrieving records where a status might be one among 'active', 'pending', or 'closed', reducing complexity in writing multiple OR conditions .

Normalization improves data integrity and efficiency in a SQL database by organizing data in such a way that reduces redundancy and dependency. It involves dividing a database into two or more tables and defining relationships between the tables, making data storage more efficient and consistent. For example, removing duplicated information and ensuring that each table deals with a single subject matter avoids update anomalies and facilitates easier maintenance and scalability of the database .

SQL joins are significant in database operations as they allow for the retrieval and combination of data across multiple tables, mimicking the way data is often analyzed and reported. Examples include INNER JOINs, which return rows when there is a match in both tables; LEFT OUTER JOINs, which return all rows from the left table and matched rows from the right table; RIGHT OUTER JOINs, which do the opposite; FULL OUTER JOINs, which return all rows when there is a match in either table; and CROSS JOINs, which return the Cartesian product of the two tables .

SQL constraints enhance data integrity by imposing rules on the data entering the database, thus ensuring consistent, valid, and reliable data. For example, a primary key constraint uniquely identifies each record and prevents duplicate entries. A foreign key constraint ensures referential integrity by linking rows in different tables, while a not null constraint enforces that a column cannot contain null values, ensuring that essential data is always populated. Unique constraints ensure no duplicate values are entered in specific columns, and check constraints allow only values that meet a specified condition. These constraints collectively maintain a robust dataset, preventing anomalies and errors .

Transaction control commands like COMMIT, ROLLBACK, and SAVEPOINT are critical for maintaining data integrity in SQL databases by managing how transactions are executed. COMMIT saves all changes made by the transaction to the database, making them permanent. ROLLBACK, on the other hand, undoes changes made during the transaction, reverting the database to its previous consistent state. SAVEPOINT allows a transaction to set intermediate points within it, so subsequent ROLLBACK commands can undo changes up to the savepoint without affecting preceding commands. These controls ensure that partial or erroneous transactions do not compromise data integrity .

The primary advantages of using a Relational Database Management System (RDBMS) over a simple database system include the ability to store data in structured tables with defined relationships, which allows for more organized and efficient data management. RDBMS supports complex queries using SQL for data manipulation and retrieval, ensuring data integrity and reducing redundancy through normalization. Additionally, RDBMS systems enable multi-user access and data backup, improving data security and availability .

Using a SQL view for handling complex queries provides several benefits, including simplifying query execution by encapsulating complex logic in an easily accessible manner and improving data security by allowing users to access data through a predefined lens without exposing underlying table structures. However, potential drawbacks include performance overhead, as views can sometimes be less efficient than directly querying tables, and the added complexity in maintenance, as any change in the underlying tables necessitates updating the views accordingly .

You might also like