Data base (SQL)homework

profilerick07
case4.docx

Page 47of88

Using the previous assignment (see below) Use SQL command to:

1. Create related tables to capture sales data and sales transactions.

2. Populate all tables with the related data.

3. Update the database when new products arrive.

4. Update the database when a sale is made.

5. Describe (in at least a half page) the major points you learned on SQL and what lessons you have learned in this exercise.

Normalization is the process of organizing databases in the form of tables. It is a technique of eliminating redundancy from database. It makes database easy to maintain and efficient to use. When a table in a database reconstructed into multiple tables to eliminate certain dependency in each table, if dependency is not eliminated then the whole process is repeated until the dependency is eliminated. There are different forms of Normalization:

First Normal Form1NF: It refers to the database table, eliminating repeating sets of data elements and creates individual table of each sets of related data. There is no repetition of any value and attributes in the table as each column holds one attribute and each row holds primary key i.e. unique. Second Normal Form 2NF: It should be 1NF. It doesn’t have partial dependency of any column in primary key and refers to non-prime attribute is dependent on any candidate key. Third Normal Form 3NF: It should be 2NF. There should be no transitive dependency in the table and all the attributes of table depends must depends on candidate key of the table. Example: Online Transaction Processing (OLTP).

Boyce Codd Normal Form BCNF: It should be 3NF. There should be no functional dependency in the table and it must not depend on multiple candidate keys so, that redundancy is improved.

Importance of Normalization:

· Normalization is the technique to minimize data redundancy and also reduce storage requirements.

· It provides unique identification (primary key) from records in a database.

· It eliminates unwanted dependencies from a database table.

· It describes database efficiently.

· It is used for simple retrieval of database designs.

· It determines the nature of data and also gives understanding of data structure and databases.

· Ability to access database quickly and without adjusting consistency and integrity.

My tables are already normalized from case 2. It meets the requirements for 1NF, it has different tables for each set of related data, and there is no repetition of any value and attributes in the table as each column holds one attribute and each row holds primary key. Each column in my tables that is not a determiner of the contents of another column is itself a function of the other columns in the table, thus satisfying the 2NF. Finally my tables allow for things such as pricing to be tracked separately without affecting data in other tables satisfying the requirements for the 3NF.

ACID Properties:

ACID properties is the major concept of database transactions. It ensures reliability and provides safe sharing of data. It stands for Atomicity, Consistency, Isolation and Durability.

Atomicity: It ensures all data transaction as one atom (whole unit) which means transaction is fully committed or completely rollback.

Consistency: It ensures valid data, if there is some invalid data in transactions, database will return back to its previous state. So the data should be authorized with the person.

Isolation: It ensures concurrent transactions. It allows multiple users accessing the same data at the same point of time. It provides integrity and consistency to databases.

Durability: It ensures no single point of failure during transactions. It maintains the committed transactions in the database at the time of system or storage failure.

Need of Normalization:

Normalization ensures redundancy of databases to make it efficient and also eliminating unnecessary data dependencies from the database relationships. It also ensures minimum storage space so that logical data is stored. There is no duplicate data stored in the databases. It also minimize the need of reconstructing data structures and increase the life span of databases. It make relational databases more descriptive and easy to maintain.

Optimization in Normalized databases is achieved by analyzing various small portions of data that is stored in the relational database which is correlated with each other with their dependencies. Analysis of normalization must achieve four goals.

· By arranging data into tables as small logical groups.

· By minimizing data redundancy in the database.

· To maintain integrity of database manipulating data quickly and efficiently.

· Maintaining data in the form, when we modify it, it should be kept at one place.

Let us take an example of Optimizing normalization. Suppose, we are maintaining database of Canada in a table, and after sometime Quebec decided as not to be a part of Canada anymore. In this situation we need to write large code and complex queries to search data and instance of the Quebec and gets changed accordingly. It takes lots of time and become very complex and difficult. In accordance of solving this issue, we use normalization. We need to find a table named like tableProvince, and make the changes in it that will effectively change all the entries related to that table relationships. This practice ensures integrity and consistency throughout the table and provide normalized data.

This is how you would create a table using SQL statements.

create table "tablename"

("column1" "data type",

"column2" "data type",

"column3" "data type");

For example:

create table employee

(first varchar(15),

last varchar(20),

age number(3),

address varchar(30),

city varchar(20),

state varchar(20));

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

MY TABLES NORMALIZED

Entity Set Description

CLIENT:

Name

Cust_ID

Name

Phone

Email

ValueRange

Positive

number

ASCII

ASCII

DateLastTrans

Last shopping date.

Date

Any date equal to or greater than DateCreated

DateCreated

Date joined, first trans.

date

Valid

numericchars

Anydate

beforepresent

Default

Value

NULL

Allowed?Unique?

Singleor Multi-value?

SimpleorComposite?

Next

available

Passedvalue

null

null

null

Passed

value

NO

NO

YES

YES

YES

NO

YES

NO

NO

NO

NO

NO

Single

Single

Single

Single

Single

Single

Simple

Simple

Simple

Simple

Simple

Simple

Description

PK

name

Cellphone

email

Domain/Type

Number(9)

Varchar(128)

Number(10)

Varchar(128)

Candidate keys: Cust_ID,Name, Phone,Email

Primary key:Cust_ID

Weak/Strong: strong

Fieldstobeindexed:Cust_ID

DEPENDENTS:

Name

Bdate

Name

Relationship

Email

DateCreated

Emp_ID

DescriptionDomain/Type

ValueRange

DefaultValue

NULL

Allowed?Unique?

Birthdate

date

Anydatebeforepresent

null

YESNO

Employeename

Varchar(128)ASCII

Passedvalue

NO NO

Relationtoemployee

Varchar(128)ASCII

Passedvalue

NO NO

emailVarchar(128)

ASCII

null

YESNO

Dateadded

date

Anydatebeforepresent

Autogenerated

NO NO

ID ofEmployee

Number(7)integer

Passed value

NO

NO

Singleor Multi-value?

SimpleorComposite?

single

simple

Single

Simple

Single

Simple

Single

Simple

Single

Simple

Single

Simple

Candidate keys:none

Primary key:none

Weak/Strong:weak

Partial key:Name

Fields to be indexed:Name

1 | Page

ELECTRONIC_EMPLOYEES:

Name

Emp_NUM

Name

SSN

Phone

Electronic_ITEMS_ID

Date_Start

Description

PK

name

Socialsecurity#

Phonenumber

Foreign

Key fromElectronic_ITEMStable

Date of employment

Domain/Type

Number(7)

Varchar(128)

Number(9)

Number(10)

Number(3)

Date

ValueRange

1 … 9999

ASCII

ASCII

ASCII

1 … 999

Date

DefaultValue

Next available

Passed value

Passed value

null

FKvalue

Current

NULL

Allowed?

NO

NO

NO

YES

YES

NO

Unique?

YES

NO

YES

NO

NO

NO

Single or Multi-value?

single

Single

Single

Single

Single

Single

Simple or Composite?

simple

Simple

Simple

Simple

Simple

Simple

ELECTRONIC_EMPLOYEES:

Name

Address

PayType

Password

Email

Pay

Date_End

Description

Home address

0=Salaried

&1=hourly

User password

email

Amount they make

Date

Domain/Type

Varchar(128)

Number(1)

Varchar(128)

Varchar(128)

Number(9)

Date

ValueRange

ASCII

0 or 1

ASCII

ASCII

1…99999999

9

Date

DefaultValue

Null

Passedvalue

Passedvalue

null

Null

NULL

NULL

Allowed?

YES

NO

YES

YES

YES

YES

Unique?

NO

NO

NO

NO

NO

NO

Singleor Multi-value?

Single

single

Single

Single

Single

Single

SimpleorComposite?

Simple

simple

Simple

Simple

Simple

Simple

Candidatekeys:Emp_ID,SSN,Email,Name

Primarykey:Emp_ID

Weak/Strong:strong

Emp_IDCHECKOUT:

Name

Checkout_ID

Cust_ID

Date

Store_ID

Subtotal

Emp_ID

Tax

Description

Specific checkout number

FKfrom CustomerID

Date of checkout

Foreign key

from Store table

Amount to fall items on list

Employee who performed checkout

Tax amount on subtotal

Domain/Type

Number(9)

Number(7)

date

Number(3)

double

Number(7)

double

ValueRange

integer

integer

Present date

integer

Positivenumber

integer

Positive number

DefaultValue

Nex tavailable

FKvalue

Auto generated

FKvalue

null

FKvalue

null

NULL

Allowed?

NO

NO

NO

NO

NO

NO

NO

Unique?

YES

YES

NO

YES

NO

YES

NO

Singleor Multi-value?

single

Single

Single

Single

Single

Single

Single

SimpleorComposite?

simple

Simple

Simple

Simple

Simple

Simple

Simple

Candidatekeys: Checkout_ID,Cust_ID,Electronic_ITEMS_ID

Primarykey: Checkout_ID

Weak/Strong:strong

Fields to be indexed:Checkout_ID

ELECTRONIC_ITEMS:

Name

Item_ID

Brand

Description

Price

Cost

Description

Sequence- UniqueinternalID#

Brandofitem

Itemdescription

Currentprice

Whole sale cost

Domain/Type

Number(7)

Varchar(32)

Varchar(128)

Double

Double

ValueRange

integer

ASCII

ASCII

Positive number

Positive number

DefaultValue

Next available

Passed value

Passed value

Passed value

Passed value

NULL

Allowed?

NO

NO

NO

NO

NO

Unique?

YES

NO

NO

NO

NO

Singleor Multi-value?

single

Single

Single

Single

Single

SimpleorComposite?

simple

Simple

Simple

Simple

Simple

ELECTRONIC_ITEMS: (continued)

Name

Shape

Size

UPC

Weight

Taxable

Description

Basicshapedescription

Basic size description

ManufacturesID

Currentweight

Istheitemtaxed

Domain/Type

Varchar(32)

Varchar(32)

Number(9)

Number(4,2)

integer

ValueRange

ASCII

ASCII

Positive number

Positive number

1 for yes 0 for no

DefaultValue

Null

Null

Passedvalue

null

1

NULL

Allowed?

YES

YES

NO

YES

NO

Unique?

NO

NO

YES

NO

NO

Singleor Multi-value?

single

single

Single

Single

Single

SimpleorComposite?

simple

simple

Simple

Simple

Single

Candidate keys: Item_ID,UPC

Primarykey:Item_ID,UPC

Weak/Strong:strong

Fieldstobeindexed:Item_ID,UPC

Name

Electronic_ITEMS_ID

Address

Description

Sequence-UniqueinternalID#

Physicallocation

Domain/Type

Number(3)

Varchar(128)

ValueRange

integer

ASCII

DefaultValue

Nextavailable

Passedvalue

NULL

Allowed?

NO

NO

Unique?

YES

NO

Singleor Multi-value?

single

Single

SimpleorComposite?

Simple

Simple

Candidatekeys:Electronic_ITEMS_ID

Primarykey:Electronic_ITEMS_ID

Weak/Strong:strong

Fieldstobeindexed:Electronic_ITEMS_ID

Page14of88

STORE:

Name

Electronic_ITEMS_ID

Item_ID

Quantity

Description

WhatElectronic_ITEMSisitin?

Whatitemisininventory?

Howmany?

Domain/Type

Number(3)

Varchar(128)

Number(5)

ValueRange

integer

ASCII

Positivenumber

DefaultValue

FKfromElectronic_ITEMStable,ID.

FKfromItemtable,ID.

Passedvalue

NULL

Allowed?

NO

NO

NO

Unique?

NO

NO

NO

Singleor Multi-value?

single

Single

Single

SimpleorComposite?

simple

Simple

Simple

Candidatekeys: Electronic_ITEMS_ID,Item_IDPrimarykey:Electronic_ITEMS_ID,Item_IDWeak/Strong:strong

Fieldstobeindexed:Electronic_ITEMS_ID,Item_ID

Notes:Electronic_ITEMS_IDisneededheretotietheElectronic_ITEMStothequantityforreportsthatareElectronic_ITEMSspecific

Page15of88

Name

Checkout_ID

Quantity

Item_ID

Description

Specificcheckoutnumber.

NumberofElectronic_ITEMSpurchasedonthattransaction.

Specificitemthatwascheckedout.

Domain/Type

sequence

Integer

sequence

ValueRange

1 … 999999

Positivenumber

1 … 9,999,999

DefaultValue

FKfromCheckouttable,ID.

Passedvalue

FKfromElectronic_ITEMStable,ID.

NULL

Allowed?

NO

YES

NO

Unique?

NO

NO

NO

Singleor Multi-value?

Single

Single

Single

SimpleorComposite?

Simple

Simple

Simple

Candidatekeys:Checkout_ID,Item_IDPrimarykey:Checkout_ID,Item_IDWeak/Strong:strong

Fields to be indexed: Checkout_ID,Item_ID

Page16of88

CHECKOUTACTION:

MANAGER

Name

Electronic_ITEMS_ID

Emp_ID

Position

Description

Specificcheckoutnumber.

FKfromEmployeetable

LevelofmanagementattheElectronic_ITEMS.

Domain/Type

sequence

Number

Number

ValueRange

1 … 999

1 … 9999999

1 … 999

DefaultValue

FKfromCheckouttable,ID.

FKfromEmployeetable

1

NULL

Allowed?

NO

NO

NO

Unique?

NO

NO

NO

Singleor Multi-value?

Single

Single

Single

SimpleorComposite?

Simple

Simple

Simple

Candidate keys: Checkout_ID,Item_ID

Primary key:Checkout_ID,Item_ID

Weak/Strong:strong

Fields to be indexed: Checkout_ID,Item_ID

Page 18of88

E-RDiagram

Sample Relation Instances

Clients

Cust_ID

Name

Phone

Email

DateCreated

DateLastTrans

150

BobSuchi

2612552455

null

2001-1-1

2016-5-7

151

Namita

4788754568

[email protected]

2005-5-5

2016-4-25

152

Scottnidhi

4776521465

[email protected]

2011-12-12

2015-3-4

153

neha agarawal

NULL

[email protected]

2008-8-12

2014-5-9

158

Gita

23232345

[email protected]

2001-6-6

2010-8-25

167

Kavita

5619755866

[email protected]

1999-4-9

2010-4-6

199

ScottNeha

4586875837

[email protected]

2000-1-9

2011-10-10

205

ShakeNidhi

4578905545

[email protected]

2000-8-25

2017-8-18

278

Wayneshalini

5619872345

null

2000-1-9

2013-12-5

279

Monik

57895812154

Null

2001-5-20

2012-8-18

Dependents

Bdate

Name

Relationship

Email

DateCreated

Emp_ID

1995-5-12

Bmmm

Husband

NULL

2011-2-30

1

2011-7-23

D

Son

NULL

2011-4-20

2

NULL

Soha

Daughter

[email protected]

2011-4-20

2

NULL

Salman

Husband

null

2015-5-6

3

2004-6-24

Neh

Son

null

2010-9-2

4

19890-2-9

Je

Wife

[email protected]

1990—8-6

6

1992-8-2

Amina

Wife

NULL

2011-9-7

8

2008-9-9

Be

Daughter

NULL

2011-11-5

9

2008-1-8

L

Wife

Null

2011-7-9

9

Page 23of88

Electronic_EMPLOYEES

Emp_ID

Name

SSN

Phone

Pay

Date_End

Electronic_ITEMS_ID

1

DarrelPhilbin

654269856

5489659874

20

2011-2-2

854

2

RickyTanner

125651452

6988532587

10

1999-6-10

354

3

SusanPhillips

145969658

9856984523

15

NULL

696

4

GeorgeScott

147589652

2586521452

42000

NULL

159

5

ErinAbernathy

256985698

5896583541

30

NULL

674

6

TedSmith

352956587

4736593569

50000

NULL

369

7

HarryButs

458521658

2586584763

12

NULL

778

8

MaynarTeener

256656521

2596573257

9.25

NULL

989

9

MattLongfellow

958786548

5249868525

60000

NULL

247

10

JerryGarcia

758965897

6521458569

52000

NULL

348

11

LawarnceTom

625458569

9658745632

15

2011-9-0

348

12

DexterRobert

254125478

1111111111

12.25

NULL

854

13

MarkNick

563258965

2225478512

8.25

NULL

854

14

JeremyDavid

111111112

2356895654

16000

NULL

159

15

LukeTed

111111144

2144544123

20000

NULL

778

Electronic_EMPLOYEEScontinue

Address

Pay_Type

Password

Email

Date_Start

Xyz road

0

1234

[email protected]

1985-4-5

15 7Hst

0

Abcdef

omegam [email protected]

1990-6-8

343 hno xux

st

0

Password

[email protected]

1972-6-9

41 Gold street

0

Apass

NULL

1999-7-25

65 golden avenue

1

Bpaa

[email protected]

1998-12-20

132 asaa

0

Work

NULL

1989-6-8

1 wo street

1

Pass

NULL

1970-10-20

43 sss

0

Pass

null

2005-6-4

64 dss

1

Pass

[email protected]

2000-9-21

434ccc

1

1234

1990-9-24

245aa

1

NULL

NULL

1989-1-20

312xxx

0

NULL

NULL

1990-5-6

68xxx

1

NULL

NULL

1998-2-5

123

0

NULL

NULL

2000-6-3

23 mmmm

1

NULL

NULL

2004-9-9

Checkout

Checkout_ID

Cust_ID

Emp_ID

Date

Electronic_ITEMS_ID

Subtotal

Tax

101

150

2

2014-9-9

54

65.25

1.74

132

151

2

2014-9-6

54

115.25

1.5

6589

152

3

2012-5-01

96

66.52

.35

1147

199

4

2011-11-7

59

500.25

2.24

110

279

5

2012-12-6

74

41.35

.891

1141

105

6

2007-5-6

69

64.25

2.25

3652

278

6

2011-11-12

78

14.25

1.12

225

158

7

2005-12-24

89

80.85

3.12

Electronic_ITEMS

Item_ID

Brand

Description

Price

Cost

1

N

Tea maker

32.25

31.00

65

Britania

Washing machine

3335.00

2223.00

458

Kkkkk

tv

3236.00

224.00

236

Parle

cattle

221.99

.22250

8485

Zzz

e-cooker

2222.50

3434

352

Samsuung

mobile

2224.00

2342.00

355

LG

TV

221.99

123.50

156

De

smartphome

.11150

.11110

25

He

Candy maker

1113.99

111.00

14

Kl

Tea maker

112.99

111.00

Electronic_ITEMS

Electronic_ITEMSID

Address

54

22556ElmSt

154

820BirchRd

96

710EdisonDr

59

13636FirSt

74

14496MapleWay

69

940GreenSt

78

341MainSt

89

25459AspenBlvd

47

13695AlderSt

48

650BeechSt

ManagesFor

FK_Electronic_ITEMS ID

FK_Emp_ID

Position

54

1

Director

154

2

Director

96

3

Director

59

4

Director

74

5

Director

69

6

Director

78

7

Director

89

8

Director

Inventory

FK_Electronic_ITEMS ID

FK_ItemID

Quantity

854

12

10

854

658

10

354

1566

4

696

12

23

696

658

38

159

335

27

159

1566

31

674

4587

23

674

2365

28

SELECT * FROM TMJ_CHECKOUT;

DESC TMJ_CHECKOUT;

SELECT * FROM TMJ_CHECKOUT_ACTION;

DESC TMJ_CHECKOUT_ACTION;

SELECT * FROM TMJ_CLIENT;

DESC TMJ_CLIENT;

SELECT * FROM TMJ_DEPENDANTS;

SELECT * FROM TMJ_INVENTORY;

DESC TMJ_INVENTORY;

DESC TMJ_ELECTRONIC_EMPLOYEES;

Page45of88

DESC TMJ_DEPENDANTS;

SELECT * FROM TMJ_ELECTRONIC_EMPLOYEES;

Page 46of88

DESC TMJ_ELECTRONIC_ITEMS;

DESC TMJ_MANAGESFOR;

DESC TMJ_ELECTRONIC_ITEMS;