7<br />SQL SERVER: CUSTOMIZINGTHE DATABASE DESIGN<br />
Customizing a database<br />The Power of a DBMS lies in the control that it offers the user over the data lying underneath...
  Adding a foreign key
  Adding Constraints
  Unique Constraint
  Not Null Constraint
  Check Constraint</li></li></ul><li>Managing Data Integrity<br />Data integrity controls of the SQL Server 2008, help to ...
Primary Keys<br />What is a Primary Key:<br />A Primary key is a field/attribute in a table which is used to uniquely iden...
Primary Key<br />Adding a Primary Key:<br />A Table must have only one primary key. The Primary key must be defined during...
Redefining primary key<br />Now, let us take a step back and redefine the primary key that we have learnt already.<br />Th...
Redefining primary key<br />In this case, it would be possible to locate a record uniquely only with information of more t...
Adding primary keys to existing tables<br />The SQL Server allows primary keys to be added to an existing table. <br />Syn...
Foreign Keys<br />Foreign Key:<br />A Foreign key is a field/attribute in one table which is used as a primary key in anot...
Foreign Keys<br />In SQL Server, a foreign key is created as follows:<br />Create table &lt;Table2_Name&gt; (&lt;filedName...
Adding foreign keys to existing tables<br />The SQL Server allows foreign keys to be added to an existing table. <br />Syn...
Not Null Constraint<br />In feeding data into a database, it lies with the user to feed data for a field or assign a null ...
Unique Constraint<br />In SQL Server, it is possible to designate a field to be unique, .i.e., the field will not accept d...
Upcoming SlideShare
Loading in...5
×

MS SQLSERVER:Customizing Your D Base Design

347

Published on

MS SQLSERVER:Customizing Your D Base Design

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • http://dbmanagement.info/Tutorials/SQL_Server.htm
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total Views
347
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

MS SQLSERVER:Customizing Your D Base Design

  1. 1. 7<br />SQL SERVER: CUSTOMIZINGTHE DATABASE DESIGN<br />
  2. 2. Customizing a database<br />The Power of a DBMS lies in the control that it offers the user over the data lying underneath. By customizing the structure of the data, a programmer can optimize the efficiency of the system in a specific implementation.<br />A Basic customization feature is controlling the type of data that is stored in a database table.<br />Some of the such basic customizations are:<br /><ul><li> Adding a primary key
  3. 3. Adding a foreign key
  4. 4. Adding Constraints
  5. 5. Unique Constraint
  6. 6. Not Null Constraint
  7. 7. Check Constraint</li></li></ul><li>Managing Data Integrity<br />Data integrity controls of the SQL Server 2008, help to guarantee the accuracy, validity, and correctness of data stored. They are a set of rules which can be defined by the programmer, so as to decide if a given data is valid for the system or not.<br />Now, let us see more about them.<br />
  8. 8. Primary Keys<br />What is a Primary Key:<br />A Primary key is a field/attribute in a table which is used to uniquely identify a record<br />Eg: Consider a dream database<br />Here, a particular record can be uniquely located using the DreamNumber and hence, it is taken as the Primary key.<br />If there are more than fields, eligible of being the primary key, the decision of choosing one among them lies with the DB designer<br />
  9. 9. Primary Key<br />Adding a Primary Key:<br />A Table must have only one primary key. The Primary key must be defined during the creation of the table. <br />Syntax:<br />Create table &lt;tableName&gt; ( &lt;fieldName1&gt;&lt;field1Type&gt; primary key, ..);<br />Example: Consider the creation of the following table. The field ‘Dream Number’ is to be designated as the primary key<br />Create table dreamtable( dreamnumberintprimary key, dream varchar(10), dreamdate date, dreamtime time, dreamtypevarchar(10)) ;<br />
  10. 10. Redefining primary key<br />Now, let us take a step back and redefine the primary key that we have learnt already.<br />The definition that we learnt was:<br />A Primary key is a field which lets us to uniquely identify a record in a table.<br />This definition may sound intuitive, but in real word cases, a single field may not be enough to identify a record in a table. In such cases, a collection (two or more) of fields are used as a primary key. Consider the following example of a hospital’s patient table:<br />
  11. 11. Redefining primary key<br />In this case, it would be possible to locate a record uniquely only with information of more than one field values. Example:<br />PatientID, Date of Admisssion and Time of Admission can be combined to form a primary key. The SQL Command to do this is via a constraint:<br />create table &lt;tableName&gt; (&lt;field names&gt;,…, constraint &lt;constraintName&gt; primarykey(&lt;filedName1&gt;,&lt;fieldName2&gt;,..) );<br />Example: create table Patients(patientidvarchar(10), patientnamevarchar(20), dateofadmission date, timeofadmission time, issue varchar(20),constraint patientpkeysprimary key(patientid, dateofadmission, timeofadmission) );<br />
  12. 12. Adding primary keys to existing tables<br />The SQL Server allows primary keys to be added to an existing table. <br />Syntax:<br />ALTER TABLE &lt;tableName&gt; <br /> ADD CONSTRAINT &lt;constraintName&gt;<br /> PRIMARY KEY (&lt;FieldName&gt;);<br />Example: Consider the patient database. The command to define the primary key after creating the table is:<br />Alter table Patients add constraint patientpkeysprimary key(patientid, dateofadmission, timeofadmission) );<br />
  13. 13. Foreign Keys<br />Foreign Key:<br />A Foreign key is a field/attribute in one table which is used as a primary key in another table<br />Eg: Consider a dream database<br />Every foreign key value must be present as a primary key in the referenced table.<br />Eg: A dream number ‘3’ isn’t possible in ‘Luck Table’ unless such a dream number exists in the ‘Dream Table’<br />Dream Table<br />Notice that foreign key entries can repeat (where-as primary key entries can’t!)<br />Foreign Key<br />Primary Key<br />Luck Table<br />Refer-ences<br />
  14. 14. Foreign Keys<br />In SQL Server, a foreign key is created as follows:<br />Create table &lt;Table2_Name&gt; (&lt;filedName1&gt; foreign key references &lt;Table1_Name&gt; (Table1FieldName));<br />In the example that we just considered, the dreamNumberof the Luck Table is using the value of the primary Key dreamNumberfrom Dream Table. Hence, it is a foreign Key. The SQL statement to effect this is:<br />Create table lucktable (dreamnumberintforeign key references dream(dreamnumber), luck varchar(15), predictor varchar(20));<br />NOTE: It is essential to create the table which is being referenced by the foreign key before creating the foreign key itself.<br />
  15. 15. Adding foreign keys to existing tables<br />The SQL Server allows foreign keys to be added to an existing table. <br />Syntax:<br />ALTER TABLE &lt;tableName&gt;<br /> ADD FOREIGN KEY (&lt;fieldName&gt;) <br /> REFERENCES &lt;referencedTable&gt;(&lt;referencedFieldName&gt;);<br />Example: Table B has an attribute B1. B1 references to the primary key A1 of table A. Now, the command to add B1 as a foreign key of B is as follows:<br />Table A<br />Primary Key of A<br />Table B<br />Field A1<br />Foreign Key of B<br />Field B1<br />&lt;&lt; B1 references A1 <br />Alter table B add foreign key(B1) references A(A1) ;<br />
  16. 16. Not Null Constraint<br />In feeding data into a database, it lies with the user to feed data for a field or assign a null value. But sometimes, the value of the field may be highly valuable for data processing. In such places, we may prevent a ‘null’ from being assigned to a field by using the not null constraint. <br />Eg: Consider a customer table. Here, the name of the customer is very important. So, it can be made to be not null.<br />Note: CutomerID being a primary key, inherently, can never be null.<br />Create table customer(customeridint primary key, name varchar(10) not null, address varchar(30));<br />An attempt to insert a null value into a not-null field, will be flagged as an error.<br />
  17. 17. Unique Constraint<br />In SQL Server, it is possible to designate a field to be unique, .i.e., the field will not accept duplicate values. <br />Primary keys are inherently unique in nature.<br />Eg: Consider a customer table. Here, the credit card number of the customers will be unique.<br />Create table customer(customeridint primary key, name varchar(10) not null, creditcardnumbervarchar(20) unique);<br />An attempt to insert a credit card number that already exists in the table will be flagged as an error.<br />
  18. 18. Check Constraint<br />A Check constraint is used to specify some rules for the data that can be stored for a field.<br />A Check constraint can be defined while creating a table (with create table) or can be added to an existing table.<br />While adding a check constraint to an existing table, the data that the table holds is checked against the constraint being defined. To prevent this checking, the nocheck command can be used.<br />A Check constraint can be defined on a single column(field) or across multiple columns of the same table. <br />fieldname can take values between 1 and 100. eg: 1,2,3,…,99,100<br />SQL Syntax:<br />Create table &lt;tableName&gt; (&lt;fieldname&gt; check (fieldname &gt; 100) );<br />Create table &lt;tableName&gt; (&lt;fieldname&gt; check (fieldname between 1 and 100) );<br />Create table &lt;tableName&gt; (&lt;fieldname&gt; check (fieldname like ‘[0-9][0-9][a-z][A-Z]’) );<br />[0-9]: Denotes a single value in the range specified<br />
  19. 19. Adding Check Constraint to an existing table<br />It is possible to add check constraints to existing tables using the alter table command:<br />Syntax:<br />ALTER TABLE &lt;tableName&gt;<br /> ADD CHECK (&lt;fieldName&gt; &lt;condition&gt;);<br />Example:<br />ALTER TABLE student<br /> ADD CHECK (cgpa Between 0 and 10 );<br />
  20. 20. Summary<br />7. Customizing your database:<br /><ul><li>Managing Data integrity
  21. 21. Primary Keys
  22. 22. Definition
  23. 23. Adding primary key while creating table
  24. 24. Adding primary key to existing tables
  25. 25. Multiple field – primary key
  26. 26. Foreign Keys
  27. 27. Definition
  28. 28. Adding foreign key while creating table
  29. 29. Adding foreign key to existing table
  30. 30. Not Null Constraint
  31. 31. Unique Constraint
  32. 32. Check Constraint</li></li></ul><li>Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at www.dataminingtools.net<br />

×