Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DBMS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Database Management System

Informatics Practices (065)

Database Managements
System

As per CBSE Syllabus 2023-24

8460831302 Page 1
Database Management System

Data:-Data is row figure of fact.


e.g:-1

information:-Information is collection of data or processed data.


e.g:-1,"shree",11,"sci",99

Database:- A database is a collection of interrelated data stored together.


so.g:-School is database which may store student,teacher,class..etc realted
information.

DBMS:- A DBMS(Database Management System) is a software system which


designed to maintain a database and provide data Management services.

Advantages of DBMS.
1.Reduce data Redundancy
2.Data consistency
3.Data sharing
4.Database enforce standards
5.Data security
6.Data independance

SQL Terminology:-
Table:-SQL table is collection of data which is organized in terms of rows and
columns. In DBMS, the table is known as relation or entity.

Field:-A Field is an element in which one piece of information is stored. In


DBMS, It is also called column or attribute.
Record:- Record is collection of items or data organized within a table.In
DBMS, It is also called row or tuple.
Degree:- Number of columns in a table, is called degree.
Cardinality:- Number of rows in a table, is called cardinality.

8460831302 Page 2
Database Management System

Table:-Employee. Entity/Relation

Keys database:-
There are following keys in DBMS.
1.Primary key:- A primary key is a set of one or more attribute(column)
that can uniquely identify row(record/tuple) within the relation.E.g:-Emp_id
2.Candidate key:- A candidate key is combination of one of more
attribute which include primary key.E.g:-Emp_id+Ename
3.Alternate key:- A alternate key is combination of one of more
attribute which does n't include primary key. E.g:-ename+post
4.Foreign key:-A foreign key is attribute which values are derived from
other table.E.g:-Emp_id is primary key for employee table but Emp_id is foreign
key for department table.
SQL Constraints:-A database constraints is set of rules that define valid
data.DBMS ensure that all constraints satisfy before inserting new data.
There are following Constraints in MySQL.
1.Primary key
2.Unique:-It allows unique values.
3.Not NULL:-It didn't allow to left blank.
4.Check:-It will check conditions. such as marks>0 and marks<100
8460831302 Page 3
Database Management System

5.Default:-Specify the default value such as city's default value is surat


6.Foreign key

SQL:-
 SQL stands for Structured Query Language
 SQL lets you access and manipulate databases
 SQL can execute queries against a database
 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create views in a database

Classification of SQL Statements:-


o SQL commands are instructions. It is used to communicate with the database.
It is also used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the
table, modify the table.

1. Data Definition Language (DDL)


o DDL changes the structure of the table like creating a table, deleting a table,
altering a table, etc.
o All the command of DDL are auto-committed that means it permanently save
all the changes in the database.

Here are some commands that come under DDL:

o CREATE
o ALTER
o DROP
8460831302 Page 4
Database Management System

o TRUNCATE

2. Data Manipulation Language


o DML commands are used to modify the database. It is responsible for all form
of changes in the database.
o The command of DML is not auto-committed that means it can't permanently
save all the changes in the database. They can be rollback.

Here are some commands that come under DML:

o INSERT
o UPDATE
o DELETE

3. Transaction Control Language


TCL commands can only use with DML commands like INSERT, DELETE and
UPDATE only.

These operations are automatically committed in the database that's why they cannot
be used while creating tables or dropping them.

Here are some commands that come under TCL:

o COMMIT
o ROLLBACK
o SAVEPOINT.

Data Type:-
There are following MySQl data type.
1. Int:-Stores integer numbers. E.g :-rollno can store as int
2. Decimal:-store decimal point numbers. E.g:- price can store as decimal
3. Char:-store characters (also stores number and special charcaters). It is
static data type.e.g:-name

8460831302 Page 5
Database Management System

4. Varchar:-store characters (also stores number and special charcaters). It is


dynamic data type.e.g:-name
5. Date:-stores date e.g:-Date_of_birth

SQL Commands:-
1.Create database:-It is used to create new database.

Syntax:-create database <databasename>;

E.g:- create database school;

2.use:-It is used to change currently working database.

Syntax:-use <databasename>;

E.g:- use school;

3.Show Databases:- It will display all databases available in MySQL.

Syntax:- show databases;

E.g:-show databases;

4.Create table:-

Student

Fieldname Datatype(size) Constraints


Grno Int(4) Primary key
Name Varchar(25) Not null
class Varchar(20)
M1 Int(3)
Syntax:-create table <tablename>(<field1> datatype(<size>) constraints, <field1>
datatype(<size>) constraints);

8460831302 Page 6
Database Management System

Create table student(grno int(4) primary key,name varchar(25)not null,class


varchar(20),m1 int(3));

Try Yourself:-
Employee

Fieldname Datatype(size) Constraints


EmpID Int(4) Primary key
Ename Varchar(30)
Salary Decimal(11,2)
Phone Decimal(10)
Address Varchar(20)
Designation Varchar(25)
Department Varchar(20)
5.Show Tables:- It will display all Tables available in current database.

Syntax:- show tables;

E.g:-show tables;

6.insert into:-It is used to add new record in table.

Syntax(1):- insert into <tablename>(<field1>,<field2>)


values(<value1>,<value2>);

E.g:-insert into student(grno,name) values (1,”shree”);

Note:- It will add only given field.

Syntax(2):- insert into <tablename> values(<value1>,<value2>);

E.g:-insert into student values (1,”shree”,”diploma”,90);

8460831302 Page 7
Database Management System

Note:- It will add all fields of table.

7.select:- It will display records from table.

Syntax(1):- select * from <tablename>;

E.g:-select * from student;

Note:- It will display all fields.

Syntax(2):- select <field1>,<field2> from <tablename>;

E.g:-select grno,name from student;

Note:- It will display given fields.

7.1 select with where clause:-

Syntax(1):- select * from <tablename> where <fieldname>=value;

E.g:-select * from student where class=”11 Sci”;

7.2where with logical operators:-This command is used to search data based on


two conditions.
You can use And and Or logical operators with where.
And will display record which match both the condition.
or will display record which match any one condition.
Syntax:- select * from <tablename> where <field>=<value> and/or
<field>=<value>;
e.g(1):-select * from students where std =11 or std=12;
(2) select * from students where std=11 and division=”com”;

7.3 Where with in keyword:-In keyword is used to specify multiple conditions.


You can make list and search values in it.
Syntax:- select * from <tablename> where in<field>(<value1>,<value2>...);
e.g:-select * from students where std in (10,11,12);

7.4 where with between keyword:-Between is used to specify the range. Lower

8460831302 Page 8
Database Management System

limit and upper limit of range specify by and keyword.


Syntax:- select * from <tablename> where <field>between <lowerlimit> and
<upperlimit>;
e.g:-select * from students where ipmark between 80 and 90;

7.5 Simple calculation:-you can perform simple calculation using mathematic


operators.
Syntax:- select <number><operator><number>;
e.g:-select 25+25;

7.6 where with like keyword:-Like keyword is use to do pattern matching.


MySQL use two wildcard characters for pattern matching.
1.percent(%):- Matches any number of characters.
2.underscore(_):-Matches one character only.
Suppose you want
1.Name start with character ”A”
2.End with character ”A”
3.In between character ”A”
4.Particular character
Then you can use pattern matching
Syntax:- select * from <tablename> where <field> like<pattern>;
e.g:-select * from students where name like”s%”;

7.7 where with is keyword:-is keyword is used to search for null data.
Syntax:- select * from <tablename> where <field> is NULL;
e.g:-select * from students where name is NULL;

7.8 Distinct keyword:- It is used to elimate duplicate rows from result.


Syntax:- select distinct(<field>) from <tablename>;
e.g:-select distinct(std) from students;

7.9 Order by clause:- Order by clause is used to display data in order(ascending


or descending).
By deafult, MySQL arrange data in ascending order. For Descending order you
need to specify DESC word.
Syntax:- select * from <tablename> order by <field>;
e.g(1):-select * from students order by name;
(2) select * from students order by name desc;
8.Desc:-It is used to display structure of table.
8460831302 Page 9
Database Management System

Syntax:- desc <tablename>;


e.g:-desc student;

practice question
 Display all student those who are living in “Nana Varchcha” area
 Display all details of student named “Shree”
 Display all details of grno 2
 Display all students who got more than 90 marks in IP
 Display all students who got less than 20 marks in IP
 Display all science students
 Display all 11 std students

Create following table “Teacher” in MySQL.


Fieldname Datatype(Size) Constraints
Tno Int(3) Primary key
Tname Varchar(10)
Subject Varchar(20)
Std Int(2)
Qualification Varchar(20)
Salary Decimal(11,2)
 Insert at least 10 records in above table.
 Display all data from teacher table.
 Display teacher who are teching Computer subject
 Display teacher who are getting more than 20000 salary
 Display all MCA graduate teacher
Practice Time:-
Create following table “Movies” in MySQL.
Fieldname Datatype(Size) Constraints
MovieNo Int(3) Primary key
MovieName Varchar(20)
TicketPrice Decimal(8,2)
TypeOfMovie Varchar(20)
Actor1 Varchar(20)
Actor2 Varchar(20)
ProductionName Varchar(20)
Language Varchar(20)
8460831302 Page 10
Database Management System

Insert at least 7 record in above table and perform following queries.


1. Display all movies which produced in Hindi language only.
2. Display all movies whose ticket price in range of 100 to 200
3. Display available types of movies
4. Display all movies whose starting charcter is “H”
5. Display all movies of drama,horror, fraction type
6. Display all movie produced by “RRR”
7. Display discount(less 50 rs) price of all movies
8. Display all movies which don’t have actor

Updating Row of table:-


To update row of the table, you can use update statement.
Syntax:- update <tablename> set <fieldname>=<value> where
<fieldname>=<value>;
e.g:-update student set address=”Katargam” where grno=5;
Note:-Writing where is compulsory. If you don’t specify where cause it will
update all rows of the table.

deleting Row of table:-


To delete row of the table, you can use delete statement.
Syntax:- delete from <tablename> where <fieldname>=<value>;
e.g:-delete from student where grno=5;
Note:-Writing where is compulsory. If you don’t specify where cause it will
delete all rows of the table

Add/update/delete column in table:-


Alter statement is used to add/update and delete column in table.

Add new column in table:-


Alter with add statement is add new column in table.
Syntax:- alter table <tablename> add <columnname> datatype(size);
e.g:-alter table student add city varchar(20);

8460831302 Page 11
Database Management System

update existing column in table:-


Alter with modify statement is add new column in table.
Syntax:- alter table <tablename> modify <columnname> datatype(size);
e.g:-alter table student modify address varchar(50);

deleting existing column in table:-


Alter with drop statement is add new column in table.
Syntax:- alter table <tablename> drop <columnname> ;
e.g:-alter table student drop address;

deleting existing table:-


Drop statement is used to delete table from database.
Syntax:- drop table <tablename> ;
e.g:-drop table student;

deleting existing database:-


Drop statement is used to delete database.
Syntax:- drop database <database> ;
e.g:-drop database shk;
reference integrity:-
You can divided table in two parts to reduce data redundancy. (duplication)

Student
Fieldname Datatype(Size) Constraints
Grno Int(3) Primary key
Name Varchar(25) Not null
Std Int(2)
Division Varchar(10)
Address Varchar(25)
Create table student(grno int(3)primary key,name varchar(25),std int(2),division
varchar(10),address varchar(25));
Marks
Fieldname Datatype(Size) Constraints

8460831302 Page 12
Database Management System

Grno Int(3) Foreign key


Eng Int(3)
IP Int(3)
Phy Int(3)
Chem Int(3)
Maths Int(3)

Create table marks(grno int(3),eng int(3),ip int(3),phy int(3),chem int(3),maths


int(3),foreign key(grno) references student(grno));
 Student table is master/super or parent tanle.
 Marks table is detail/sub or child table.
 Grno in student’s table is primary key but grno in mark’s table is foreign
key.
Note:-
First create Master table (Student table), then create detail table (marks
table).
First enter data in master table and then enter in detail table otherwise
MySQL will give error.
If you want to delete record, first delete from master table.
Equi joins
When you join two table based on same data (of primary key and foreign key) ,it is
called equi join.
E.g:-
Select student.grno,name,std,division,eng,ip,phy,chem,math from student,marks
where student.grno=marks.grno;
If you want to search record based on condition, you can use logical operator and
to join condition.
Suppose you want to display student whose are fail in maths,
Select student.grno,name,std,division,eng,ip,phy,chem,math from student,marks
where student.grno=marks.grno and math<33;

Non-Equi joins or Cartesian product


When you join one record of first table with all other record of second table,
second record of first table with all other records in second table and so on, it is
called Cartesian product.
Select student.grno,name,std,division,eng,ip,phy,chem,math from student,marks;

8460831302 Page 13
Database Management System

Group by
When you want to create group in table then search data, group by is used.
Suppose, display student areawise, you can write
Select * from student group by address;
Suppose you want to display total no of student in each std.
Select count(*) from student group by std;

Having
You cannot not used where clause with group by. Having can be used in place of
where to search data based on condition.
Suppose you want to display std where more than 50students are studying..
Select count(*) from student group by std having count(*)>50;

8460831302 Page 14

You might also like