Sunday, October 30, 2011

Normalization

  • What is normalization?

It is a process of analysing the given relation schemas based on their Functional Dependencies (FDs) and primary key to achieve the properties
(1).Minimizing redundancy, (2). Minimizing insertion, deletion and update anomalies.

Normalization is the process of reducing data redundancy and maintains data integrity. This is performed by creating relationships among tables through primary and foreign keys. Normalization procedure includes 1NF, 2NF, 3NF, BCNF, and then the data is normalized.

It is set of rules that have been established to aid in the design of tables that are meant to be connected through relationships. This set of rules is known as Normalization.

  • Benefits of Normalizing your database include:
Avoiding repetitive entries
Reducing required storage space
Preventing the need to restructure existing tables to accommodate new data
Increased speed and flexibility of queries, sorts, and summaries


  • First Normal Form
In addition to breaking data up into the smallest meaningful values, tables in first normal form should not contain repetitions groups of fields

  • Second Normal Form
The second normal form states that each field in a multiple field primary key table must be directly related to the entire primary key. In other words, each non-key field should be a fact about all the fields in the primary key.


  • Third Normal Form
A non-key field should not depend on another Non-key field. The field Total is dependent on Unit price and qty.


References:
http://www.indiabix.com/technical/dbms-basics/7
http://www.techpreparation.com/mysql-interview-questions-answers1.htm
http://www.dotnetfunda.com/interview/exclusive/showcatquestion.aspx?category=38







  • Normalisation Demonstration

http://www.youtube.com/watch?v=fg7r3DgS3rA&feature=related
no data is unnecessarily duplicated







  • SQL Normalization - The Basics - 1st, 2nd, 3rd Normal Form Software Engineering Tutorial

http://www.youtube.com/watch?v=ygfikznRjpw&feature=related

a process eliminating redundancy for a database
reduces data anamolies
making OLTP updates efficient by allowing concurrency

1NF
no repeating groups
every row should be unique

eg you can't create a product column and then put nuts cake coak all together in this product column
or
you can't create product1,product2,product3 columns and the seperately put products above in these fields

2NF
first of all must be in 1NF
no partial dependencies

all non-key fields(attributes) must depend on primary key.
but non-key fields don't have to depend ONLY on primary key.

3NF
non-key fields  have to depend ONLY on primary key.
connection table(3rd table between tables)

OLAP normalizes up to 2nf means OLAP tables must comply with 1NF and 2NF but not 3NF





  • Advantages And Disadvantages Of Database Normalization



Advantages of normalization

1. Smaller database: By eliminating duplicate data, you will be able to reduce the overall size of the database.
2. Better performance:

a. Narrow tables: Having more fine-tuned tables allows your tables to have less columns and allows you to fit more records per data page.
b. Fewer indexes per table mean faster maintenance tasks such as index rebuilds.
c. Only join tables that you need.


Disadvantages of normalization

1.                  More tables to join: By spreading out your data into more tables, you increase the need to join tables.
2. Tables contain codes instead of real data: Repeated data is stored as codes rather than meaningful data. Therefore, there is always a need to go to the lookup table for the value.
3. Data model is difficult to query against: The data model is optimized for applications, not for ad hoc querying.

http://softwaretestinginterviewfaqs.wordpress.com/category/disadvantages-of-normalization/



Advantages

1) Avoids data modification (INSERT/DELETE/UPDATE) anomalies as each data item lives in One place

2) Greater flexibility in getting the expected data in atomic granular

3) Normalization is conceptually cleaner and easier to maintain and change as your needs change

4) Fewer null values and less opportunity for inconsistency

5) A better handle on database security

6) Increased storage efficiency

7) The normalization process helps maximize the use of clustered indexes, which is the most powerful and useful type of index available. As more data is separated into multiple
tables because of normalization, the more clustered indexes become available to help
speed up data access.


Disadvantages

1) Requires much more CPU, memory, and I/O to process thus normalized data gives reduced database performance

2) Requires more joins to get the desired result. A poorly-written query can bring the database down

3) Maintenance overhead. The higher the level of normalization, the greater the number of tables in the database.

http://sql-databases.blurtit.com/q362724.html





  • 1st Normal Form Requirements

The requirements to satisfy the 1st NF:

Each table has a primary key: minimal set of
attributes which can uniquely identify a record

The values in each column of a table are
atomic (No multi-value attributes allowed).

There are no repeating groups: two columns
do not store similar information in the same
table.



The requirements to satisfy the 2nd NF:

All requirements for 1st NF must be met.
Redundant data across multiple rows of a table must be moved to a separate table.
The resulting tables must be related to each other by use of foreign key.


The requirements to satisfy the 3rd NF:

All requirements for 2nd NF must be met.
Eliminate fields that do not depend on the primary key;
That is, any field that is dependent not only on the  primary key but also on another field must be moved to another table.

https://docs.google.com/viewer?a=v&q=cache:CKxApoGZTDgJ:www.cs.sjsu.edu/~lee/cs157/26FCS157-Normal_Forms_Carlos_Alvarado.ppt+&hl=en&pid=bl&srcid=ADGEEShIjKxn2PSxvC83qyRjKj3OIOhRHbwR6SrbT9bpSJQfOpVbtlMrK6oz27NEmGApjgDHIbEk3Z2D6dbhHVL8HMotwWeMXw-r0OZ000ZJtxpWa8r3d3hgpjdABya6__jtpR9ldxwT&sig=AHIEtbSNTQYm3z30HRVzmKmURIVFUi4jhQ




  • First Normal Form (1st NF)

The table cells must be of single value.
Eliminate repeating groups in individual tables.
Create a separate table for each set of related data.
Identify each set of related data with a primary key.


Do not use multiple fields in a single table to store similar data.
For example, to track an inventory item that may come from two
possible sources, an inventory record may contain fields for
Vendor Code 1 and Vendor Code 2. But what happens when you
a dd a third vendor? Adding a field is not the answer; it requires
program and table modifications and does not smoothly
accommodate a dynamic number of vendors. Instead, place all
vendor information in a separate table called Vendors, then link
inventory t o vendors with an item number key, or vendors to
inventory with a vendor code key.



Second Normal Form (2nd NF)
If it's in 1st NF and if the Primary Key is composite (multiple
columns) then any fact in that table must be a fact about the
entire composite Primary Key not just part of the Primary Key.

For example, if an inventory table has a primary key made
up of two attributes PartId and WarehouseId. Suppose the
inventory table has the warehouse address in it, since
warehouse address is a fact about WarehoseId and not
about the PartId the w arehouse address is in the wrong
table. This is in violation of the 2nd Normal Form.


Third Normal Form (3rd NF)
If it's in the 2nd NF and there are no non-key fields that
depend on attributes in the table other than the Primary Key.

Suppose in the Student table you had student birth date as an
attribute and you also had student's age. Students age depends
on the student's birth date (a fact about his/her birth date) so
3rd Normal Form is violated.

Also, a student table that contains the address of the Department of his/her major.
That address is in the wrong table, because that address should be in the
Department table. That address is not a fact about the Student
Primary Key. A violation of 3rd Normal Form.

A (non-key) fact in a table should be about the key, the whole key,
and nothing but the key.




BCNF
A table is in BCNF if it's in 3rd NF and every determinant can be used as a Primary Key.

Fourth Normal Form A table is in the 4NF if it's in BCNF and has no attribute with mutivalued dependencies.

Note that StudentId can be associated with many major as well as many activities (multivalued dependency).
Multivalued dependency lead to modification anomalies.


https://docs.google.com/viewer?a=v&q=cache:2NuHEWsb7NoJ:myweb.brooklyn.liu.edu/gnarra/database/downloads/Normalization.pdf+&hl=en&pid=bl&srcid=ADGEESjEt-GQQAGsVAqLfHJcw42ot8iruhuv9B-opTx0N7nYC8Q8c6tzeN8QH-v2XuWtpCPc5BYJ9qZolgOIMCzXQjo1TJyRwEBJLhmYaP0OtiNX84rZkqhYd0jH_Nvjooz1_J09ns1m&sig=AHIEtbR3XZpPxlh8ZGzJCUh-8Jg6IzLEig


  • Objectives of Normalization

Develop a good description of the data, its relationships and constraints

Produce a stable set of relations that

    Is a faithful model of the enterprise
    Is highly flexible
    Reduces redundancy-saves space and reduces inconsistency in data
    Is free of update, insertion and deletion anomalies

Normal Forms

    First normal form -1NF
    Second normal form-2NF
    Third normal form-3NF
    Boyce-Codd normal form-BCNF
    Fourth normal form-4NF
    Fifth normal form-5NF
    Domain/Key normal form-DKNF

Each is contained within the previous form – each has stricter rules than the previous form Objectives of Normalization

Develop a good description of the data, its relationships and constraints

Produce a stable set of relations that

    Is a faithful model of the enterprise
    Is highly flexible
    Reduces redundancy-saves space and reduces inconsistency in data
    Is free of update, insertion and deletion anomalies

Normal Forms

    First normal form -1NF
    Second normal form-2NF
    Third normal form-3NF
    Boyce-Codd normal form-BCNF
    Fourth normal form-4NF
    Fifth normal form-5NF
    Domain/Key normal form-DKNF

Each is contained within the previous form – each has stricter rules than the previous form Objectives of Normalization

http://jcsites.juniata.edu/faculty/rhodes/dbms/funcdep.html


MySQL interview questions


  • How do you start and stop MySQL on Windows?

Command prompt.net start MySQL, net stop MySQL


  • How do you start MySQL on Linux?

/etc/init.d/mysql start


  • What’s the default port for MySQL Server?

3306


  • Can you save your connection settings to a conf file?

Yes, and name it ~/.my.conf. You might want to change the permissions on the file to 600, so that it’s not readable by others


  • How do you change a password for an existing user via mysqladmin?

mysqladmin -u root -p password "newpassword"



  • Use mysqldump to create a copy of the database?

mysqldump -h mysqlhost -u username -p mydatabasename > dbdump.sql

References:
http://www.techpreparation.com/computer-interview-questions/mysql-interview-questions-answers6.htm


  • how to query date range?


mysql date range
and event_date between '2009-01-19' and '2009-01-25'
or
and event_date >='2009-01-19'
and event_date <='2009-01-25'


  • how to connect remote mysql database?

mysql -h hostIP -u username -p  --database=databasename




  • The BLOB and TEXT Types


A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. These differ only in the maximum length of the values they can hold. The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. These correspond to the four BLOB types and have the same maximum lengths and storage requirements. See Section 11.2, “Data Type Storage Requirements”.

BLOB values are treated as binary strings (byte strings). They have no character set, and sorting and comparison are based on the numeric values of the bytes in column values. TEXT values are treated as nonbinary strings (character strings). They have a character set, and values are sorted and compared based on the collation of the character set.

https://dev.mysql.com/doc/refman/5.0/en/blob.html



  • What is the difference between BLOB AND TEXT?


A BLOB is a binary large object that can hold a variable amount of data. There are four types of BLOB –
    TINYBLOB
    BLOB
    MEDIUMBLOB and
    LONGBLOB
They all differ only in the maximum length of the values they can hold.

A TEXT is a case-insensitive BLOB. The four TEXT types
    TINYTEXT
    TEXT
    MEDIUMTEXT and
    LONGTEXT
They all correspond to the four BLOB types and have the same maximum lengths and storage requirements.
The only difference between BLOB and TEXT types is that sorting and comparison is performed in case-sensitive for BLOB values and case-insensitive for TEXT values.
http://www.gointerviews.com/top-50-mysql-interview-questions-answers/#vGT8u8EJSFsxrp87.99




  • What is the difference between primary key and candidate key?

Every row of a table is identified uniquely by primary key. There is only one primary key for a table.
Primary Key is also a candidate key. By common convention, candidate key can be designated as primary and which can be used for any foreign key references.
http://www.gointerviews.com/top-50-mysql-interview-questions-answers/#vGT8u8EJSFsxrp87.99


  • What does myisamchk do?

It compress the MyISAM tables, which reduces their disk or memory usage.
http://www.gointerviews.com/top-50-mysql-interview-questions-answers/#vGT8u8EJSFsxrp87.99


  • What are federated tables?

Federated tables which allow access to the tables located on other databases on other servers.
http://www.gointerviews.com/top-50-mysql-interview-questions-answers/#vGT8u8EJSFsxrp87.99

  • What is HEAP table?

Tables that are present in the memory are called as HEAP tables.
These tables are now more commonly known as memory tables.
These memory tables never have values with data type like “BLOB” or “TEXT”.
They use indexes which make them faster.
http://careerride.com/MySQL-HEAP-table.aspx

HEAP tables are present in memory and they are used for high speed storage on temporarybasis.
BLOB or TEXT fields are not allowed
Only comparison operators can be used =, <,>, = >,=<
AUTO_INCREMENT is not supported by HEAP tables
Indexes should be NOT NUL

http://www.gointerviews.com/top-50-mysql-interview-questions-answers/#vGT8u8EJSFsxrp87.99



  • What are the different tables present in MySQL?

Total 5 types of tables are present:

    MyISAM
    Heap
    Merge
    INNO DB
    ISAM

MyISAM is the default storage engine as of MySQL .


  • What is ISAM?

ISAM  is abbreviated as Indexed Sequential Access Method.
It was developed by IBM to store and retrieve data on secondary storage systems like tapes.


  • What is InnoDB?

lnnoDB is a transaction safe storage engine developed by Innobase Oy which is a Oracle Corporation now.
http://www.gointerviews.com/top-50-mysql-interview-questions-answers/#vGT8u8EJSFsxrp87.99




  • 13. Explain advantages of InnoDB over MyISAM? 

Row-level locking, transactions, foreign key constraints and crash recovery.


  • 14. Explain advantages of MyISAM over InnoDB? 

Much more conservative approach to disk space management – each MyISAM table is stored in a separate file, which could be compressed then with myisamchk if needed. With InnoDB the tables are stored in tablespace, and not much further optimization is possible. All data except for TEXT and BLOB can occupy 8,000 bytes at most. No full text indexing is available for InnoDB. TRhe COUNT(*)s execute slower than in MyISAM due to tablespace complexity.



  • How are the tables handled in the case of MERGE engine and with partionining?

The extremely large datasets can be stored using MERGE engine or by partitioned tables.
The MERGE engine is similar to implementation of a VIEW concept for all tables with something similar to UNION ALL.
This will have quick inserts and deletes from the datasets, but not as efficient as partitions in storage aspects.


  • Are there any security flaws in MySQL that you are aware of?


The one main security flaw is in creating the database names. The database names that are starting with the word “test” are by default taken as experimental tables meant for test purposes. So it is given the access permissions so that everyone can access. This has to be taken care of while naming the databases.


  • What is the difference between mysql and mysqli interfaces?

The mysqli interface is meant for the Object oriented library access from the PHP.


  • What is the console option meant for?

The console option is used to redirect the logs and print outputs of the mysql engine to the console. If it is not used then the mysql engine redirects output to the .err log files. This can represent a considerable overhead to the server when we are dealing with busy system with huge databases.



  • How can you back up and restore the databases in MySQL?

The mysqldump is used with the < and > symbols to back up and restore the specific database. The exact usage is
shell> mysqldump –user=username –password=password databasename > backupfile.sql
Shell> mysqldump –user=username –password=password databasename < backupfile.sql
The created .sql file internally has all the statements that have to executed to create the specific database from scratch. We can also have the values altered using editors over this text formatted file before retoring it onto another machine.

http://www.totalinterviewquestions.com/2010/12/17/mysql-dba-interview-questions-and-answers-part1-425/




  • 1. Describe MySQL architecture.

Answer: MySQL has got the logical architecture as follows
A. Connection Manager
B. Query Optimizer
C. Pluggable Engines.



  • 2. What are the major differences between MySQL 5.1 and 5.5?

Answer: The Major difference I know is that the default engine was myisam in 5.1 and innodb in 5.5


  • 3. What are the different database engines available in MySQL? (explain)

Answer: Following are the highly used engines available in mysql

A. MyISAM
B. INNODB
C. Memory
D. Federated
E. CSV

mysql> show engines;
----------------+--------------+------+------------+
| FEDERATED          | NO      | Federated MySQL storage engine
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables
| MyISAM             | YES     | MyISAM storage engine
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears)
| CSV                | YES     | CSV storage engine
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables    
| ARCHIVE            | YES     | Archive storage engine
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys
| PERFORMANCE_SCHEMA | YES     | Performance Schema
                | NO           | NO   | NO         |
+--------------------+---------+---------------------

http://dba-diary.blogspot.com/2012/12/mysql-dba-interview-questions-and.html





  • 4. What are the major differences between MyISAM and InnoDB?

Answer: Following are the differences between InnoDB and MyISAM
A. MyISAM does not support transactions whereas InnoDB does.
B. InnoDB supports multi versioning concurrency control.
C. MyISAM uses table level locks and InnoDB uses row level locking.


  • 10. How do you find out slow queries in mysql?

Answer : By enabling the slow query log as follows
SET GLOBAL slow_query_log = 1;


  • 13. If the mysql server is performing slow than how to find out the process which is causing problem.

Answer : show processlist


  • 22. How to take incremental backup in MySQL?

Answer : Using percona xtrabackup


  • 26. What do you do if the data disk is full?

Answer : if the data disk is full then create a soft link and move the .frm and .idb files to the linked location.

http://dba-diary.blogspot.com/2012/12/mysql-dba-interview-questions-and.html




  •  What are ENUMs used for in MySQL?
 You can limit the possible values that go into the table.


(tested mysql 5.6 community server)
CREATE TABLE months(month ENUM ('January','February', 'March'));
INSERT INTO months VALUES ('April');
INSERT INTO months VALUES ('March');
select * from months;


CREATE TABLE Test(NY ENUM('Y','N') DEFAULT 'N',
Size ENUM('S','M','L','XL','XXL'),
Color ENUM('Black','Red','White'));
Insert into Test (NY, Size, Color) values ('Y','S','Black');
select * from Test;
Insert into Test (NY, Size, Color) values ('Y','S','yellow');


http://mysqlboss.blogspot.com/


SQL interview questions


  • In an SQL query, which built-in function is used to total numeric columns?

A) AVG
B) COUNT
C) MAX
D) MEAN
E) SUM


  • In an SQL query, which SQL keyword is used with GROUP BY to select groups meeting specified criteria?

A) AND
B) EXISTS
C) HAVING
D) IN
E) WHERE

  • How to import table using "insert" statement?

INSERT INTO TempColorTable

SELECT code,ColorValue

FROM ColorTable

  • What is the difference between a "where" clause and a "having" clause?
"Where" is a kind of restiriction statement. You use where clause to restrict all the data from DB.Where clause is using before result retrieving. But Having clause is using after retrieving the data.Having clause is a kind of filtering command.


  • What structure can you implement for the database to speed up table reads?
1] properly use indexes ( different types of indexes)
2] properly locate different DB objects across different tablespaces, files and so on.
3] create a special space (tablespace) to locate some of the data with special datatype ( for example CLOB, LOB and …)

  • What are the tradeoffs with having indexes?
1. Faster selects, slower updates.
2. Extra storage space to store indexes. Updates are slower because in addition to updating the table you have to update the index.


  • What is a "constraint"?
A constraint allows you to apply simple referential integrity checks to a table.
There are four primary types of constraints that are currently supported by SQL Server:
PRIMARY/UNIQUE - enforces uniqueness of a particular table column.
DEFAULT - specifies a default value for a column in case an insert operation does not provide one.
FOREIGN KEY - validates that every value in a column exists in a column of another table.
CHECK - checks that every value stored in a column is in some specified list.

NOT NULL is one more constraint which does not allow values in the specific column to be null. And also it the only constraint which is not a table level constraint.


  • What is a "trigger"?
Triggers are stored procedures created in order to enforce integrity rules in a database.
A trigger is executed every time a data-modification operation occurs (i.e., insert, update or delete)
A trigger is a database object directly associated with a particular table.
Basically, trigger is a set of SQL statements


3. Answer the short question below regarding this trigger.  Any syntax errors are not intended and there may have been some liberties taken with the grammar.
[10 pts]
CREATE TRIGGER LogIt
AFTER UPDATE (balance) OR INSERT OR DELETE
ON accounts
  FOR ROW
  (UPDATE accounts
    SET lastChanged = Now
    WHERE acctNo = NEW.acctNo;
  INSERT INTO accountsLog
      VALUES (NEW.acctNo, NEW.balance, OLD.balance, Now, Username);
  );



  a. If an INSERT INTO accounts (acctNo, balance) VALUES (98765,1000.00) statement is executed, explain the actions of the trigger. [3]

The INSERT is followed by an UPDATE the same table that also sets a timestamp.  The  action is then logged into accountsLog with the new account number and balance (old balance will be NULL along with the timestamp and the user who made the change.


b. If an UPDATE accounts SET balance = balance*1.005 statement is executed, explain the actions of the trigger. [4]

Every account  balance is increased by 0.5%. and for each account the updated timestamp is also applied.  The log is updated with old and new balances with time stamp.  Every account causes the trigger to fire.


c. Could this trigger loop forever since it is triggered on table accounts and it then updates table accounts?  Why or why not? [3]
NO, the trigger updates the lastChanged attribute and not the balance attribute which won’t cause a loop


jcsites.juniata.edu/faculty/rhodes/dbms/exams/mid2f13key.docx
***************************************************************
  • What is a view?
An output of a query can be stored as a view
View is a precomplied SQL query which is used to select data from one or more tables
A view is like a table but it doesn’t physically take any space.
View can also be used to restrict users from accessing the tables directly.
The trigger can e defined to execute once for the entire statement or once for every row that is inserted, updated, or deleted.


  • What are stored-procedures? And what are the advantages of using them?
Stored procedures are database objects that perform a user defined operation. A stored procedure can have a set of compound SQL statements. A stored procedure executes the SQL commands and returns the result to the client. Stored procedures are used to reduce network traffic.


References
http://quizlet.com/5386117/database-processing-chapter-2-flash-cards/
http://www.dotnetfunda.com/interview/exclusive/ShowCatQuestion.aspx?start=15&page=2&category=38
http://www.geekinterview.com/question_details/32906
http://www.techinterviews.com/sql-interview-questions-and-answers
http://www.sqltutorial.org/sqlouterjoin.aspx



  • Assume that you export a table from oracle which does not have primary key and you want to delete repeating records.



create table employee
(
 EmpId number,
 EmpName varchar2(10),
 EmpSSN varchar2(11)
);

insert into employee values(1, 'Jack', '555-55-5555');
insert into employee values (2, 'Joe', '555-56-5555');
insert into employee values (3, 'Fred', '555-57-5555');
insert into employee values (4, 'Mike', '555-58-5555');
insert into employee values (5, 'Cathy', '555-59-5555');
insert into employee values (6, 'Lisa', '555-70-5555');
insert into employee values (1, 'Jack', '555-55-5555');
insert into employee values (4, 'Mike', '555-58-5555');
insert into employee values (5, 'Cathy', '555-59-5555');
insert into employee values (6 ,'Lisa', '555-70-5555');
insert into employee values (5, 'Cathy', '555-59-5555');
insert into employee values (6, 'Lisa', '555-70-5555');


DELETE FROM employee
WHERE rowid not in
(SELECT MIN(rowid)
FROM employee
GROUP BY EmpId, EmpName, EmpSSN);

http://stackoverflow.com/questions/985384/delete-duplicate-records-from-a-sql-table-without-a-primary-key



  • What's the difference between "IN" and "EXISTS"?



IN:

Returns true if a specified value matches any value in a subquery or a list.

Exists:

Returns true if a subquery contains any rows.

http://weblogs.sqlteam.com/mladenp/archive/2007/05/18/60210.aspx



In SQL, what are the differences between EXISTS and IN when addressing sub-queries? (2
points)
⇒ Answer: EXISTS R is a condition that is true if and only if R is NOT empty. r IN R is
true if and only if r is equal to one of the values in R

webcache.googleusercontent.com/search?q=cache:8vRupC7L9OYJ:https://wiki.engr.illinois.edu/download/attachments/227743489/CS411-F2011-Final-Sol.pdf%3Fversion%3D1%26modificationDate%3D1380470739000+&cd=3&hl=tr&ct=clnk&gl=tr&client=firefox-a


  • Create a table on mssql server with some data


CREATE TABLE [dbo].[employee2](
[empid] [numeric](18, 0) NOT NULL,
[empname] [nvarchar](50) NULL,
[empssn] [nvarchar](50) NULL,
[empdept] [nvarchar](50) NULL,
[emptitle] [nvarchar](50) NULL,
[empsal] [numeric](18, 0) NULL
)


insert into employee2 values( 1 ,'Jack','555555555','HR','specialist', 1200 );
insert into employee2 values( 2 ,'Joe','555555556','Management','director', 2000 );
insert into employee2 values( 3 ,'Fred','555555557','Reception','specialist', 1400 );
insert into employee2 values( 4 ,'Lisa','555555558','IT','specialist', 3000 );
insert into employee2 values( 5 ,'Cathy','555555559','Support','specialist', 2500 );
insert into employee2 values( 6 ,'Mike','555555560','Finance','director', 1400 );
insert into employee2 values( 7 ,'Fred','555555561','Management','director', 3000 );
insert into employee2 values( 8 ,'Lisa','555555562','Reception','specialist', 2500 );
insert into employee2 values( 9 ,'Cathy','555555563','IT','director', 2300 );

list max min and average salaries in IT department


SELECT MIN(EmpSal), MAX(EmpSal), AVG(EmpSal)
FROM employee2
WHERE EmpDept='IT';

list the number of employees in IT department


select count(*)
FROM employee2
WHERE EmpDept='IT';

list the highest paid salaries based on department


SELECT EmpDept,MAX(EmpSal)
FROM employee2
GROUP BY EmpDept;

list the salaries based on department


SELECT EmpDept,SUM(EmpSal)  FROM employee2
GROUP BY EmpDept;

list the employees who has the same title but different salaries


select distinct(a.empsal),a.emptitle
from employee2 a
where a.emptitle in
(select b.emptitle
from employee2 b
where b.emptitle=a.emptitle
)
order by a.emptitle asc;



  • How does one escape special characters when writing SQL queries?
Escape quotes

Use two quotes for every one displayed. Examples:

SQL> SELECT 'Frank''s Oracle site' AS text FROM DUAL;
TEXT
--------------------
Franks's Oracle site

SQL> SELECT 'A ''quoted'' word.' AS text FROM DUAL;
TEXT
----------------
A 'quoted' word.

SQL> SELECT 'A ''''double quoted'''' word.' AS text FROM DUAL;
TEXT
-------------------------
A ''double quoted'' word.



References:
http://www.orafaq.com/wiki/SQL_FAQ#How_does_one_escape_special_characters_when_writing_SQL_queries.3F





  • how to add data into a view in oracle.In this example as you add data into view you add also into the table on which view is based



create table Employee2(
     ID                 VARCHAR2(4 BYTE)         NOT NULL,
     First_Name         VARCHAR2(10 BYTE),
     Last_Name          VARCHAR2(10 BYTE),
     Start_Date         DATE,
     End_Date           DATE,
     Salary             Number(8,2),
     City               VARCHAR2(10 BYTE),
     Description        VARCHAR2(15 BYTE)
   );
   
   
   
  insert into  Employee2(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
                values('02','Alison',   'Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver','Tester')
  
;


  insert into  Employee2(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
                  values('03','James',    'Smith',   to_date('19781212','YYYYMMDD'), to_date('19900315','YYYYMMDD'), 6544.78, 'Vancouver','Tester')

;


  insert into  Employee2(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
               values('04','Celia',    'Rice',    to_date('19821024','YYYYMMDD'), to_date('19990421','YYYYMMDD'), 2344.78, 'Vancouver','Manager')

;


  insert into  Employee2(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
                  values('05','Robert',   'Black',   to_date('19840115','YYYYMMDD'), to_date('19980808','YYYYMMDD'), 2334.78, 'Vancouver','Tester')
  ;



  insert into  Employee2(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
                 values('06','Linda',    'Green',   to_date('19870730','YYYYMMDD'), to_date('19960104','YYYYMMDD'), 4322.78,'New York',  'Tester')
  ;



  insert into  Employee2(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
                values('07','David',    'Larry',   to_date('19901231','YYYYMMDD'), to_date('19980212','YYYYMMDD'), 7897.78,'New York',  'Manager')
  ;



  insert into  Employee2(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
                values('08','James',    'Cat',     to_date('19960917','YYYYMMDD'), to_date('20020415','YYYYMMDD'), 1232.78,'Vancouver', 'Tester')
  ;
  
  select * from Employee2;

CREATE VIEW Employee2_view AS
    SELECT id, first_name, last_name
   FROM Employee2;

 select * from Employee2_view;

 INSERT INTO Employee2_view (id, first_name, last_name) VALUES (
                               13, 'New',      'Westerrn');


http://www.java2s.com/Tutorial/Oracle/0160__View/PerforminganINSERTUsingaView.htm




  • UPDATE table_name

SET column1=value, column2=value2,...
WHERE some_column=some_value


UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'

http://www.w3schools.com/sql/sql_update.asp
   


  • SQL query to identify list of duplicate rows 


SELECT col1, col2, count(*)
FROM t1
GROUP BY col1, col2
HAVING count(*) > 1



The first step is to identify which rows have duplicate primary key values:

create table t1(col1 int, col2 int, col3 char(50))
insert into t1 values (1, 1, 'data value one')
insert into t1 values (1, 1, 'data value one')
insert into t1 values (1, 2, 'data value two')


SELECT col1, col2, count(*)
FROM t1
GROUP BY col1, col2
HAVING count(*) > 1


Select the duplicate key values into a holding table. For example:

create table holdkey(col1 int,col2 int,counter int );

insert into holdkey select col1,col2,count(*)
from t1
group by col1,col2
having count(*)>4 ;



Select the duplicate rows into a holding table, eliminating duplicates in the process. For example:

create table holdups(col1 int,col2 int,col3 char(50) );


insert into holdups
select distinct t1.col1,t1.col2,t1.col3
from t1,holdkey
where t1.col1=holdkey.col1
and t1.col2=holdkey.col2;


At this point, the holddups table should have unique PKs,
SELECT col1, col2, count(*)
FROM holdups
GROUP BY col1, col2


Delete the duplicate rows from the original table. For example:

DELETE t1
FROM t1, holdkey
WHERE t1.col1 = holdkey.col1
AND t1.col2 = holdkey.col2




http://support.microsoft.com/default.aspx?scid=kb;en-us;139444



  • 4. Below is a JDBC application (which should look vaguely familiar).  Give short answers the questions below referring to this application, or fill in the blanks as directed.
[20 pts]
import java.io.*;
import java.sql.*;
public class dbdemo {

  public static void main (String[] args) throws IOException{
    String connString = null;
    String input =null;
        int population,votes;
        String stateName = null;
    BufferedReader br = new BufferedReader( new InputStreamReader(System.in));

    Connection conn = null;
    Statement stmt = null;
    ResultSet res = null;
    PreparedStatement ps = null;

    String query = "SELECT P.PRES FROM PRESIDENTS P";
    String pQuery = "SELECT * FROM STATES WHERE POP BETWEEN ? AND ?";
    connString = "jdbc:postgresql://itcsserver/pres?user=guest&password=guest ";

  try {
        Class.forName("org.postgresql.Driver");                //****A****
        conn = DriverManager.getConnection(connString ); //****B****
        stmt = conn.createStatement();

        res = stmt1.executeQuery(query);
        while (res.next()){
            System.out.println(res.getString("PRES"));
    }
        ps = conn.prepareStatement( pQuery );
    System.out.print("Enter minimum population level: ");
    input = br.readLine();
        ps.setInt(1, Integer.parseInt(input));                //****C****
    System.out.print("Enter maximum population level: ");
    input = br.readLine();
        ps.setInt( 2, Integer.parseInt(input));

        res = ps.executeQuery();
        while(res.next()){                                  //****D****
            stateName = res.getString("STATE");
            population = res.getInt("POP");
            votes = res.getInt("STATES_VOTES");                //****E****
            System.out.println(stateName + " has population "+population
                +" and holds "+votes+" electoral votes");
        }
  } catch (Exception e) {
        System.out.println(e.toString());
  }
      System.exit(0);
  }
}
a. What is the purpose of the Java code line labeled “A”. [4]
                This loads the driver software dynamically matching the database server type to the Java code

b.  List 3 ways the connection action in Java code line labeled “B” can fail. [6]
Wrong path, wrong protocol, wrong username or unauthorized, bad password, network not available


c. Explain the actions of line labeled “C”. [3]

It fills in the first ? with a numerically converted string into the SQL statement



d. Explain the next() method in the line labeled “D” [4]

The cursor is advanced to the next or first tuple (if first loop).  Access to the result set is limited to this tuple.  If there are no more tuples, null/false is returned.


e. Describe two exceptions that can be thrown by a resultSet.getInt(“attrName”) method call such as found in line labeled “E”. [3]
bad attribute name, attribute can be made as an integer, resultset tuple not exist


jcsites.juniata.edu/faculty/rhodes/dbms/exams/mid2f13key.docx

MS SQL Server interview questions

  • Describe SQL Server Databases Architecture.
SQL database is divided into logical and physical components.
  • Logical Components
  1. Database objects,
  2. Login,
  3. Users,
  4. Roles,
  5. Groups.
  • Physically components exist as two or more files on disk.
Physically files can be of three types.
  1. Primary data files;starting point of a database. It also points to other files in database. Extension: .mdf
  2. Secondary data files:All data files except primary data file is a part of secondary files. Extension: .ndf
  3. Log files:All log information used to recover database. Extension: .ldf
  • Every SQL Server instance has primarily 4 system databases:master, model, tempdb and msdb
  • A single SQL Server instance is capable of handling thousands of users working on multiple databases.

  • What are the basic functions for master, msdb, model, tempdb and resource databases?
  1. The master database holds information for all databases located on the SQL Server instance and is the glue that holds the engine together. Because SQL Server cannot start without a functioning master database.
  2. The msdb database stores information regarding database backups, SQL Agent information, DTS packages, SQL Server jobs, and some replication information such as for log shipping
  3. The tempdb holds temporary objects such as global and local temporary tables and stored procedures
  4. The model is essentially a template database used in the creation of any new user database created in the instance.
  5. The resource Database is a read-only database that contains all the system objects that are included with SQL Server. SQL Server system objects, such as sys.objects, are physically persisted in the Resource database, but they logically appear in the sys schema of every database. The Resource database does not contain user data or user metadata.
  • what is extent and page ? the relation between them
Extent is a basic unit of storage to provide space for tables. Every extent has a number of data pages. As new records are inserted new data, pages are allocated. There are eight data pages in an extent. So as soon as the eight pages are consumed, it allocates a new extent with data pages.


References:
http://www.dotnetspider.com/resources/21249-SQL-Data-Base.aspx
http://www.careerride.com/SQL-Server-Databases-Architecture.aspx

dbms interview questions

  • What is database?
A database is a logically coherent collection of data with some inherent meaning, representing some aspect of real world and which is designed, built and populated with data for a specific purpose

  • What is DBMS?
It is a collection of programs that enables user to create and maintain a database. In other words it is general-purpose software that provides the users with the processes of defining, constructing and manipulating the database for various applications.

  • What is the difference between DBMS and RDBMS?
DBMS provides a systematic and organized way of storing, managing and retrieving from a collection of logically related information. RDBMS also provides what DBMS provides, but above that, it provides relationship integrity

RDBMS = DBMS + REFERENTIAL INTEGRITY

every person should have an Address. This is a referential integrity between Name and Address. If we break this referential integrity in DBMS and files, it will not complain, but RDBMS will not allow you to save this data if you have defined the relation integrity between person and addresses. These relations are defined by using “Foreign Keys” in any RDBMS.

if the DBMS fulfills the twelve CODD rules, it’s truly a RDBMS. Almost all DBMS (SQL SERVER, ORACLE etc.) fulfill all the twelve CODD rules and are considered truly as RDBMS.

  • What is a Database system?
The database and DBMS software together is called as Database system.

  • What are the advantages of DBMS?
  1. Redundancy is controlled.
  2. Unauthorised access is restricted.
  3. Providing multiple user interfaces.
  4. Enforcing integrity constraints.
  5. Providing backup and recovery.
  • What are the disadvantage in File Processing System
  1. Data redundancy and inconsistency.
  2. Difficult in accessing data.
  3. Data isolation.
  4. Data integrity.
  5. Concurrent access is not possible.
  6. Security Problems.
  • Difference between a file and database
That’s what the main difference is between a simple file and a database; database has an independent way (SQL) of accessing information while simple files do not
File meets the storing, managing and retrieving part of a database, but not the independent way of accessing data.

  1. Database provides a systematic and organized way of storing, managing and retrieving from a collection of logically related information.
  2. Secondly, the information has to be persistent, that means even after the application is closed the information should be persisted.
  3. Finally, it should provide an independent way of accessing data and should not be dependent on the application to access the information.
  • What is Relational Algebra?
It is procedural query language. It consists of a set of operations that take one or two relations as input and produce a new relation.

  • What is E-R model?
This data model is based on real world that consists of basic objects called entities and of relationship among these objects. Entities are described in a database by a set of attributes.

  • What is Data Model?
A collection of conceptual tools for describing data, data relationships data semantics and constraints


  • What is system catalog or catalog relation?
A RDBMS maintains a description of all the data that it contains, information about every relation and index that it contains. This information is stored in a collection of relations maintained by the system called metadata. It is also called data dictionary.


  • Define the "integrity rules"?

There are two Integrity rules.
Entity Integrity: States that "Primary key cannot have NULL value"
Referential Integrity: States that "Foreign Key can be either a NULL value or should be Primary Key value of other relation.
  • What is indexing and what are the different kinds of indexing?
Indexing is a technique for determining how quickly specific data can be found.
Types:
  1. Binary search style indexing
  2. B-Tree indexing
  3. Inverted list indexing
  4. Memory resident table
  5. Table indexing

  • What is a checkpoint and When does it occur?
A Checkpoint is like a snapshot of the DBMS state. By taking checkpoints, the DBMS can reduce the amount of work to be done during restart in the event of subsequent crashes.


  • What are the unary operations in Relational Algebra?
PROJECTION and SELECTION

  • Are the resulting relations of PRODUCT and JOIN operation the same?
No.
PRODUCT: Concatenation of every row in one relation with every row in another.
JOIN: Concatenation of rows from one relation and related rows from another.


  • What is Buffer Manager?
It is a program module, which is responsible for fetching data from disk storage into main memory and deciding what data to be cached in memory.

  • What is Transaction Manager?
It is a program module, which ensures that database, remains in a consistent state despite system failures and concurrent transaction execution proceeds without conflicting.

  • What is File Manager?

It is a program module, which manages the allocation of space on disk storage and data structure used to represent information stored on a disk.

  • What is DDL, DML and DCL ?
  1. Data Definition Language deals with database schemas and descriptions of how the data should reside in the database, therefore language statements like CREATE TABLE or ALTER TABLE belong to DDL.
  2. DML deals with data manipulation, and therefore includes most common SQL statements such SELECT, INSERT, etc.
  3. Data Control Language includes commands such as GRANT, and mostly concerns with rights, permissions and other controls of the database system.
References:
http://www.indiabix.com/technical/dbms-basics/7
http://www.techpreparation.com/mysql-interview-questions-answers1.htm
http://www.dotnetfunda.com/interview/exclusive/showcatquestion.aspx?category=38



DBMS Interview Questions With Answers
http://ihelpstudy.com/dbms-interview-questions-with-answers-database-management-system-most-common-interview-question-788.html

DBMS Interview Questions and Answers
http://vacancyhub.com/?p=800

DBMS - Interview Questions and Answers
http://www.indiabix.com/technical/dbms/