Anda di halaman 1dari 21

DATABASE MANAGEMENT SYSTEM

A Database Management System (DBMS) is a software package with computer programs that control the creation, maintenance, and the use of a database. It allows organizations to conveniently develop databases for various applications by database administrators (DBAs) and other specialists. A database is an integrated collection of data records, files, and other database objects needed by an application. A DBMS allows different user application programs to concurrently access the same database. DBMSs may use a variety of database models, such as the relational model or object model, to conveniently describe and support applications. It typically supports query languages, which are in fact high-level programming languages, dedicated database languages that considerably simplify writing database application programs. Database languages also simplify the database organization as well as retrieving and presenting information from it. A DBMS provides facilities for controlling data access, enforcing data integrity, managing concurrency control, recovering the database after failures and restoring it from backup files, as well as maintaining database security.

COMPONENTS OF DATABASE SYSTEM


A database system is composed of four components; Data Hardware Software Users which coordinate with each other to form an effective database system.

1. Data - It is a very important component of the database system. Most of the organizations generate, store and process 1arge amount of data. The data acts a bridge between the machine parts i.e. hardware and software and the users which directly access it or access it through some application programs. Data may be of different types. User Data - It consists of a table(s) of data called Relation(s) where Column(s) are called fields of attributes and rows are called Records for tables. A Relation must be structured properly. Metadata - A description of the structure of the database is known as Metadata. It basically means "data about data". System Tables store the Metadata which includes. - Number of Tables and Table Names - Number of fields and field Names - Primary Key Fields Application Metadata - It stores the structure and format of Queries, reports and other applications components. ' 2. Hardware - The hardware consists of the secondary storage devices such as magnetic disks (hard disk, zip disk, floppy disks), optical disks (CD-ROM), magnetic tapes etc. on which data is stored together with the Input/Output devices (mouse, keyboard, printers), processors, main memory etc. which are used for storing and retrieving the data in a fast and efficient manner. Since database can range from those of a single user with a desktop computer to those on mainframe computers with thousand of users, therefore proper care should be taken for choosing appropriate hardware devices for a required database. 3. Software - The Software part consists of DBMS which acts as a bridge between the user and the database or in other words, software that interacts with the users, application programs, and database and files system of a particular storage media (hard disk, magnetic tapes etc.) to insert, update, delete and retrieve data. For performing these operations such as insertion, deletion and updation we can either use the Query Languages like SQL, QUEL, Gupta SQL or application softwares such as Visual 3asic, Developer etc. 4. Users - Users are those persons who need the information from the database to carry out their primary business responsibilities i.e. Personnel, Staff, Clerical, Managers, Executives etc. On the basis of the job and requirements made by them they are provided access to the database totally or partially.

DATA INDEPENDENCE
Data independence is the type of data transparency that matters for a centralized DBMS. It refers to the immunity of user applications to make changes in the definition and organization of data. Physical data independence deals with hiding the details of the storage structure from user applications. The application should not be involved with these issues, since there is no difference in the operation carried out against the data. The data independence and operation independence together gives the feature of data abstraction. There are two levels of data independence.

Logical data independence: The ability to change the logical (conceptual) schema without changing the External schema (User View) is called logical data independence. For example, the addition or removal of new entities, attributes, or relationships to the conceptual schema should be possible without having to change existing external schemas or having to rewrite existing application programs. Physical data independence: The ability to change the physical schema without changing the logical schema is called physical data independence. For example, a change to the internal schema, such as using different file organization or storage structures, storage devices, or indexing strategy, should be possible without having to change the conceptual or external schemas. THE THREE-SCHEMA ARCHITECTURE: The goal of the three-schema architecture is to separate the user applications and the physical database. In this architecture, schemas can be defined at 3 levels : 1. Internal level or Internal schema : Describes the physical storage structure of the database. The internal schema uses a physical data model and describes the complete details of data storage and access paths for the database. 2. Conceptual level or Conceptual schema : Describes the structure of the whole database for a community of users. It hides the details of physical storage structures and concentrates on describing entities, data types, relationships, user operations, and constraints. Implementation data model can be used at this level. 3. External level or External schema : It includes a number of external schemas or user views. Each external schema describes the part of the database that a particular user is interested in and hides the rest of the database from user. Implementation data model can be used at this level.

IMPLEMETATION OF DDL AND DML STATEMENTS


create database employee_management use employee_management create table emp_details ( empno int, empname varchar(20), empdesig varchar(20), doj datetime, empaddress varchar(30), deptid int ) create table dept_detail ( deptid int, deptname varchar(30) ) create table salary_details ( empno int, leaves int,

presents int, hra numeric, pf numeric , netsalary numeric ) insert into emp_details values(23,'H','Manager','12-Jan-2010','122/B.R.S. Nagar',10) insert into emp_details values(21,'A','Salesperson','12-June-2010','12/Sarabha Nagar',15) insert into emp_details values(12,'B','Accountant','11-March-2010','102/Civil Lines',20) insert into emp_details values(15,'I','Executive','10-Jan-2010','23/B.R.S. Nagar',12) insert into emp_details values(33,'K','Manager','17-Jan-2011','11/Sarabha Nagar',5) insert into emp_details(empno,empname,empdesig,doj,empaddress,deptid) values(1,'Komal','Manager','14-Jan-2011','154/Sarabha Nagar',5) insert into emp_details(empno,empname,empdesig,doj,empaddress,deptid) values(11,'Kiran','Salesperson','13-Jan-2011','154/Civil Lines',15) insert into emp_details(empno,empname,empdesig,doj,empaddress,deptid) values(51,'Aayush','Production','13-June-2011','14/Kitchlu Nagar',10) insert into emp_details(empno,empname,empdesig,doj,empaddress,deptid) values(34,'Sahil','Accountant','13-July-2011','154/B.R.S.Nagar',12)

Adding a column to the existing table


alter table emp_details add empdor datetime select * from emp_details

insert into dept_detail values(10,'Production') insert into dept_detail values(12,'Accounts') insert into dept_detail values(20,'Marketing') insert into dept_detail values(5,'Management') insert into dept_detail values(15,'Sales') select * from dept_detail

Primary Key Constarint and Not Null Constraint


alter table dept_detail alter column deptid int not null alter table dept_detail with nocheck add constraint pk primary key(deptid)

View the constarints applied on the table


sp_helpconstraint[dept_detail]

Foreign Key Constraint

alter table emp_details with nocheck add constraint fk foreign key(deptid) references dept_detail(deptid) sp_helpconstraint[emp_details]

insert into emp_details(deptid) values(22)

Violation of foreign key constraint


Error Messaage Msg 547, Level 16, State 0, Line 1 The INSERT statement conflicted with the FOREIGN KEY constraint "fk". The conflict occurred in database "employee_management", table "dbo.dept_detail", column 'deptid'. The statement has been terminated. alter table salary_details add basicpay numeric insert into salary_details(empno,leaves,presents,hra,pf) values(21,2,28,1000,2000 insert into salary_details(empno,leaves,presents,hra,pf) values(23,3,27,2000,4000 insert into salary_details(empno,leaves,presents,hra,pf) values(12,1,29,1500,2500 insert into salary_details(empno,leaves,presents,hra,pf) values(33,2,28,3000,4000 insert into salary_details(empno,leaves,presents,hra,pf) values(15,4,26,500,200 ) update salary_details set basicpay=30000 where empno=21 update salary_details set basicpay=60000 where empno=23 update salary_details set basicpay=35000 where empno=12 update salary_details set basicpay=20000 where empno=15 ) ) ) )

update salary_details set basicpay=55000 where empno=33 update salary_details set netsalary=basicpay+0.25*basicpay-0.085*pf

Check Constraint
alter table salary_details add maritalstatus varchar(4) check (maritalstatus='Single' or maritalstatus='Married')

update salary_details set maritalstatus='Bachelor' where empno=23

Violation of Check Constarint


Error Message Msg 547, Level 16, State 0, Line 1 The UPDATE statement conflicted with the CHECK constraint "CK__salary_de__marit__023D5A04". The conflict occurred in database "employee_management", table "dbo.salary_details", column 'maritalstatus'. The statement has been terminated.

Unique Constraint and Default value

create table emp (id int unique,date datetime default '12-Jan-2011') insert into emp(id) values(1) select * from emp

insert into emp values(2,'13-Jan-2011')

insert into emp(id) values(1)

Violation of Unique Key constraint

Count(*)
select count(*) from emp_details

Count(columnname)

select count(maritalstatus) from salary_details

RowCount
set rowcount 2 select * from emp_details

set rowcount 0 select * from emp_details

select * from emp_details where deptid=10

select * from emp_details where deptid=10 and empname='K'

select * from emp_details where empname like 'K%'

select * from emp_details where empname like '_o%'

select * from salary_details where netsalary between 43538 and 70000

select * from emp_details where deptid in (10,15)

select * from dept_detail order by deptid

select * from dept_detail order by deptid desc

select * from emp_details order by deptid desc,empname

create database normalized use normalized

create table client ( client_no int primary key, client_name varchar(30) ) create table clientproperty ( client_no int foreign key references client(client_no), property_no varchar(50) foreign key references property_details(property_no), rent_start datetime, rent_finish datetime primary key(client_no,property_no) ) create table owner_details ( owner_no varchar(20), owner_name varchar(30) ) alter table owner_details alter column owner_no varchar(20) not null alter table owner_details add constraint pk primary key(owner_no) create table property_details ( property_no varchar(50) primary key , padd varchar(50), owner_no varchar(20) , rent numeric ) alter table property_details add constraint fk foreign key(owner_no) references owner_details(owner_no) insert into client values(75,'Akhilesh') insert into client values(54,'Nitin Chawla') insert into property_details values('PG51','16 south City Ldh','OW104',2500) insert into property_details values('PG7','4 Rajguru Nagar Ldh','OW87',1500) insert into property_details values('PG34','23 B.R.S Nagar Ldh','OW87',2000) insert into clientproperty values(75,'PG51','23-july-2000','31-aug-2001') insert into clientproperty values(75,'PG7','14-sep-2001','3-oct-2002') insert into clientproperty values(54,'PG51','1-sep-1999','10-jun-2000') insert into clientproperty values(54,'PG34','10-oct-2000','1-nov-2001') insert into clientproperty values(54,'PG7','1-nov-2002','1-aug-2003')

insert into owner_details values('OW104','Mr. Sharma') insert into owner_details values('OW87','Mr. Shukla') select * from owner_details select * from property_details select p.property_no,p.padd,p.owner_no,owner_name,p.rent from owner_details o inner join property_details p on o.owner_no=p.owner_no

insert into owner_details values('OW100','Mr. Kumar') select p.property_no,p.padd,p.owner_no,o.owner_name,p.rent from property_details p right outer join owner_details o on p.owner_no=o.owner_no

select p.property_no,p.padd,p.owner_no,o.owner_name,p.rent from property_details p full outer join owner_details o on p.owner_no=o.owner_no

create view cross_join as ( select p.property_no,p.padd,p.owner_no,o.owner_name,p.rent from property_details p cross join owner_details o ) select * from cross_join

create table self_join(empid int,empname varchar(20),managerid int) insert into self_join values(1,'A',null) insert into self_join values(2,'B',1) insert into self_join values(3,'C',2) insert into self_join values(4,'D',3) insert into self_join values(5,'E',4) insert into self_join values(6,'f',5) insert into self_join values(7,'g',1) insert into self_join values(8,'h',2) select * from self_join

select s1.empid,s1.empname,s2.managerid from self_join s1, self_join s2 where s1.empid=s2.managerid

select owner_name from owner_details union select client_name from client;

select owner_name from owner_details intersect select client_name from client;

select owner_name from owner_details except select client_name from client;

TLSQL IF-ELSE
declare @num1 int declare @num2 int select @num1=2 select @num2=3 if(@num1>@num2) begin print 'num1 is greater than num2 with value' +convert(char(5),@num1) end else begin print 'num2 is greater than num1 with value' +convert(char(5),@num2) end

WHILE LOOP DECLARE @i INT SET @i = 1 WHILE (@i<=10) BEGIN PRINT @i SET @i = @i+1 END

PROCEDURE
CREATE PROCEDURE mtable(@n int) AS begin DECLARE @count int DECLARE @prod int SELECT @count=1 WHILE @count<=10 BEGIN select @prod=@n * @count PRINT Convert(char(3),@n)+'X '+Convert(char(3),@count)+' ='+Convert(char(3),@prod) SELECT @count=@count + 1 END end execute mtable 8

FUNCTION
CREATE FUNCTION fact (@fnum int, @f int) RETURNS int as BEGIN while @fnum>=1 begin select @f = @f * @fnum select @fnum=@fnum-1 end return @f END Go select dbo.fact(8,1) as FACTORIAL

TRIGGERS
create table products(Discount float, UnitPrice float,ProductID float) insert into Products values(210, 8000,201) insert into Products values(250, 1900,202) insert into Products values(300, 2900,203)

BEGIN TRAN DECLARE @Discount float SET @Discount = 0.02 UPDATE Products SET UnitPrice = UnitPrice - (@Discount * UnitPrice) Go -- Check results... Select ProductID, UnitPrice FROM Products ORDER BY UnitPrice Desc

Anda mungkin juga menyukai