P. 1
Normalization

Normalization

|Views: 0|Likes:
Published by vinod_kumar_147

More info:

Published by: vinod_kumar_147 on Sep 16, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

09/16/2012

pdf

text

original

Let’s try understanding normalization How many normal forms are there?

There are seven normal forms. They are
• • • • • • •

First Normal Form Second Normal Form Third Normal Form Boyce-Codd Normal Form Fourth Normal Form Fifth Normal Form Sixth or Domain-key Normal form

Why do we need to do normalization? To eliminate redundancy of data i.e. having same information stored at multiple places, which eventually be difficult to maintain and will also increase the size of our database. With normalization we will have tables with fewer columns which will make data retrieval and insert, update and delete operations more efficient. What do we mean when we say a table is not in normalized form? Let’s take an example to understand this, Say I want to create a database which stores my friends name and their top three favorite artists. This database would be quite a simple so initially I’ll be having only one table in it say friends table. Here FID is the primary key. FID 1 2 FNAME Srihari Arvind FavoriteArtist Akon, The Corrs, Robbie Williams. Enigma, Chicane, Shania Twain

This table is not in normal form why?

country etc. Remember For First Normal Form Column values should be atomic. awards received by them. 2. address and favorites artists albums.e. We’ll first break our single table into two. . Let’s modify this table FID 1 2 FNAME Srihari Arvind FavoriteArtist1 Akon. repeating group of data or repeating columns. scalar or should be holding single value No repetition of information or values in multiple columns.FavoriteArtist column is not atomic or doesn’t have scalar value i. Shania Twain This table is also not in normal form why? We have now changed our table and now each column has only one value!! (So what’s left?) Because here we are having multiple columns with same kind of value. it has having more that one value. The Corrs Robbie Williams Enigma Chicane Shania Twain FID foreign key in FavoriteArtist table which refers to FID in our Friends Table.e. Each table should have information about only one entity so it would be nice if we store our friend’s information in one table and his favorite artists’ information in another (For simplicity we are working with few columns but in real world scenario there could be column like friend’s phone no. So what we need to do to make it normal or at least bring it in First Normal Form? 1. Now we can say that our table is in first normal form. So in that case having two different tables would make complete sense) FID 1 2 FID 1 1 1 2 2 2 FNAME Srihari Arvind Favorite Artist Akon. I. Enigma FavoriteArtist2 The Corrs Chicane FavoriteArtist3 Robbie Williams. email .

If I know gadget will I come to know about supplier address? Obviously no. If I know both gadget and supplier can I know cost? Yes than we can. But still let’s try to understand second normal form with another example This is our new table Gadgets Headphone Mp3 Player Headphone Supplier Abaci Sagas Mayas Cost 123$ 250$ 100$ Supplier Address New York California London In about table ITEM+SUPPLIER together form a composite primary key. Here we can say that our Friend database was already in second normal form l. If I know who the supplier is can I have it address?Yes. . Why? Because we don’t have composite primary key in our friends and favorite artists table. So cost is fully dependent (functionally dependent) on our composite primary key (Gadgets+Supplier) Let’s start with another non-key column Supplier Address. Let’s check for dependency If I know gadget can I know the cost? No same gadget is provided my different supplier at different rate. If I know supplier can I know about the cost? No because same supplier can provide me with different gadgets. Composite primary keys are.So what does Second Normal Form means? For second normal form our database should already be in first normal form and every non-key column must depend on entire primary key. But there is no such thing in our database.primary keys made up of more than one column.

In our above example Supplier Address was partially dependent on our composite key columns (Gadgets+Supplier). Composite Key: -Composite key is a primary key composed of multiple columns. but not all the columns in a composite primary key. But let’s take a break over here and learn some definitions and terms. This table is surely not in Second Normal Form.SupplierAddress “In our s table supplier address column is functionally dependent on the supplier column” Partial Functional Dependency – A non-key column is dependent on some. Supplier Address is functionally dependent on supplier name.So here supplier is not completely dependent on (partial dependent) on our composite primary key (Gadgets+Supplier). Transitive Dependency. e.A transitive dependency is a type of functional dependency in which the value in a non-key column is determined by the value in another non-key column. Functional Dependency – When value of one column is dependent on another column. So what do we need to do to bring it in second normal form? Here again we’ll break the table in two. . S. With these definitions in mind let’s move to Third Normal Form.Supplier–àS. So that if value of one column changes the value of other column changes as well. Gadgets Headphone Mp3 Player Headphone Supplier Abaci Sagas Mayas Supplier Abaci Sagas Mayas Supplier Address New York California London Cost 123$ 250$ 100$ We now how to normalize till second normal form.g. If supplier’s name is changed in a record we need to change the supplier address as well.

Album Come on over History Up MCMXC A. Album Come on over History Up MCMXC A. Album is the primary key of the above table. So in our above table Country column is depended on Artist column which is a non-key column. repeating values of Country column.D. The cross of changes Artist Shania Twain Michael Jackson Enigma Artist Shania Twain Michael Jackson Shania Twain Enigma Enigma Country Canada USA Spain No.e. The cross of changes Artist Shania Twain Michael Jackson Shania Twain Enigma Enigma No. Again we need to make sure that the non-key columns depend upon the primary key and not on any other non-key column. we shouldn’t have any non-key column depending on any other non-key column.For a table in third normal form • • It should already be in Second Normal Form. of tracks are functionally dependent on the Album(primary key). i. There should be no transitive dependency. But can we say the same of Country as well? In the above table Country value is getting repeated because of artist.e. Artist and No.D. of tracks 11 15 11 8 10 . So we will move that information in another table and could save table from redundancy i. of tracks 11 15 11 8 10 Country Canada USA Canada Spain Spain Although the above table looks fine but still there is something in it because of which we will normalize it further.

when a new record (representing a new worker) is added to the employees table. CLR Triggers Note:DDL and CLR Triggers cannot work in SQL Server 2000 DML Trigger:-These Trigger is fired only when INSERT. 1) After INSERT 2) After UPDATE 3) After DELETE 1) creating After INSERT Trigger:-Syntax: create trigger triggername on tablename AFTER INSERT As [SQL Statement/PRINT command] GO Eg: create trigger afterinsert_trigger on emp AFTER INSERT as PRINT 'AFTER TRIGGER EXECUTED SUCESSFULLY' GO Triggers are commonly used to: • • • • prevent changes (e. INSTEAD OF Triggers 2. not the client's) . Explanation on DML Trigger: Let us create a Table and insert some records in that Table. Triggers are of 3 types in SQL Server 2005: A database trigger is procedural code that is automatically executed in response to certain events on a particular table or view in a database. The trigger is mostly used for keeping the integrity of the information on the database. For example. keep a log of the users and roles involved in changes) enhance changes (e. 1) After Triggers: After Triggers can be created in 3 ways. ensure that every change to a record is time-stamped by the server's clock. DML Triggers . and DELETE Statement occurs in table.g.g. UPDATE. prevent an invoice from being changed after it's been mailed out) log changes (e. Most of real-world application has databases which are in third normal forms. and salaries 1. new records should be created also in the tables of the taxes. AFTER Triggers .g.g. keep a copy of the old data) audit changes (e. vacations.Normally this is considered enough and we don’t really go on applying the other normal forms. DDL Triggers 3.

When we use the original table in the web applications the hackers may drop the table. that use many places in the stored procedures or functions. but now a->somefield and b->someotherfield are both located in the same buffer.g. The pointer to struct "a" could be assigned to the value of a buffer. That time the original data will be persist in the table. so that the "static". When to use VIEW? When you have complex queries. . In this way. require that every invoice have at least one line item) execute business rules (e. It will be used as security mechanism in the web applications. name2. etc. The view schema can be stored in the encrypted format. That is the idea behind a union. store a record of every change..g. which can have the multiple columns from the one or more table. notify a manager every time an employee's bank account number changes) replicate data (e. It gives different ways to break down the same buffer area.. When you want to hide the particular columns to the specific people then we can create the specialized view.• • • enforce business rules (e. When we create the view it stores the view definition schema as object under the concern database. and making a pointer to each kind of struct. Let us see the syntax of the create view CREATE VIEW View Name [Alias name1. Unions: A union is a way of providing an alternate way of describing the same memory area. and the pointer to struct "b" could be assigned to the same buffer. Here is an option like SCHEMA BINDING. The idea of a union could be handled in a different way by having 2 different structs defined.g. Normally view cannot store the data permanently in the table. It can be used like the normal table. or similar portion of the data is described first. you could have a struct that contains a union.] WITH ENCRYPTION WITH SCHEMA BINDING AS SELECT statement [WITH CHECK OPTION] The create view can be created with the view name and the alias can be given in the view name parameter parenthesis. this is an important mile stone in the view to allow the developers to create the permanent view. to be shipped to another database later) Views: The view is a virtual table. and the portion that changes is described by the union.

float b. 2. }. Each element is separate and distinct.Answer: The difference between structure and union in c are: 1. one block is used by all the member of the union but in case of structure. struct TT{ int a. and p at any given . union bar { char c. char are taken as 4. char c.If a variable in double exists in union then the size of union and struct would be 8 bytes and cumulative size of all variables in struct. There is frequent rwquirement while interacting with hardware to access access a byte or group of bytes simultaneously and sometimes each byte individually. } Union UU{ int a. That is a Union offers a way for a section of memory to be treated as a variable of one type on one occasion and as a different variable of a different type on another occasion. A struct foo contains all of the elements c. each member have their own memory space Difference in their Usage: While structure enables us treat a number of different variables stored at different in memory . }.1) sizeof UU(Union) would be 4 bytes as supposed from above. char *p. a union enables us to treat the same space in memory as a number of different variables. char c. union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. Usually union is the answer. l.4. char *p. =======Difference With example***** Lets say a structure containing an int. l.float. In union. } sizeof TT(struct) would be >9 bytes (compiler dependent-if int. float b. Detailed Example: struct foo { char c.char and float is created and a union containing int char float are declared. A union bar contains only one of the elements c. and p. long l. long l.

myfoo.char *argv[]) { struct foo myfoo.l = 2L.mybar. Each element is stored in the same memory location (well.c = 1. I know it invokes the above-mentioned "undefined behavior". union bar mybar. char *p. but most likely will give some sort of output on most computers. long l. I get: . printf("myfoo: %d %ld %s\n". and you can only refer to the element which was last stored.myfoo.l = 2L. int main(int argc.c = 1. mybar.c. (ie: after "barptr->c = 2. long l. union bar { char c. mybar.) Try the following program.) ========== #include struct foo { char c.p = "This is myfoo". } ========== On my system. }. }.time.l. printf("mybar: %d %ld %s\n". (Yes. char *p." you cannot reference any of the other elements. myfoo. such as "barptr->p" without invoking undefined behavior.mybar. myfoo.mybar.p).p). mybar. return 0. myfoo.p = "This is mybar".c. they all start at the same memory location).l.myfoo.

myfoo: 1 2 This is myfoo mybar: 100 4197476 This is mybar .

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->