0% found this document useful (0 votes)
11 views12 pages

Relational Database 1

PYTHON RELATIONAL DATABASE

Uploaded by

Aman Meena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

Relational Database 1

PYTHON RELATIONAL DATABASE

Uploaded by

Aman Meena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MySQL SQL Revision Tour

Limitation of File Processing System


 Data redundancy (Data duplication)
 Data inconsistency
 Unsharable data
 Unstandardized data
 Insecure data
 Incorrect data

Various advantages of Database System are :


 It reduces data redundancy
 It controls data inconsistency
 It facilitate sharing of data
 It ensures data security
 Centralized control of database

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.

CREATING TABLES IN MySQL


mysql> SHOW TABLES ;
EMPTY SET (0.00 sec)

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 ;

DESCRIBE Statement : It is used to describe the structure of a table.


mysql> Describe pet ; OR mysql> DESC pet ;

INSERTING DATA INTO TABLE


There are three ways to insert values in a table :

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’);

INSERTING NULL Values

 INSERT INTO pet (name, owner,sex) VALUES ((‘Fluffy’, ‘Harold’, NULL);

MAKING SIMPLE QUERIES THROUGH “SELECT COMMAND”


SELECT : It is used to pull information from a table. Its syntax is as follows :

SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy

1. Selecting all data

3
2. Selecting particular rows

3. Selecting Particular Column

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 ;

6. SORTING ROWS – ORDER BY


SELECT name, birth SELECT name, birth
From pet From pet
Order by birth ; Order by birth DESC;

5
SELECT name, species, birth
From pet
Order by species, birth DESC ;

7. CONDITION BASED ON A RANGE – (BETWEEN operator) :


SELECT name, species
From pet
Where birth BETWEEN 1989-05-13 AND 1994-03-17 ;

8. CONDITION BASED ON A LIST – (IN and NOT IN operator) :


SELECT *
From pet
Where species IN (‘snake’, ‘bird’) ;

SELECT *
From pet
Where species NOT IN (‘snake’, ‘dog’, ‘bird’, ‘hamster’) ;

9. PATTERN MATCHING – (% and _ operator)


% : It matches any substring AND _ : It matches any character.

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;

SELECT name, owner


From pet
Where species IS NOT NULL ;

11. PERFORMING SIMPLE CALCULATIONS


SELECT 3.1416*6*6 ;
SELECT curdate( ) ;

12. USING COLUMN ALIASES


The columns can be given a different name i.e column alias name for output purposes.
SELECT name AS pet_name
From pet ;

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.

S.NO CONSTRAINTS DESCRIPTION


1 NOT NULL Ensures that a column cannot have NULL value
2 DEFAULT Provides a default values for a column when none is specified
3 UNIQUE Ensures that all values in a column are different
4 CHECK Makes sure that all values in a column satisfy certain criteria
5 Primary Key It is used to uniquely identify a row in the table
6 Foreign Key It is used to ensure referential integrity of the data

1. SQL NOT NULL Constraints : By default, a column can hold NULL.


CREATE TABLE Customer
( SID integer NOT NULL, Last_name varchar (30) NOT NULL, First_name varchar (30) ) ;
INSERT INTO Customer (Last_name, First_name) VALUES (‘Wang’ , ‘Pedro’) ;
It will result in an error because this will lead to column SID being NULL, which violates the
NOT NULL constraints on that column.

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’);

Student_ID Last_Name First_Name Score


10 nitin sharma 80

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) ) ;

 Using ALTER TABLE


ALTER TABLE Customer
ADD PRIMARY KEY (SID) ;
6. FOREIGN KEY CONSTRAINTS
 A Foreign key is a field (collection of fields) in one table that refers to the PRIMARY KEY
in another table.
 A Foreign key is a key, which is used to link two tables together.
 The table containing the foreign key is called the child table, and the table containing
the primary key is called the parent table.

PersonID NAME STREAM OrderID OrderNumber PersonID

1 AMAN SCIENCE 1 77895 3

2 ALEENA SCIENCE 2 44678 3

3 NIKITA M. COMMERCE 3 22456 3

4 AASTHA COMMERCE 4 24562 4

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)) ;

INSERTING DATA INTO ANOTHER TABLE


INSERT INTO branch1
SELECT *
FROM branch2
WHERE GROSS > 70000 ;

MODIFYING DATA IN TABLES


Update command : It is used to modify the existing records in a table.

PersonID NAME STREAM

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 ;

To remove all the contents of the table. DELETE FROM employee ;

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

To add a new column tel_number of type integer in table employee :


ALTER TABLE employee
ADD (tel_number integer) ;
To modify column Job of table employee to have new width of 30 characters :
ALTER TABLE employee
MODIFY (Job CHAR(30)) ;
To change the name of a column :
ALTER TABLE employee
CHANGE First_Name FirstName VARCHAR (20) ;

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>

CLASSIFICATION OF SQL STATEMENTS


 DDL (Data Definition Language) : It consist of commands that can be used to define the
database schema.
Example : CREATE, DROP, ALTER, TRUNCATE
 DML (Data Manipulation Language) : It consist of commands that deals with the
manipulation of data present in database.
Example : SELECT, INSERT, UPDATE , DELETE
11
 DCL (Data Control Language) : It consist of commands which deals with the rights,
permission and other controls of the database system.
Example : GRANT AND REVOKE.
 TCL (Transaction Control Language) : It consist of commands that deals with the
transaction within the database.
Example :
COMMIT : It is used to permanently save any transaction into the database.
COMMIT ;
ROLLBACK : It restores the database to last committed state.
ROLLBACK TO savepoint_name ;
SAVEPOINT : It is used to temporarily save a transaction so that you can rollback to that
point whenever required.
SAVEPOINT savepoint_name ;

12

You might also like