| TRUNCATE |
| TRUNCATE is faster and uses fewer system and transaction log resources than DELETE. |
| TRUNCATE 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 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. |
| You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint. |
| It cannot activate a trigger. |
| TRUNCATE can not be Rolled back using logs. |
| TRUNCATE is DDL Command. |
| TRUNCATE Resets identity of the table. |
| DELETE |
| DELETE removes rows one at a time and records an entry in the transaction log for each deleted row.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. |
| DELETE Can be used with or without a WHERE clause |
| DELETE Activates Triggers. |
| DELETE Can be Rolled back using logs. |
| DELETE is DML Command. |
| DELETE does not reset identity of the table. |
Showing posts with label Q n A SQL Server. Show all posts
Showing posts with label Q n A SQL Server. Show all posts
Thursday, September 16, 2010
What is difference between DELETE & TRUNCATE commands?
Delete command removes the rows from a table based on the condition that we provide with a WHERE clause. Truncate will actually remove all the rows from a table and there will be no data in the table after we run the truncate command.
What is normalization and their different forms?
Database normalization is a data design and organization process applied to data structures based on rules that help build relational databases. In relational database design, the process of organizing data to minimize redundancy. Normalization usually involves dividing a database into two or more tables and defining relationships between the tables. The objective is to isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database via the defined relationships.
| Types | Description |
| 1NF | Eliminate Repeating Groups Make a separate table for each set of related attributes, and give each table a primary key. Each field contains at most one value from its attribute domain. |
| 2NF | Eliminate Redundant Data If an attribute depends on only part of a multi-valued key, remove it to a separate table. |
| 3NF | Eliminate Columns Not Dependent On Key If attributes do not contribute to a description of the key, remove them to a separate table. All attributes must be directly dependent on the primary key |
| BCNF | Boyce-Codd Normal Form If there are non-trivial dependencies between candidate key attributes, separate them out into distinct tables. |
| 4NF | Isolate Independent Multiple Relationships No table may contain two or more 1:n or n:m relationships that are not directly related. |
| 5NF | Isolate Semantically Related Multiple Relationships There may be practical constrains on information that justify separating logically related many-to-many relationships. |
| ONF | Optimal Normal Form A model limited to only simple (elemental) facts, as expressed in Object Role Model notation. |
| DKNF | Domain-Key Normal Form A model free from all modification anomalies. |
| Remember, these normalization guidelines are cumulative. For a database to be in 3NF, it must first fulfill all the criteria of a 2NF and 1NF database. | |
What is View?
A simple view can be thought of as a subset of a table. It can be used for retrieving data, as well as updating or deleting rows. Rows updated or deleted in the view are updated or deleted in the table the view was created with. It should also be noted that as data in the original table changes, so does data in the view, as views are the way to look at part of the original table. The results of using a view are not permanently stored in the database. The data accessed through a view is actually constructed using standard T-SQL select command and can come from one to many different base tables or even other views.
What is Index?
An index is a physical structure containing pointers to the data. Indices are created in an existing table to locate rows more quickly and efficiently. It is possible to create an index on one or more columns of a table, and each index is given a name. The users cannot see the indexes, they are just used to speed up queries. Effective indexes are one of the best ways to improve performance in a database application. A table scan happens when there is no index available to help a query. In a table scan SQL Server examines every row in the table to satisfy the query results. Table scans are sometimes unavoidable, but on large tables, scans have a terrific impact on performance.
Clustered indexes define the physical sorting of a database table’s rows in the storage media. For this reason, each database table may have only one clustered index.
Non-clustered indexes are created outside of the database table and contain a sorted list of references to the table itself.
Clustered indexes define the physical sorting of a database table’s rows in the storage media. For this reason, each database table may have only one clustered index.
Non-clustered indexes are created outside of the database table and contain a sorted list of references to the table itself.
Tuesday, September 14, 2010
What is SubQuery and Correlated SubQuery in SQL Server?
SubQuery :
In sub query the inner query is executed only once. Depeding upon the results of inner query outer query is evaluated.
Ex:
SELECT p.product_name FROM product p
WHERE p.product_id = (SELECT o.product_id FROM order_items o
WHERE o.product_id = p.product_id);
Correlated SubQuery :
In correlated subquery the inner query is evaluated once for each row processed by the parent statement or outer query.
Ex:
SELECT id, first_name FROM student_details
WHERE first_name IN (SELECT first_name FROM student_details WHERE subject= 'Science');
In sub query the inner query is executed only once. Depeding upon the results of inner query outer query is evaluated.
Ex:
SELECT p.product_name FROM product p
WHERE p.product_id = (SELECT o.product_id FROM order_items o
WHERE o.product_id = p.product_id);
Correlated SubQuery :
In correlated subquery the inner query is evaluated once for each row processed by the parent statement or outer query.
Ex:
SELECT id, first_name FROM student_details
WHERE first_name IN (SELECT first_name FROM student_details WHERE subject= 'Science');
Subscribe to:
Posts (Atom)