• Like
  • Save
MS SQLSERVER:Manipulating Database
Upcoming SlideShare
Loading in...5
×

MS SQLSERVER:Manipulating Database

  • 224 views
Uploaded on

MS SQLSERVER:Manipulating Database

MS SQLSERVER:Manipulating Database

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
224
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. 4
    SQL SERVER: MANIPULATING A DATABASE
  • 2. Manipulating a Database
    HOW TO MAKE CHANGES TO THE STRUCTURE OF THE DATABASE?
    A Database table can be defined by:
    The Fields(Columns) and their properties
    Records(Rows) and their properties
    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
    Now let us see how to carry out the changes to the above defined properties of a database.
  • 3. Modifying the Columns
    The Columns or fields in a database table can be easily modified using the ‘alter table’ command, which comes under DDL(Data Definition Language)
    What are the changes that can be made to a field?
    • Add a new filed
    • 4. Edit an existing field
    • 5. Remove a field from table
    • 6. Add a constraint on the data that a field can hold (will be dealt with later)
    Now lets see how these actions can be performed in SQL Server 2008
  • 7. Add a new field
    The SQL DDL command ‘alter table…add’ is used for adding a new field
    alter table <tableName> add <field name> <field type>;
    For example, consider the following animal database used by ‘MetaZooa’ Zoo, for maintaining the details of the animals in the zoo.
  • 8. Add a new field
    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.
  • 9. Add a new field
    Steps to add a new field
    Considering the previous example, the command will be:
    alter table MetaZooDBaddZoo int;
    Run this command using ‘Go’ command.
  • 10. Modifying an existing field
    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.
    The Command is:
    alter table MetaZooDBalter column Zoo varchar(15);
    Run this command using ‘Go’ command.
    But we might have complications…
  • 11. Modifying an existing field
    Problem:
    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
    Compatibility Chart
    Source
    Destination
    Source
    Varchar
    (Strings)
    Integers (Int)
    Varchar (Strings)
    Integers (Int)
    Legend:
    Conversion Allowed:
    Conversion NOT allowed:
    Decimals (Float)
    Date and Time
  • 12. Modifying an existing field
    Source
    Destination
    Source
    Varchar (Strings)
    Decimals (Float)
    Date and Time
    Integers (Int)
    Decimals (Float)
    Date and Time
  • 13. Remove a column from table
    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:
    alter table <table_name> drop column <column_name>
    For example, consider the follwingDreamTable. Suppose the User wishes to remove the DreamType field…
    alter table DreamTable drop column DreamType
  • 14. Deleting a Row
    A Record is a row in a table. For example, consider a fish database maintained by a ‘Eden-Lake Ecology farm’.
    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.
    Now let us see the command
  • 15. Deleting a Row
    Deleting a row:
    For deleting a row, it must be identified using a ‘distinguishing’ attribute which lets the computer tell it apart from other records.
    For the above example, the record can be identified using:
    The Primary key (FishID)
    Or any other special attribute (like Fish Name).
    But in general, always use the Primary Key, as it is best suited for uniquely identifying a record in a database table.
    NOTE: Strings/Date data-types must be encapsulated within single quotes
    delete from <table_name> where <condition>
    delete from EdenFishTable where FishID = ‘23H’
  • 16. Summary
    4. Manipulating a Database
  • Visit more self help tutorials
    Pick a tutorial of your choice and browse through it at your own pace.
    The tutorials section is free, self-guiding and will not involve any additional support.
    Visit us at www.dataminingtools.net