Best My SQL Interview Questions Part – 3
1.How do you implement one-to-one,one-to-many and many-to-many relationships while designing tables?
One-to-One relationship can be implemented as a single table and rarely as two tables with primary and foreign key relationships. One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships.
Many-to-Many relationships are implemented using a junction table with the keys from both the tables forming the composite primary key of the junction table. It will be a good idea to read up a database designing fundamentals text book.
2.Database design – What is denormalization and when would you go for it?
As the name indicates,denormalization is the reverse process of normalization. It’s the controlled introduction of redundancy in to the database design. It helps improve the query performance as the number of joins could be reduced.
3.Database design – What is normalization?Explain different levels of normalization?
Check out the article Q100139 from Microsoft knowledge base and of course,there’s much more information available in the net. It’ll be a good idea to get a hold of any RDBMS fundamentals text book,especially the one by C. J. Date. Most of the times,it will be okay if you can explain till third normal form.
4.What are the steps you will take to improve performance of a poor performing query?
This is a very open ended question and there could be a lot of reasons behind the poor performance of a query. But some general issues that you could talk about would be:
No indexes,table scans,missing or out of date statistics,blocking,excess recompilations of stored procedures,procedures and triggers without SET NOCOUNT ON,poorly written query with unnecessarily complicated joins,too much normalization,excess usage of cursors and temporary tables.
Some of the tools/ways that help you troubleshooting performance problems are: SET SHOWPLAN_ALL ON,SET SHOWPLAN_TEXT ON,SET STATISTICS IO ON,SQL Server Profiler,Windows NT /2000 Performance monitor,Graphical execution plan in Query Analyzer.
5.What is an index?What are the types of indexes?How many clustered indexes can be created on a table?I create a separate index on each column of a table. What are the advantages and disadvantages of this approach?
Indexes in SQL Server are similar to the indexes in books. They help SQL Server retrieve the data quicker. Indexes are of two types. Clustered indexes and non-clustered indexes. When you create a clustered index on a table,all the rows in the table are stored in the order of the clustered index key.
So,there can be only one clustered index per table. Non-clustered indexes have their own storage separate from the table data storage. Non-clustered indexes are stored as B-tree structures (so do clustered indexes),with the leaf level nodes having the index key and it’s row locater.
The row located could be the RID or the Clustered index key,depending up on the absence or presence of clustered index on the table.
If you create an index on each column of a table,it improves the query performance,as the query optimizer can choose from all the existing indexes to come up with an efficient execution plan.
At the same t ime,data modification operations (such as INSERT,UPDATE,DELETE) will become slow,as every time data changes in the table,all the indexes need to be updated. Another disadvantage is that,indexes need disk space,the more indexes you have,more disk space is used.
6.What’s the difference between DELETE TABLE and TRUNCATE TABLE commands?
DELETE TABLE is a logged operation,so the deletion of each row gets logged in the transaction log,which makes it slow. TRUNCATE TABLE also deletes all the rows in a table,but it won’t log the deletion of each row,instead it logs the deallocation of the data pages of the table,which makes it faster.
Of course,TRUNCATE TABLE can be rolled back. The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table’s data,and only the page deallocations are recorded in the transaction log.
TRUNCATE TABLE removes all rows from a table,but the table structure and its columns,constraints,indexes and so on remain. The counter used by an identity for new rows is reset to the seed for the column. If you want to retain the identity counter,use DELETE instead. If you want to remove table definition and its data,use the DROP TABLE statement.
You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint; instead,use DELETE statement without a WHERE clause. Because TRUNCATE TABLE is not logged,it cannot activate a trigger. TRUNCATE TABLE may not be used on tables participating in an indexed view.
7.What is a primary key?What is a foreign key?
A primary key is the field(s) in a table that uniquely defines the row in the table; the values in the primary key are always unique. A foreign key is a constraint that establishes a relationship between two tables.
This relationship typically involves the primary key field(s) from one table with an adjoining set of field(s) in another table (although it could be the same table). The adjoining field(s) is the foreign key.
8.CREATE INDEX myIndex ON myTable(myColumn)What type of Index will get created after executing the above statement?
Non-clustered index.By default a clustered index gets created on the primary key,unless specified otherwise
9.What are constraints?Explain different types of constraints.
Constraints enable the RDBMS enforce the integrity of the database automatically,without needing you to create triggers,rule or defaults. Types of constraints: NOT NULL,CHECK,UNIQUE,PRIMARY KEY,FOREIGN KEY .
10.What is lock escalation?
Lock escalation is the process of converting a lot of low level locks (like row locks,page locks) into higher level locks (like table locks). Every lock is a memory structure too many locks would mean,more memory being occupied by locks.
To prevent this from happening,SQL Server escalates the many fine-grain locks to fewer coarse-grain locks. Lock escalation threshold was definable in SQL Server
6.5,but from SQL Server
7.0 onwards it’s dynamically managed by SQL Server.
11.Explain different isolation levels.
An isolation level determines the degree of isolation of data between concurrent transactions. The default SQL Server isolation level is Read Committed.
Here are the other isolation levels (in the ascending order of isolation): Read Uncommitted,Read Committed,Repeatable Read,Serializable. See SQL Server books online for an explanation of the isolation levels.
Be sure to read about SET TRANSACTION ISOLATION LEVEL,which lets you customize the isolation level at the connection level. CREATE INDEX myIndex ON myTable(myColumn)
12.What is a transaction and what are ACID properties?
A transaction is a logical unit of work in which,all the steps must be performed or none. ACID stands for Atomicity,Consistency,Isolation,Durability. These are the properties of a transaction.
13.There is a trigger defined for INSERT operations on a table,in an OLTP system. The trigger is written to instantiate a COM object and pass the newly inserted rows to it for some custom processing. What do you think of this implementation?Can this be implemented better?
Instantiating COM objects is a time consuming process and since you are doing it from within a trigger,it slows down the data insertion process. Same is the case with sending emails from triggers. This scenario can be better implemented by logging all the necessary data into a separate table,and have a job which periodically checks this table and does the needful.
14.What is the system function to get the current user’s user id?
USER_ID(). Also check out other system functions like USER_NAME(),SYSTEM_USER,SESSION_USER,CURRENT_USER,USER,SUSER_SID(),HOST_NAME().
15.What is an extended stored procedure?Can you instantiate a COM object by using T-SQL?
An extended stored procedure is a function within a DLL (written in a programming language like C,C++ using Open Data Services (ODS) API) that can be called from T-SQL,just the way we call normal stored procedures using the EXEC statement. Yes,you can instantiate a COM (written in languages like VB,VC++) object from T-SQL by using sp_OACreate stored procedure.
16.Can you have a nested transaction?
Yes,very much. Check out BEGIN TRAN,COMMIT,ROLLBACK,SAVE TRAN and @@TRANCOUNT
17.What is a join and explain different types of joins.
Joins are used in queries to explain how different tables are related. Joins also let you select data from a table depending upon data from another table. Types of joins: INNER JOINs,OUTER JOINs,CROSS JOINs. OUTER JOINs are further classified as LEFT OUTER JOINS,RIGHT OUTER JOINS and FULL OUTER JOINS.
18.What is a correlated sub-query?How can these queries be useful?
The more seasoned developer will be able to accurately describe this type of query. A correlated sub-query is a special type of query containing a sub-query. The sub-query contained in the query actually requests values from the outside query,creating a situation similar to a loop.
19.What is a performance consideration of having too many indexes on a production online transaction processing (OLTP) table?
You are looking for the applicant to make some reference regarding data manipulations. The more indexes on a table,the more time it takes for the database engine to update,insert,or delete data,as the indexes all have to be maintained as the data manipulation occurs.
20.What does NULL mean?
The value NULL is a very tricky subject in the database world,so don’t be surprised if several applicants trip up on this question. The value NULL means UNKNOWN; it does not mean ” (empty string). Assuming ANSI_NULLS are on in your SQL Server database,which they are by default,any comparison to the value NULL will yield the value NULL