database_note_send

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

Chapter: Database

Data Integrity: Ensuring consistency, correctness and accuracy of data. Can be done
by:

 Making sure changes occur for all copies of data


 Ensuring referential integrity
 Using validation and verification rules

Limitation of a file-based approach

File is collection of items of data. Can be structured as a collection of records, each


record made up of fields containing data about the same ‘thing’. Individual elements
are data items.

Limitations are:

 Storage space is wasted due to duplication and redundancy


 Data can be altered by one application and not by other; i.e. inconsistent
 Enquiries depend on structure of data and software used. i.e. Data is not
independent.

Database:

Database is structured collection of data items that can be accessed by different


application programs.

Relational database is a database in which the data items are linked by internal pointers.
Benefits of database approach:

 No redundancy or little redundancy.


 Data altered in one application is not available in another application. i.e.
consistent
 Enquiries are independent of structure of data and software used. i.e. data is
independent.

Terms related to relational database:

Table is group of similar data, in a database, with rows for instance of entity and column
for attribute.

Record is a row in database table. Field is column. Each column of the table is an
attribute (description).

Database must be updated automatically or manually, to ensure consistency among


data.

Pointers are keys in database that provide relationship between tables.

Primary key is unique identifier of a table. Foreign key is set of attributes in one table
that refer to the primary key in another table. Candidate key is an attribute or set of
attributes in a table where no tuple has the same value. Primary key is special case of
candidate key. Secondary key is alternative to primary key.
i.e., Candidate keys are unique set of attributes. We select one as primary key and rest
as secondary keys. For another table linked to this one, primary key of this table is the
foreign key.

Relationships

A relationship is link between two tables which is formed when one table in database
has a foreign key that refers to a primary key in another table in the database.

If primary key corresponding to foreign key isn’t there, this creates issue due to lack of
referential integrity.

Relationship can be of type: one-to-one, one-to-many, many-to-one, many-to-many

Entity Relationship diagrams:


It documents database design. Visual representation of how entities are related.

Normalization process:

Process of making sure a relational database conforms to certain rules to ensure


efficiency.

1NF: Ensuring that a table doesn’t contain repeating attributes or groups and all data in
the table are atomic. (Generally adding rows)

2NF: Ensuring that database is in 1NF + there are no partial dependencies on primary
key. (Generally adding tables)

3NF: 2NF + all fields are fully dependent on the primary key. (Adding tables)

Fully normalized => Database is in 3NF.


DDL and DML using SQL:

Data definition Language (DDL): used to create, modify and remove data structures
that form a database.

Data Manipulation Language (DML): used to add, modify, delete and retrieve the data
stored in a relational database.

SQL scripts: list of SQL commands that perform specified task.

i.e., DDL defines structure or schema of the database and DML helps in managing and
manipulating data in the database.

DDL:

DDL(SQL) command Description


CREATE DATABASE Creates a database
CREATE TABLE Creates a table definition
ALTER TABLE Changes the definition of a table
PRIMARY KEY Adds a primary key to a table
FOREIGN KEY … REFERENCES … Adds a foreign key to a table

Creating a database:

CREATE DATABASE <name_of_database>

Defining a table:

CREATE TABLE <table_name> (

<column_name> datatype,

<column_name> datatype,

……

);

Example:

CREATE TABLE Student (


StudentID INT PRIMARY KEY NOT NULL,

NAME TEXT NOT NULL,

AGE INT NOT NULL,

ADDRESS VARCHAR(50),

ClassID CHARACTER

);

Altering a table:

ALTER TABLE <table_name> ADD PRIMARY KEY (<column_name>) adds


primary key.

ALTER TABLE <table_name> ADD FOREIGN KEY <column_name>


REFERENCES <table_name>(<column_name>) adds foreign key. (Look example)

Example:

ALTER TABLE Student ADD PRIMARY KEY (StudentID) would make


StudentID the primary key if it wasn’t already.

ALTER TABLE Student ADD FOREIGN KEY ClassID REFERENCES


Class(ClassID) //This assumes there are two tables Class and Student. Foreign key
ClassID is added which is present in both tables to link them.

** After creating the table you can view the schema of table using .schema (sqlite)

Available data types:

Data type for attributes Description


CHARACTER Fixed length text
VARCHAR(n) Variable length text
BOOLEAN True or False (1 or 0)
INTEGER Whole number
REAL Floating point numbers
DATE YYYY-MM-DD
TIME HH:MM:SS
DML Commands:

Syntax of SELECT Statement (basic):

SELECT column1, column2, …. column FROM table_name;

If you want to select all the available fields (column names):

SELECT * FROM table_name;

** If you are using sqlite for practice: Use header on and mode column and width
value1, value2, … valueN to customize look of table

Using where to add conditions:

SELECT column1, column2, column

FROM table_name

WHERE [condition]

To specify condition, use logical operators >, <, =, NOT, etc.


For example:

SELECT * FROM COMPANY

WHERE AGE >= 25 AND NOT SALARY >= 65000;

ORDER BY clause is used to sort the data in an ascending or descending order, based
on one or more columns.

SYNTAX:

SELECT column-list

FROM table_name

WHERE [condition]

ORDER BY [column list] [ASC / DESC];

GROUP BY clause is used to arrange identical data into groups. GROUP BY clause
precedes the ORDER BY clause.

SYNTAX:

SELECT column-list

FROM table_name

WHERE [conditions]

GROUP BY [column list]

ORDER BY [column list]

You can use SUM, AVG, COUNT function similarly:

SYNTAX:

SELECT SUM(column name) FROM table_name;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy