You are on page 1of 10

Lets 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? Lets 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 Ill 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?

FavoriteArtist column is not atomic or doesnt have scalar value i.e. it has having more that one value. Lets modify this table FID 1 2 FNAME Srihari Arvind FavoriteArtist1 Akon. Enigma FavoriteArtist2 The Corrs Chicane FavoriteArtist3 Robbie Williams. 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 whats left?) Because here we are having multiple columns with same kind of value. I.e. repeating group of data or repeating columns. So what we need to do to make it normal or at least bring it in First Normal Form? 1. Well first break our single table into two. 2. Each table should have information about only one entity so it would be nice if we store our friends 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 friends phone no, email , address and favorites artists albums, awards received by them, country etc. 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. The Corrs Robbie Williams Enigma Chicane Shania Twain

FID foreign key in FavoriteArtist table which refers to FID in our Friends Table. Now we can say that our table is in first normal form. Remember For First Normal Form Column values should be atomic, scalar or should be holding single value No repetition of information or values in multiple columns.

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. Here we can say that our Friend database was already in second normal form l. Why? Because we dont have composite primary key in our friends and favorite artists table. Composite primary keys are- primary keys made up of more than one column. But there is no such thing in our database. But still lets 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. Lets 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. If I know both gadget and supplier can I know cost? Yes than we can. So cost is fully dependent (functionally dependent) on our composite primary key (Gadgets+Supplier) Lets start with another non-key column Supplier Address. If I know gadget will I come to know about supplier address? Obviously no. If I know who the supplier is can I have it address?Yes.

So here supplier is not completely dependent on (partial dependent) on our composite primary key (Gadgets+Supplier). This table is surely not in Second Normal Form. So what do we need to do to bring it in second normal form? Here again well break the table in two. 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. But lets take a break over here and learn some definitions and terms. Composite Key: -Composite key is a primary key composed of multiple columns. Functional Dependency When value of one column is dependent on another column. So that if value of one column changes the value of other column changes as well. e.g. Supplier Address is functionally dependent on supplier name. If suppliers name is changed in a record we need to change the supplier address as well. S.SupplierS.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, but not all the columns in a composite primary key. In our above example Supplier Address was partially dependent on our composite key columns (Gadgets+Supplier). Transitive Dependency- 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. With these definitions in mind lets move to Third Normal Form.

For a table in third normal form


It should already be in Second Normal Form. There should be no transitive dependency, i.e. we shouldnt have any non-key column depending on any other non-key column.

Again we need to make sure that the non-key columns depend upon the primary key and not on any other non-key column. Album Come on over History Up MCMXC A.D. The cross of changes Artist Shania Twain Michael Jackson Shania Twain Enigma Enigma No. 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. Album is the primary key of the above table. Artist and No. of tracks are functionally dependent on the Album(primary key). But can we say the same of Country as well? In the above table Country value is getting repeated because of artist. So in our above table Country column is depended on Artist column which is a non-key column. So we will move that information in another table and could save table from redundancy i.e. repeating values of Country column. Album Come on over History Up MCMXC A.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. of tracks 11 15 11 8 10

Normally this is considered enough and we dont really go on applying the other normal forms. Most of real-world application has databases which are in third normal forms. 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, when a new record (representing a new worker) is added to the employees table, new records should be created also in the tables of the taxes, vacations, and salaries 1. DML Triggers . AFTER Triggers . INSTEAD OF Triggers 2. DDL Triggers 3. CLR Triggers Note:DDL and CLR Triggers cannot work in SQL Server 2000 DML Trigger:-These Trigger is fired only when INSERT, UPDATE, and DELETE Statement occurs in table. Explanation on DML Trigger: Let us create a Table and insert some records in that Table. 1) After Triggers: After Triggers can be created in 3 ways. 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.g. prevent an invoice from being changed after it's been mailed out) log changes (e.g. keep a copy of the old data) audit changes (e.g. keep a log of the users and roles involved in changes) enhance changes (e.g. ensure that every change to a record is time-stamped by the server's clock, not the client's)

enforce business rules (e.g. require that every invoice have at least one line item) execute business rules (e.g. notify a manager every time an employee's bank account number changes) replicate data (e.g. store a record of every change, to be shipped to another database later)

Views: The view is a virtual table, which can have the multiple columns from the one or more table. It can be used like the normal table. Normally view cannot store the data permanently in the table. When we create the view it stores the view definition schema as object under the concern database. Let us see the syntax of the create view CREATE VIEW View Name [Alias name1, name2,] 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. The view schema can be stored in the encrypted format. Here is an option like SCHEMA BINDING; this is an important mile stone in the view to allow the developers to create the permanent view. When to use VIEW? When you have complex queries, that use many places in the stored procedures or functions, etc.., It will be used as security mechanism in the web applications. When we use the original table in the web applications the hackers may drop the table. That time the original data will be persist in the table. When you want to hide the particular columns to the specific people then we can create the specialized view. Unions: A union is a way of providing an alternate way of describing the same memory area. In this way, you could have a struct that contains a union, so that the "static", or similar portion of the data is described first, and the portion that changes is described by the union. The idea of a union could be handled in a different way by having 2 different structs defined, and making a pointer to each kind of struct. The pointer to struct "a" could be assigned to the value of a buffer, and the pointer to struct "b" could be assigned to the same buffer, but now a->somefield and b->someotherfield are both located in the same buffer. That is the idea behind a union. It gives different ways to break down the same buffer area.

Answer: The difference between structure and union in c are: 1. 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. 2. In union, one block is used by all the member of the union but in case of structure, 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 , a union enables us to treat the same space in memory as a number of different variables. 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. There is frequent rwquirement while interacting with hardware to access access a byte or group of bytes simultaneously and sometimes each byte individually. Usually union is the answer. =======Difference With example***** Lets say a structure containing an int,char and float is created and a union containing int char float are declared. struct TT{ int a; float b; char c; } Union UU{ int a; float b; char c; } sizeof TT(struct) would be >9 bytes (compiler dependent-if int,float, char are taken as 4,4,1) sizeof UU(Union) would be 4 bytes as supposed from above.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. Detailed Example: struct foo { char c; long l; char *p; }; union bar { char c; long l; char *p; }; A struct foo contains all of the elements c, l, and p. Each element is separate and distinct. A union bar contains only one of the elements c, l, and p at any given

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

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

You might also like