Database Management System
Database Management System
Record
A full row is called record, eg: BIKRAM 12 BAGLUNG SOS is a record in
table for Name Class District School. A record hold heterogeneous data,
it holds complete information about a person or item.
Field
The individual data entered in record is called field,eg;Bikram is a field or
12 is a field.It is one cell data of a row or record. A collection of Field
forms a record and A collection of records forms table.
PRIMARY KEY
Primary key is used to identify a record in a table.
In a data of students of table there may be many students of same name
so we cannot differentiate them on the basis of Name,if they are from
same family we cannot differentiate on the basis of their phone number,
May be we can differentiate on the basis of their roll no or email id.
So primary key is different for different data or different table.
Primary key cannot be Null and it cannot be same value on two different
records.
Another eg for primary key is account no in bank account, even though.
Account name can be same,date of joining is same account no is always
unique.
FOREIGN KEY
It is a column in RDBMS that creates links between tables. Generally
when a primary key of a table is used in another table it creates links
between tables and the primary key of 1st table on 2nd table is called
foreign key.
Eg: For a customer detail table in bank account no is primary key, but in
a transition table primary key is transition id but we use account no to
Know where is money transferred or from which account no transition is
made so account no acts as foreign key.Foreign key can be repeated as a
single account no can have multiple transition.
Foreign key helps to reduce redundancy as we don’t have to put all
details about customer instead we can put account no.
In order:
Status
Show databases;
Use db_name;
Show tables;
Describe table_name;
Select col_name from table_name;
Now use following command to create and update databases and also
use above command to see if happened or not
eg create database db_name; and do show databases;
Note: Even if u create a database u are still in previous database so after
creating database u have to use “use” command to use that database to
do something onto it.
CREATE DATABASE
DROP DATABASE
If you drop selected db and use status command,It will show current
database empty.
CREATE TABLE
CREATE TABLE TABLE_NAME
(
COL1_NAME DTYPE CONSTRAINT,
COL2_NAME DTYPE CONSTRAINT,
COL3_NAME DTYPE CONSTRAINT,
);
Eg:
CREATE TABLE EMPLOYE ( EID INT, NAME VARCHAR(255), AGE INT);
ALSO WE CAN DO AS :
CREATE TABLE DB_NAME.TABLE_NAME(COL1_NAME DTYPE
CONSTRAIN,
);
DELETE TABLE:
DROP TABLE TABLE_NAME;
EG: DROP TABLE EMPLOYE; (DELETES TABLE NAMED EMPLOYE)
SIZE:MAXIMUM NO OF DIGITS
UNDERSTANDING DATATYPES:
|| UNSIGNED INT is a variant of the INT data type that only allows non-
negative numbers. UNSIGNED Key is used as constrain.
NOT NULL:
By default a column can hold null value, not null constrain enforces a
column to not accept null value. So adding null value is not possible
when not null constraint is applied.
It can be applied while creating and through alter command.
But while applying not null through you must check the column where
not null constrain is applied don't contain null value.
SYNTAX:
CREATE TABLE TABLE_NAME(
COL1_NAME DATATYPE CONSTRAINT CONSTRAINT_NAME NOT NULL,
COL2_NAME DATATYPE CONSTRAINT CONSTRAINT_NAME NOT NULL
)
EG:
CREATE TABLE STUDENTS(
STD_ID INT CONSTRAINT CNAME NOT NULL,
STD_NAME VARCHAR CONSTRAINT CON_NAME NOT NULL);
);
EG:
CREATE TABLE STUDENTS(
STD_ID INT NOT NULL,
STD_NAME VARCHAR NOT NULL);
OR UNIQUE(COL1_NAME,COL2_NAME,…));
TO DELETE CONSTRAIN
ALTER TABLE TABLE_NAME
DROP INDEX CONTRAINT_NAME
NOTE:After applying unique constrain you can have multiple null value.
PRIMARY KEY:
It uniquely identifies each record in a table, field which is primary key
cannot contain null and must contain unique value. A table can have
only one primary key.But a primary key may be made up of combination
of more than one column.
Applying primary key on a table.
CREATE TABLE STUDENTS(
STD_ID VARCHAR(30) PRIMARY KEY,
NAME VARCHAR(30) NOT NULL,
CLASS INT,
ROLL_NO INT);
OR
CREATE TABLE STUDENTS(
STD_ID VARCHAR(30),
NAME VARCHAR(30) NOT NULL,
ROLL_NO INT,
PRIMARY KEY(STD_ID));
);
EG:STUDENTS TABLE
LETS HAVE A TABLE WITH :
+--------+---------+-------+---------+
| STD_ID | NAME | CLASS | ROLL_NO |
+--------+---------+-------+---------+
| ST1121 | David | 10 | NULL |
| ST1234 | Alice | 10 | 1|
| ST3141 | Eve | 11 | 4|
| ST5678 | Bob | 11 | 2|
| ST9101 | Charlie | 12 | 3|
+--------+---------+-------+---------+
Where STD_ID is primary key.
Now lets say a school did a programme those students also participated
and some data of programme is kept in table program.
STD_ID is primary key of table students which acts as foreign key here.
ENTERING RANDOM VALUE:
INSERT INTO program (PROJECT_ID, CONTRIBUTOR, EXACT_TIME)
VALUES (1, 'ST1121', '14:30:00');
CHECK CONSTRAINT:
It is use to limit values within certain range while entering data onto the
column.
SYNTAX:
CREATE TABLE TABLE_NAME(
COL1_NAME DTYPE,
….…………………………
CONSTRAINT C_NAME CHECK (condition) // check(condition)
);
Eg:
create table emp(
name varchar(30) not null,
age int,
roll_no int,
constraint cons check(age>20)); [But age can be Null]
ALTER TABLE TABLE_NAME
ADD CONSTRAINT CONS_NAME CHECK(CONDITION);
DEFAULT:
It provides a default value for a column when no value is specified during
an insert operation.In absence of default value null value is obtained.
Note: If you have Deleted Your Custom made default value, then there is
no default value. By Default Null is a Default value if u delete Your
Custom made default value Now Null won’t be default value.
To delete column
SELECT COMMAND:
1. Using % Wildcard
% means any no of any type of character so mysql will try infinite
combinations [4 letter word, 5 letter word ] and show results.
2. Using _ Wildcard
_ means a single character
Example: Find all employees whose names have exactly 4 characters and
start with "J":
SELECT *FROM employees Where name LIKE 'J___'; [4 “_” ]
Example: Find all employees whose names have "a" as the second
character:
SELECT *FROM products WHERE product_name LIKE '_a%';
Example: Find all employees whose names have 5 letters
SELECT * FROM products WHERE product_name like “_____”;
USE OF IN OPERATOR
It will print all rakesh because it will look for combination of name,age
that are same since there is no same combination of name and age it will
print all rakesh.
UPDATE COMMAND:
It is used to update records in a table.
UPDATE TABLE_NAME
SET COL1=VAL1,COL2=VAL2,……….
WHERE CONDITION;
Update command without where will change all the records.
To compare NULL value don’t use
Where price=NULL [wrong]
Instead do
Where price is NULL [correct]
DELETE COMMAND:
We cannot delete a field but can delete a record.
ORDER BY:
To sort data.
Null is the smallest value.
AGREGATE FUNCTIONS:
To perfrom basic mathematical calculation of column we use agregate
functions.
MIN().MAX(),COUNT(),SUM(),AVG(),STDDEV(),VARIANCE() etc are
agregate functions.
All ignore null except,
count() it count null if we want to see of whole table but it ignores null
for a coulmn.
STUDENTS TABLE
Teacher Table
Teacher_name Class Students Subject
Bishnu 10 Kiran Math
Bishnu 10 Kiran Math
Laxmi 10 Shyam Math
Kiran 9 Deepa Science
Sunita 10 Anil Math
Sunita 10 Niraj Math
Bishnu 10 Suman Math
Bishnu 10 Sujan Math
Student table
Name Balance Mother_Name Father_Name
Aarav 8765 Manju Hari
Anil 7890 Kumari Bikash
Arjun 5678 Laxmi Ram
Bibek 5674 Sunita Dilip
Dipesh 4563 Sarita Prakash
Kiran 2345 Shanti Dinesh
Niraj 6789 Gita Madan
Pratik 4321 Sita Shyam
Sujan 3456 Radha Gopal
Suman 1234 Mina Krishna
SQL ALIAS:
When we work with multiple table together it becomes confusing to add
different cols from different tables in a siingle query. So we use alias to
fix that.We give nickname to table.
For above student table:
select s.name,s.balance from students as s;
Note:Once the query is complete, the alias is no longer available for the
next query.
Teacher Table
Teacher Name Class Students Subject Salary
Laxmi 10 Shyam Math 1200
Kiran 9 Deepa Science 5500
Nabin 10 Kiran Math 9000
Rabin 10 Suman Math 9000
Sunita 10 Sujan Math 7000
Nirmal 10 Anil Math 7500
Apark 9 Pratik Science 2300
Students Table
Name Balance Mother's Name Father's Name
Aarav 8765 Manju Hari
Anil 7890 Kumari Bikash
Arjun 5678 Laxmi Ram
Bibek 5674 Sunita Dilip
Dipesh 4563 Sarita Prakash
Kiran 2345 Shanti Dinesh
Niraj 6789 Gita Madan
Pratik 4321 Sita Shyam
Sujan 3456 Radha Gopal
Suman 1234 Mina Krishna
select s.name from students as s where s.balance<(select t.salary from
teacher as t where s.name=t.students);
Name
Suman
Kiran
Sujan
Left join selects all from left table and only data that matches to left
table from right table.
Teacher Name Class Students Subject Salary Name Balance Mother's Name Father's Name
UNION:
It helps to merge two select statements are 2 or same tables but it will
be possible only when the order and type of data on 2 tables must be
same but name of columns can vary.
EG:
Table1: std_id,class,Course
Table2: teacher_id, class ,Course
These 2 table can apply union
But:
Table1: std_id,class,Course
Table2: teacher_id, Course,class
These 2 table cannot apply union as order of data is not same.
Syntax:
Select columbs from table1 union Select columbs from table2
This will show duplicate data only once
Select columbs from table1 UNION ALL Select columbs from table2
This will show duplicate data as much as apppear.
5 Class E History
Chemisty
Biology
History
GROUP BY: