Monday 25 June 2012

Difference bettwen DBMS and RDBMS and about KEY

DBMS



Transaction with TRY-Catch

BEGIN TRY
BEGIN TRANSACTION transABC

INSERT INTO TABLEA (col1,col2,col3) VALUES ('a','b','c')
INSERT INTO TABLEB (col1,col2,col3) VALUES ('a','b','c')

UPDATE TABLEA SET col2='abcde' WHERE col1 = 'a'

COMMIT TRANSACTION transABC
END TRY

BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION transABC --RollBack in case of Error

-- Raise an error with the details of the exception
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity INT
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()

RAISERROR(@ErrMsg, @ErrSeverity, 1)

END CATCH
//////////
DBMS vs. RDBMS
• Relationship among tables is maintained in a RDBMS whereas this not the case DBMS as it is used to manage the database.
• DBMS accepts the ‘flat file’ data that means there is no relation among different data whereas RDBMS does not accepts this type of design.
• DBMS is used for simpler business applications whereas RDBMS is used for more complex applications.
• Although the foreign key concept is supported by both DBMS and RDBMS but its only RDBMS that enforces the rules.
• RDBMS solution is required by large sets of data whereas small sets of data can be managed by DBMS.

///////////////////////////////////////////////////////////////////

A key is a single or combination of multiple fields in a table. Its is used to fetch or retrieve records/data-rows from data table according to the condition/requirement. Keys are also used to create relationship among different database tables or views.
Types of SQL Keys
We have following types of keys in SQL which are used to fetch records from tables and to make relationship among tables or views.
1.             Super Key
Super key is a set of one or more than one keys that can be used to identify a record uniquely in a table.Example : Primary key, Unique key, Alternate key are subset of Super Keys.
2.            Candidate Key
A Candidate Key is a set of one or more fields/columns that can identify a record uniquely in a table. There can be multiple Candidate Keys in one table. Each Candidate Key can work as Primary Key.
Example: In below diagram ID, RollNo and EnrollNo are Candidate Keys since all these three fields can be work as Primary Key.
3.            Primary Key
Primary key is a set of one or more fields/columns of a table that uniquely identify a record in database table. It can not accept null, duplicate values. Only one Candidate Key can be Primary Key.
4.            Alternate key
A Alternate key is a key that can be work as a primary key. Basically it is a candidate key that currently is not primary key.
Example: In below diagram RollNo and EnrollNo becomes Alternate Keys when we define ID as Primary Key.
5.            Composite/Compound Key
Composite Key is a combination of more than one fields/columns of a table. It can be a Candidate key, Primary key.
6.            Unique Key
Uniquekey is a set of one or more fields/columns of a table that uniquely identify a record in database table. It is like Primary key but it can accept only one null value and it can not have duplicate values
7.            Foreign Key
Foreign Key is a field in database table that is Primary key in another table. It can accept multiple null, duplicate values.
Example : We can have a DeptID column in the Employee table which is pointing to DeptID column in a department table where it a primary key.
Defined Keys -
CREATE TABLE Department1
 (
  DeptID int PRIMARY KEY,
   Name varchar (50) NOT NULL,
  Address varchar (200) NOT NULL, )
 CREATE TABLE Student
 (
   ID int PRIMARY KEY,
   RollNo varchar(10) NOT NULL,
 Name varchar(50) NOT NULL,
 EnrollNo varchar(50) UNIQUE,
Address varchar(200) NOT NULL,
 DeptID int FOREIGN KEY REFERENCES Department1(DeptID))


Sum Function with Group By Clause 
                                      Table Name=S3
 
                                              Table Name=S4
 
 Inner Join for Sum of Mark
SELECT        s.Sid, s.SName, e.TOT
FROM            S3 AS s INNER JOIN
                             (SELECT        Sid, SUM(Mark) AS TOT
                               FROM            S4
                               GROUP BY Sid) AS e ON e.Sid = s.Sid 
 Inner Join for Max Mark 
SELECT        s.Sid, s.SName, e.ww
FROM            S3 AS s INNER JOIN
                             (SELECT        Sid, MAX(Mark) AS ww
                               FROM            S4 AS b
                               GROUP BY Sid) AS e ON e.Sid = s.Sid

No comments:

Post a Comment