View stunning SlideShares in full-screen with the new iOS app!Introducing SlideShare for AndroidExplore all your favorite topics in the SlideShare appGet the SlideShare app to Save for Later — even offline
View stunning SlideShares in full-screen with the new Android app!View stunning SlideShares in full-screen with the new iOS app!
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.
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 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
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.
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.
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.
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…
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
Modifying an existing field Source Destination Source Varchar (Strings) Decimals (Float) Date and Time Integers (Int) Decimals (Float) Date and Time
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
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
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’
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