Beruflich Dokumente
Kultur Dokumente
A database management system (DBMS) is a collection of interrelated data and a set of programs to access those data. The collection of data, usually referred to a database , contains information relevant to an enterprise.
1. Oracle Oracle 8i, Oracle9i, Oracle 10g, Oracle 11g 2. Microsoft SQL Server 3. IBM DB2/DB2UDB 4. Informix 5. Sybase 6. MySQL 7. Ingress 8. PostgreSQL For Personal Computers
1. Microsoft Access 2. FoxPro Instances and Schemas 3. dBase Schema: The overall design of the database is called the database schema. Schemas are changed infrequently, if at all.
Data Models
Instances: Databases change over time as information is inserted and deleted. A data model is a information collection of conc eptual tools for at describing data, data is The collection of stored in the database a particular moment relationship, data semantics and consistency constraints. called an instance of the databas e. Relational Model: One of the most important data models is relational model. It Relation schema: uses a collection of tables to represent both data and relationships among those data. Branch_schema = (branch_name, branch_city, assets) (customer_name, customer_street, customer_city) Customer_schema = Each table has multiple columns, and each column has a unique name. Loan_schema = (loan_no., branch_name, amount) Borrower_schema = (customer_name, loan_no.) The relational model is an example of a record-based model. This is because Account_schema = (account_no., branch_name, balance) the database is structured in fixed-format records of several types. Each table Depositor_schema = (customer_name, account_no.) contains records of a particular type. Each record type defines a fixed no. of fields,instance: or attributes. The columns of the table correspond to the attributes of Relation the record type. The contents of a relation instance may change with time as the relation is The relational model the most widely used data model and a vast majority updated. We simply sayis relation to mean relation instance. of current database systems are based on the relational model.
Keys
The records within a given relation are distinguished in terms of its attributes. That is, the value of the attributes of a record must be such that they can uniquely identify the record. In other words, no two records in a relation are allowed to have exactly the same value for all the attributes. 1. Superkey Definition: A superkey is a set of one or more attributes that, taken collectively, allows identifying uniquely a record in the relation. A superkey may contain extraneous attributes. In Branch_schema {branch_name}, {branch_name, branch_city}, {all attributes} are all superkeys. But, {branch_city} is not a superkey. In Account_schema {account_no} is a superkey, Any superset of a superkey is also a superkey. 2. Candidate Key Definition: The superkey, for which no proper subset is a superkey, is a candidate key. So the minimul superkeys are called candidate keys. It is possible that several distinct sets of attributes could serve as a candidate key. In Branch_schema {branch_name} is a candidate key but {branch_name, branch_city} is not a candidate key, because {branch_name} is a subset of {branch_name, branch_city} and {branch_name} itself is a superkey. 3. Primary Key Definition: The primary key is to denote a candidate key that is chosen by the database designer as the principal means of identifying tuples within a relation. In Loan_schema {loan_no} In Branch_schema {branch_name} So, Primary key Candidate Key Super key. A key (primary, candidate or super) is the property of the entire relation, rather than of the individual tuples.
4. Foreign key Definition: A relation schema R 2 may include among its attributes the primary key of another relation schema R . This attribute is called a foreign key from R , 1 2 referencing R 1 . The relation r 2 is also called the referencing relation of the foreign key dependency and r is called the referenced relation of the foreign 1
key. The attribute branc h_name in Account-schema Account_schema referencing Branch_schema . branch_name branch_city assets Mirpur Dhaka 7100000 Kalabagan Dhaka 9000000 Satmatha Bogra 400000 Laxipur Rajshahi 3700000 Branch relation loan_number branch_name amount L-11 Satmatha 900 L-14 Kalabagan 1500 L-15 Mirpur 1500 L-16 Laxipur 1300 L-17 Kalabagan 1000 Loan relation PK_Branch = branch_name FK in Loan = branch_name , PK_Loan = loan_number is a foreign key from
Relationships
There are four types of relationships exist between two relations: 1. One to one Relationship between basic and detail information of something
2. One to many Indicates one record of one relation is linked with one or many records of another relation. Relationship between Branch and Account, Branch and Loan 3. Many to one - Indicates many or one record of one relation is linked with one record of another relation. Relationship between Account and Branch or Loan and Branch. 4. Many to many - Indicates one or many record/s of one relation is linked with one or many record/s of another relation. Relationship between Account and Customer, Loan and Customer.
Schema Diagram
A database schema, along with primary key and foreign key dependencies, can be depicted pictorially by schema diagram. Each relation appears as a box, with the attributes listed inside it and the relation name above it. If there are primary key attributes, a horizontal line crosses the box, with primary key attributes listed above the line. Foreign key dependencies appear as arrows from the foreign key attributes of the referencing relation to the primary key of the referenced relation.
Database Languages
SQL (Structured Query Language) SQL is the most influential commercially marketed query language. This is not just query language; it can define the structure of the data, modify data in the database and specify security constraints.
Parts of SQL: Data Definition Language (DDL): The SQL DDL provides commands for defining relation schemas, deleting relation schemas, modifying relation schemas and creating indices.
Interactive Data Manipulation Language (DML): DML is a language that enables users to access or manipulate data as organized by appropriate data model. The types of accesses are: i) The retrieval of information stored in the database - Query ii) The insertion of new information into the database - insert iii) The deletion of information from the database - delete iv) The modification of information stored in the database update
Integrity: The SQL DDL includes commands for specifying integrity constraints that the data stored in the database must satisfy. Updates that violate integrity constraints are disallowed. The integrity constraints declaration (super key, candidate key or unique key, primary key), form of a relationship (one to one, one to many, many to one and many to many), domain constraints, referential integrity (foreign key), assertion, trigger, functional dependencies Authorization: The SQL DDL includes commands for specifying access rights to schemas, relations. Permissions on schema may be: resource, index, alter, drop and permissions on data may be: select, insert, update, delete etc. A user may be assigned all, none or a combination of these types of authorization.
are: key
float (n): A floating-point, with user-specified precision of at least n digits. date : A calendar date, containing four digit year, month, and day of the month. time : A time of the day in hours, minutes, and seconds.
Loan Table create table loan (loan_no char(5), branch_name varchar2(15), amount number(10,2) not null, constraint l_pk primary key (loan_no), constraint l_fk foreign key (branch_name) references branch (branch_name), constraint l_chk1 check (amount >= 0), constraint l_chk2 check (loan_no like L-%)); Depositor Table create table depositor (customer_name varchar2(20), account_no char(5), constraint de_pk primary key (customer_name, account_no), constraint de_fk1 foreign key (customer_name) references customer (customer_name), constraint de_fk2 foreign key (account_no) references account (account_no)); Borrower Table create table borrower (customer_name varchar2(20), loan_no char(5), constraint bo_pk primary key (customer_name, loan_no), constraint bo_fk1 foreign key (customer_name) references customer (customer_name), constraint bo_fk2 foreign key (loan_no) references loan (loan_no));
The basic structure of an SQL expression consists of three clauses: 1. select It is used to list the attributes desired in the result of the query. 2. from It lists the relations to be scanned in the evaluation of the expression. 3. where It consists of a predicate (condition) involving attributes of the relations that appear in the from clause.
The asterisk * symbol can be used to denote all attributes select * from loan The select clause may also contain arithmetic expressions involving the operators +, -, * and / operating on constants or attributes of tuples. select loan_number, amount * 100 from loan
SQL uses the logical connectives and , or and not in where clause. The operands of the logical connectives can be expressions involving the comparison operators <, <=, >, >=, = and <>. SQL allows us to use comparison operators to compare strings and arithmetic expressions. SQL includes a between comparison to simplify where clause that specify <= and >=. It is also possible to use not between operator. b) Find the loan number of those loans with loan amounts between 1,500 and 2,000. select loan_number from loan where amount between 1500 and 2000 or select loan_number from loan where amount >= 1500 and amount <=2000
6. Set Operations
SQL set operations are: 1. union, 2. intersect and 3. except/minus The relations participating in the set operations in SQL must be compatible; that is, they must have the same set of attributes.
Unlike the select clause, the union operation automatically eliminates duplicates. If we want to retain all duplicates:
b) Find all customers who have a loan but no account at the bank. (select customer_name minus (select customer_name from borrower) from depositor)
7. Aggregate Functions
Aggregate functions are functions that take a collection (a set or multiset) of values as input and return a single value. SQL offers five built-in aggregate functions: 1. Average: avg 2. Minimum: min 3. Maximum: max 4. Total: sum 5. Count: count The input to sum and avg must be a collection of numbers, but the other operators can operate on collection of non-numeric data types, such as string, as well. Aggregate functions on a single set of tuples :
a) Find the average account balance. select avg (balance) from account b) Find the total account balance of Mirpur branch. select sum (balance) from account where branch_name = Mirpur
Aggregate functions on
c) Find the average account balance, maximum account balance at each branch. select branch_name, avg (balance), max (balance) from account group by branch_name Retaining duplicates is important in computing sum or average. There are some cases where we must eliminate duplicate before computing an aggregate function.
d) Find branch name and average balance where average balance is greater than 500. select branch_name, avg (balance) from account group by branch_name having avg (balance) > 500 The aggregate function tuples in a relation. count is frequently used to count the number of
e) Count the number of tuples in account relation. select count (*) from account SQL does not allow the use of distinct with count (*). distinct can be used with min and max , but result does not change.
1. Insertion
To insert data into a relation, we specify a tuple to be inserted. Attribute values for inserted tuples must be members of the attribute's domain. Similarly, tuples inserted must be of the correct arity. a) Insert a tuple for a customer who has Tk 1200 in account A-9372 at the Mirpur branch insert into account values (A-9372, Mirpur, 1200)
The above insertion is identical with: insert into account (account_number, branch_name, balance) values (A-9372, Mirpur, 1200) It is possible for inserted tuples to be given values on only some attributes of the schema. The remaining attributes are assigned a null value denoted by null. We can prohibit the insertion of null values using the SQL DDL. insert into account values (A-9372, null, 1200)
2. Updates
Updating allows us to change some values in a tuple without necessarily changing all. Like insert and delete, we can choose the tuples to be updated using a query. a) Increase all balances by 5 percent. update account set balance = balance * 1.05 This statement is applied to every tuple in account. b) Give 6 percent interest for all accounts with balance over 10000 and 5 percent for the rest. update account set balance = balance * 1.06 where balance > 10,000 update account set balance = balance * 1.05 where balance <= 10,000 Here the order of the two operations is important. (Why?) c) Pay 5% interest on account whose balance is greater than average update account set balance = balance * 1.05 where balance > ( select avg (balance) from account)
3. Deletion
A delete request is expressed in much the same way as a query. Instead of displaying, the selected tuples are removed from the database. We can only delete whole tuples; we cannot delete values on only particular attributes. A deletion in SQL is of the form delete from r where P where P represents a predicate and r repr esents a relation. The delete statement first finds all tuples t in r for which P(t) is true and then deletes them form r. If the where clause is omitted, all tuples are deleted. A delete command operates on only one relation. If we want to delete tuples from several relations, we must use one delete command for each relation.
a) Delete all of Smith's account records delete from depositor where customer_name=Smith b) Delete all account tuples in the Mirpur branch delete from account where branch_name=Mirpur c) Delete all loans with loan amounts between 1300 and 1500. delete from loan where amount between 1300 and 1500 d) Delete all accounts at branches located in Dhaka delete from account where branch_name in (select branch_name from branch where branch_city = Dhaka)
We may only delete tuples from one relation at a time, but we may reference any number of relations in a select-from-where clause embedded in the where clause of a delete . e) Delete the records of all accounts with balances below the average delete from account where balance < ( select avg (balance) from account)
Sample Question:
1. 2. 3. 4. 5. 6. What is DBMS? Name some of the DBMS systems. What is the difference between schema and instance? Define superkey, candidate key and primary key. With example define foreign key. How many relationships exist in relational model? What is schema diagram? Draw a schema diagram based on a given relational database. 7. What is SQL? What are the basic parts of SQL? 8. What can be done using DML? 9. What is query? What is the basic format of a query?