0% found this document useful (0 votes)
5 views

SQL Notes

sql notes computer science

Uploaded by

deevasharma24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL Notes

sql notes computer science

Uploaded by

deevasharma24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

06/05/2024, 21:17 OneNote

RD & SQL
Monday, May 06, 2024 5:28 PM

Database
• Organized collection of data about an entity or things
• It can be integrated as well as shared

(8 bits make a byte)

DBMS-database management system


• Electronic or computerized record keeping system
• It stores data in a manner which it makes it easier and highly
efficient to retrieve.
• Keeps info in integrated and summarized manner

Advantages:
1. Elimination of data redundancy.(duplication of data)
2. Sharing of data
3. Reduced programming effort
4. Data consistency
5. Database enforces standards
6. Improved data integrity
7. Privacy and security
8. Improved backup and recovery system

Types
1.Hierarchical method
Records are organized as trees. It represents a hierarchy of parent and
child segments.
Represents one-to-many(1:M) relationships b/w parent and child
segment.

• It is simple in operation and concept


• Promotes data sharing
• Promotes data integrity
• Efficient and can be naturally mapped for 1:M relationship
Limitations
• Suitable for hierarchical relationships only
• Implementation complex and difficult
• Dependent on parent node
• Change in structure require change in all the applications

2.Network data model


Network data models are a way of representing data that can be used for
organizing and managing complex relationships between various entities.
Many:many relationship

https://lotusvalleyne-my.sharepoint.com/personal/293_lotusvalleynoidaextension_com/_layouts/15/Doc.aspx?sourcedoc={7c93b9a5-8238-4408-a489-… 1/5
06/05/2024, 21:17 OneNote

Advantages
• Can handle more relationship types
• Data access is easy and more flexible
• it includes DDL data definition language as well as DML data
manipulation language
• it commodes better data integrity
Limitations
• Quite complicated system several numbers of links
• structural changes require changes to be made in all application
programs
• Extra memory is consumed for holding links
• High level language interface is required to interact with the database

3.relational data model


Data is organized in two-dimensional tables called relations. The data and
relations between them are organized into tables this model is simple and
has all the properties and capabilities required to process data with storage
efficiency
• It is represented as orderly arrangement of data into rows and
columns
• Each relation is represented as a table
• Column described in a table are the attributes
• Each row in a table represents a single entity
• all values in a relational are scaler
Advantages
• it provides structural independence by using independent tables
• Changes made in table structure do not affect the data access
• Represented in form of table so it is easy simple to understand
• Data organization and manipulation are easy flexible and faster
• Mathematical operations can be successfully carried out using RDBMS
Limitations
• RDBMS incurs hardware and system software overheads
• the size of a database may become very large
relational database
It is a type of database that stores and provides access of data points that
are related to one another, each row in the table is a record with a unique
ID called the key. The columns hold attributes of the entity and each record
usually has a value for each attribute.
Cardinality of relation-rows or tuples
Degree of relation-columns or attributes

Database keys
Primary key-set of one or more attributes/fields which uniquely identifies
a tuple/row in a table.
Candidate key-it refers to all the attributes in a relation that are candidates
or capable of becoming a primary key.
candidate key-primary key=alternate key
Alternate key-a candidate key that is not the primary key is called an
alternate key.
Foreign key-it is a non-key attribute whose value is derived from the
primary key of another table.
DDL Commands
1. Creating database
Create database<database_name>;
2. Opening database
USE database<database_name>;

https://lotusvalleyne-my.sharepoint.com/personal/293_lotusvalleynoidaextension_com/_layouts/15/Doc.aspx?sourcedoc={7c93b9a5-8238-4408-a489-… 2/5
06/05/2024, 21:17 OneNote
3. Removing database
Drop database<database_name>;
4. Create table
Create table<table_name>;
5. Viewing table
Show tables;
6. Viewing table structure
DESCRIBE <table_name>;
7. Alter table structure
Used for
-adding a column
-renaming existing column
-changing datatype and modifying size of column
-to remove or physically delete a column
(a)adding a column to an existing table
ALTER TABLE<table_name>ADD<column_name><data type>
[size]/default data;
(b)Modifying an existing column defination
Alter table<table_name>
Modify([column_name]<data type>);
(c)Renaming a column
Alter table<table_name>
Change<old-column-name><new-column-name>column_defination;
(e)Removing a column
Alter table<table_name>Drop<column name>
(f)Adding and deleting primary key constraints
Alter table employee add/drop primary key(Emp_ID);
8.Drop table command
Drop table<table_name>;

DML command
1.The insert into command is used to insert a new record/row/tuple in a
table.
(a)Inserting data(for all columns)into a table:
Insert into<table_name>values(value1,value2,value3...)
(b)Inserting data by specifying all the column names and associated values
into a table:
Insert into<table_name>(column1,column2,columnN,…)
Values(value1,value2,valueN,...);
(c)Inserting data into specific columns of a table:
Insert into<table_name>[(column1,column2,...columnN)]
Values[(value1,value2,...valueN)]
2.Modify data into table
Update<table_name>
SET<column1>=<value1>,<column2>=<value2>,…
Where<condition>;
3.Removing data from a table
Delete from<table_name>where<condition>;
-SQL Truncate statement(used to delete all the rows from the table and
free the space containing the table for reuse)
Truncate table<table_name>;
SQL SELECT Statement
1.Projection
Select <column-name1>,<column-name2> from <Table-name>;
2.Selection-WHERE clause
Select*From <table_name> where<conditions_to_satistfy>;
3.Reordering columns while displaying query result
Ex. Select Name,Roll no,DOB,Marks From Student;
4.Eliminating Duplicate/Redundant Data-DISTINCT clause
Select distinct<column-name> from <Table-name>;
SQL operators
Logical functions

https://lotusvalleyne-my.sharepoint.com/personal/293_lotusvalleynoidaextension_com/_layouts/15/Doc.aspx?sourcedoc={7c93b9a5-8238-4408-a489-… 3/5
06/05/2024, 21:17 OneNote
1.AND
2.OR
3.NOT
4.SPECIAL
a.Between...AND
Select<colounm name> from <tablename> where <columnname>
BETWEEN<value1>AND<value2>;
b.Conditions based on a List-IN
Select<colounm name> from <tablename> where <columnname> IN/NOT
IN(value1,value2,...);
c.Conditions based on Pattern-LIKE
Select<colounm name> from <tablename> where <columnname>
LIKE/NOT LIKE <pattern>;
%-matches any sting
_-matches any one char
SORTING in SQL-ORDER BY
It is used to sort the data in ascending and descending orderbased on one
or more columns.
For descending [DESC]
Select<column-list>FROM<Table-name>[WHERE<condition>] ORDER BY
<column_name>[ASC or DESC]
Where clause optional
Ex. Select rollno,name,marks from student ORDER BY Name;
Select rollno,name,marks from student ORDER BY marks DESC,
Name;
AGGREGATE FUNCTIONS
1.MAX()
2MIN()
3.SUM()
4.AVG()
5.COUNT()
GROUP BY
Select<colunm1,column2,column_n<aggregate_function(expression)>
FROM <tables>WHERE<conditions>GROUP BY<colunm1,column2,…
column_n>;
HAVING CLAUSE
Select<colunm1,column2,column_n<aggregate_function(expression)>
FROM <tables>WHERE<conditions>GROUP BY<colunm1,column2,…
column_n>HAVING[<condition1...condition_n>];
Having is only used with group by clause
SQL JOINS
Equi join
SELECT <column_list> FROM <table1, table2....> WHERE <table1.primary
key column> = <table2.foreign key column>;

https://lotusvalleyne-my.sharepoint.com/personal/293_lotusvalleynoidaextension_com/_layouts/15/Doc.aspx?sourcedoc={7c93b9a5-8238-4408-a489-… 4/5

You might also like