4<br />SQL SERVER: MANIPULATING A DATABASE<br />
Manipulating a Database<br />HOW TO MAKE CHANGES TO THE STRUCTURE OF THE DATABASE?<br />A Database table can be defined by...
Modifying the Columns<br />The Columns or fields in a database table can be easily modified using the ‘alter table’ comman...
Edit an existing field
Remove a field from table
Add a constraint on the data that a field can hold (will be dealt with later)</li></ul>Now lets see how these actions can ...
Add a new field<br />The SQL DDL command ‘alter table…add’ is used for adding a new field<br />alter table &lt;tableName&g...
Add a new field<br />Now, suppose the MetaZooa corp. decides to construct another Zoo beside the old one and divide the an...
Add a new field<br />Steps to add a new field<br />Considering the previous example, the command will be:<br />alter table...
Modifying an existing field<br />Now, suppose MetaZooa decides to that ‘Zoo’ contains the Zoo Name rather than the number ...
Modifying an existing field<br />Problem:<br />The Data contained in the field that is to be modified must be compatible w...
Modifying an existing field<br />Source<br />Destination<br />Source<br />Varchar (Strings)<br />Decimals (Float)<br />Dat...
Remove a column from table<br />The DDL command ‘alter table’ in conjunction with the ‘drop column’ is used to delete a co...
Deleting a Row<br />A Record is a row in a table. For example, consider a fish database maintained by a ‘Eden-Lake Ecology...
Upcoming SlideShare
Loading in...5
×

MS SQL SERVER: Manipulating Database

289

Published on

MS SQL SERVER: Manipulating Database

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

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

No notes for slide

Transcript of "MS SQL SERVER: Manipulating Database"

  1. 1. 4<br />SQL SERVER: MANIPULATING A DATABASE<br />
  2. 2. Manipulating a Database<br />HOW TO MAKE CHANGES TO THE STRUCTURE OF THE DATABASE?<br />A Database table can be defined by:<br />The Fields(Columns) and their properties<br />Records(Rows) and their properties<br />It is important to understand clearly that the records are user-entries into a database table. Fields are created during the design phase of a database<br />Now let us see how to carry out the changes to the above defined properties of a database.<br />
  3. 3. Modifying the Columns<br />The Columns or fields in a database table can be easily modified using the ‘alter table’ command, which comes under DDL(Data Definition Language)<br />What are the changes that can be made to a field?<br /><ul><li>Add a new filed
  4. 4. Edit an existing field
  5. 5. Remove a field from table
  6. 6. Add a constraint on the data that a field can hold (will be dealt with later)</li></ul>Now lets see how these actions can be performed in SQL Server 2008<br />
  7. 7. Add a new field<br />The SQL DDL command ‘alter table…add’ is used for adding a new field<br />alter table &lt;tableName&gt; add &lt;field name&gt; &lt;field type&gt;;<br />For example, consider the following animal database used by ‘MetaZooa’ Zoo, for maintaining the details of the animals in the zoo.<br />
  8. 8. Add a new field<br />Now, suppose the MetaZooa corp. decides to construct another Zoo beside the old one and divide the animals between the zoos. Then, the database of MetaZooa must contain details of the zoo-number also. <br />
  9. 9. Add a new field<br />Steps to add a new field<br />Considering the previous example, the command will be:<br />alter table MetaZooDBaddZoo int;<br />Run this command using ‘Go’ command.<br />
  10. 10. Modifying an existing field<br />Now, suppose MetaZooa decides to that ‘Zoo’ contains the Zoo Name rather than the number ‘1’ or ‘2’, we need to modify the field type.<br />The Command is:<br />alter table MetaZooDBalter column Zoo varchar(15);<br />Run this command using ‘Go’ command.<br />But we might have complications…<br />
  11. 11. Modifying an existing field<br />Problem:<br />The Data contained in the field that is to be modified must be compatible with the destination data-type. Other-wise, conversion cannot be carried out<br />Compatibility Chart<br />Source<br />Destination<br />Source<br />Varchar<br />(Strings)<br />Integers (Int)<br />Varchar (Strings)<br />Integers (Int)<br />Legend:<br />Conversion Allowed:<br />Conversion NOT allowed:<br />Decimals (Float)<br />Date and Time<br />
  12. 12. Modifying an existing field<br />Source<br />Destination<br />Source<br />Varchar (Strings)<br />Decimals (Float)<br />Date and Time<br />Integers (Int)<br />Decimals (Float)<br />Date and Time<br />
  13. 13. Remove a column from table<br />The DDL command ‘alter table’ in conjunction with the ‘drop column’ is used to delete a column/field from a user table. The Syntax is:<br />alter table &lt;table_name&gt; drop column &lt;column_name&gt;<br />For example, consider the follwingDreamTable. Suppose the User wishes to remove the DreamType field… <br />alter table DreamTable drop column DreamType<br />
  14. 14. Deleting a Row<br />A Record is a row in a table. For example, consider a fish database maintained by a ‘Eden-Lake Ecology farm’. <br />Now, suppose the people go on a shark-mania and the sharp population vanished from the lake, it is meaningful for the Eco farm to remove its entry from the database.<br />Now let us see the command<br />
  15. 15. Deleting a Row<br />Deleting a row:<br />For deleting a row, it must be identified using a ‘distinguishing’ attribute which lets the computer tell it apart from other records.<br />For the above example, the record can be identified using:<br />The Primary key (FishID)<br />Or any other special attribute (like Fish Name).<br />But in general, always use the Primary Key, as it is best suited for uniquely identifying a record in a database table.<br />NOTE: Strings/Date data-types must be encapsulated within single quotes<br />delete from &lt;table_name&gt; where &lt;condition&gt;<br />delete from EdenFishTable where FishID = ‘23H’ <br />
  16. 16. Summary<br />4. Manipulating a Database<br /><ul><li> Adding Fields
  17. 17. Alter table… add
  18. 18. Modifying Fields
  19. 19. Alter table…alter column
  20. 20. Deleting Fields
  21. 21. Alter table.. drop
  22. 22. Removing Rows
  23. 23. Delete from..where</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 />

×