Relational Database 1
Relational Database 1
View : It is a virtual table that does not really exist in its right but is instead derived from one or
more underlying base table(s).
Foreign Key : A non-key attribute, whose values are derived from the primary key of some other
table, is known as foreign key in its current table.
Referential Integrity : It is a system of rules that a DBMS uses to ensure that relationships
between records in related tables are valid, and that users don’t accidently delete or change
related data.
MySQL
It is a freely available open source Relational Database Management System (RDBMS) that
uses Structured Query Language (SQL).
Information is stored in tables.
It provides a secure environment for storing, maintaining and accessing data.
It is a fast, reliable and scalable.
CHAR vs VARCHAR
CHAR field has fixed length and VARCHAR has variable length field. A CHAR always takes the
same amount of space regardless of what we store, whereas the storage requirements for a
VARCHAR vary depending on the specific string stored.
1
CREATING DATABASE IN MySQL ACCESSING DATABASE IN MySQL
mysql> CREATE DATABASE menagerie ; Use <databasename> ;
mysql> USE menagerie ;
Database changed.
Syntax
CREATE TABLE <tablename>
(<column name> <data type> [(<size>)] , <column name> <data type> [(<size>)] ……….) ;
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex
CHAR(1), birth DATE, death DATE);
mysql> SHOW TABLES ;
2
INSERT INTO pet VALUES (‘Fluffy’, ‘Harold’, ‘Cat’, ‘f’, ‘1993-02-04’, ‘2018-02-05’) ;
INSERT INTO pet (name, owner, species, sex, birth, death) VALUES (‘Fluffy’, ‘Harold’, ‘Cat’,
‘f’, ‘1993-02-04’, ‘2018-02-05’);
INSERT INTO pet (name, owner,sex) VALUES ((‘Fluffy’, ‘Harold’, ‘f’);
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy
3
2. Selecting particular rows
4
4. Eliminating Redundant Data (DISTINCT Keyword)
SELECT DISTINCT owner
From pet ;
5. ALL Keyword : It retains the duplicate output rows. It is just the same as when you specify
neither DISTINCT nor ALL.
SELECT ALL owner
From pet ;
5
SELECT name, species, birth
From pet
Order by species, birth DESC ;
SELECT *
From pet
Where species NOT IN (‘snake’, ‘dog’, ‘bird’, ‘hamster’) ;
6
Name containing exactly five characters :
10. WORKING WITH NULL VALUES (IS NULL and IS NOT NULL)
SELECT name, owner
From pet NO OUTPUT
Where species IS NULL;
7
SQL CONSTRAINTS
While creating tables, we may need to apply certain conditions on columns. To apply conditions
on columns, SQL constraints are used.
Constraints : It is a condition applicable on a field or set of field.
2. SQL DEFAULT Constraints : It provides a default value to a column when the INSERT INTO
statement does not provide a specific value.
CREATE TABLE Student
(Student_ID integer, Last_name varchar (30), First_name varchar (30), Score DEFAULT 80);
INSERT INTO Student (Student_ID, Last_name, First_name) VALUES (‘10’, ‘nitin’, ‘sharma’);
3. SQL UNIQUE Constraints : It ensures that all values in a column are distinct.
CREATE TABLE Customer
( SID integer Unique, Last_name varchar (30) NOT NULL, First_name varchar (30) ) ;
4. SQL CHECK Constraints : It ensures that all values in a column satisfy certain conditions.
CREATE TABLE Customer
(SID integer CHECK (SID > 0), Last_name varchar (30) NOT NULL, First_name varchar (30)) ;
8
5. PRIMARY KEY CONSTRAINT
A Primary key is used to uniquely identify each row in a table.
When multiple fields are used as a primary key, they are known as composite key.
Primary key can be specified in two ways :
Using CREATE TABLE
CREATE TABLE Customer
( SID integer NOT NULL UNIQUE PRIMARY KEY, Last_name varchar (30) NOT NULL,
First_name varchar (30) ) ;
5 SHRUTI SCIENCE
Relation : Orders
6 DAMINI COMMERCE
Relation : Person
Foreign key can be specified in two ways :
Using CREATE TABLE
CREATE TABLE Orders
(OrderID integer NOT NULL UNIQUE PRIMARY KEY, OrderNumber integer, PersonID
integer, Foreign key (PersonID) references Person (PersonID)) ;
Using ALTER TABLE
ALTER TABLE Orders
ADD Foreign key (PersonID) references Person (PersonID) ;
9
APPLYING TABLE CONSTRAINTS AND COLUMN CONSTRAINTS
CREATE TABLE Items
(icode char(5) NOT NULL, COLUMN CONSTRAINTS
descp CHAR(5) NOT NULL,
Salary integer,
Expenditure integer,
CHECK (Salary > Expenditure), TABLE CONSTRAINTS
UNIQUE (icode, descp)) ;
1 AMAN SCIENCE
2 ALEENA SCIENCE
3 NIKITA M. COMMERCE
4 AASTHA COMMERCE
5 SHRUTI SCIENCE
6 DAMINI COMMERCE
UPDATE Person
PersonID NAME STREAM SET NAME = ‘SHRUTI NAYAK’
Where NAME = ‘SHRUTI’ ;
1 AMAN SCIENCE
2 ALEENA SCIENCE
3 NIKITA M. COMMERCE
4 AASTHA COMMERCE
SHRUTI
5 SCIENCE
NAYAK
6 DAMINI COMMERCE
10
DELETING DATA FROM TABLES
DELETE command : It is used to remove rows from a table. DELETE FROM employee
WHERE gross < 2200 ;
ALTERING TABLES
It is used to change definitions of existing tables. ALTER TABLE command is used :
To add a column
To redefine a column (datatype, size, default value)
To add an integrity constraint
DROPPING TABLES
The DROP TABLE statement is used to drop an existing table in a database.
Syntax : DROP TABLE <tablename> ; OR DROP TABLE IF EXISTS <tablename> ;
TRUNCATE TABLE
It is used to delete the data inside a table, but not the table itself.
Syntax : TRUNCATE TABLE <tablename>
12