What is DDL and DML ?
DDL : Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples:
[ C A D T R ]
CREATE - to create objects in the database
ALTER - alters the structure of the database
DROP - delete objects from the database
TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
RENAME - rename an object
COMMENT - add comments to the data dictionary
DML : Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:
[ S I D U ]
SELECT - retrieve data from the a database
INSERT - insert data into a table
DELETE - deletes all records from a table, the space for the records remain
UPDATE - updates existing data within a table
MERGE - UPSERT operation (insert or update)
CALL - call a PL/SQL or Java subprogram
EXPLAIN PLAN - explain access path to data
LOCK TABLE - control concurrency
To check the complete table information including storage engine.
show table status ;
show table status where Name = '';
Difference between INNODB and MYISAM ?
1. The main differences between InnoDB and MyISAM ("with respect to designing a table or database" you asked about) are support for "referential integrity" and "transactions".
If you need the database to enforce foreign key constraints, or you need the database to support transactions (i.e. changes made by two or more DML operations handled as single unit of work, with all of the changes either applied, or all the changes reverted) then you would choose the InnoDB engine, since these features are absent from the MyISAM engine.
2. With MyISAM, a DML statement will obtain an exclusive lock on the table, and while that lock is held, no other session can perform a SELECT or a DML operation on the table.
MYISAM:
1. supports Table-level Locking
2. designed for need of speed
3. does not support foreign keys hence we call MySQL with MYISAM is DBMS
4. stores its tables, data and indexes in diskspace using separate three different files. (tablename.FRM, tablename.MYD, tablename.MYI)
5. does not support transaction. You cannot commit and rollback with MYISAM. Once you issue a command it’s done.
6. supports fulltext search
You can use MyISAM, if the table is more static with lots of select and less update and delete.
INNODB:
1. supports Row-level Locking
2. designed for maximum performance when processing high volume of data
3. support foreign keys hence we call MySQL with InnoDB is RDBMS
4. stores its tables and indexes in a tablespace
5. supports transaction. You can commit and rollback with InnoDB
Those two specific engines you asked about (InnoDB and MyISAM) have different design goals. MySQL also has other storage engines, with their own design goals. So, in choosing between InnoDB and MyISAM, the first step is in determining if you need the features provided by InnoDB. If not, then MyISAM is up for consideration. A more detailed discussion of differences is rather impractical (in this forum) absent a more detailed discussion of the problem space... how the application will use the database, how many tables, size of the tables, the transaction load, volumes of select, insert, updates, concurrency requirements, replication features, etc.
What is RDBMS?
Relational Data Base Management Systems (RDBMS) are database management systems that maintain data records and indices in tables. Relationships may be created and maintained across and among the data and tables. In a relational database, relationships between data items are expressed by means of tables. Interdependencies among these tables are expressed by data values rather than by pointers. This allows a high degree of data independence. An RDBMS has the capability to recombine the data items from different files, providing powerful tools for data usage.
What are the Properties of the Relational Tables?
Relational tables have the following six properties:
- Values are atomic.
- Column values are of the same kind.
- Each row is unique.
- The sequence of columns is insignificant.
- The sequence of rows is insignificant.
- Each column must have a unique name.
Difference between primary key and unique key in SQL
- Unique key in a table can be null, at-least one but primary key can not be null in any table in relation database like MySQL , Oracle etc.
- Primary key can be combination of more than one unique keys in same table.
- There can be only one primary key per table in relation database e.g. MySQL, Oracle or Sybase but there can be more than one unique key per table.
- Unique key is represented using unique constraint while primary key is created using primary key constraint in any table and it's automatically gets unique constraint.
- Many database engine automatically puts clustered index on primary key and since you can only have one clustered index per table, its not available to any other unique key at same time.
What is a candidate key ?
Candidate keys are those keys which is candidate for primary key of a table. In simple words we can understand that such type of keys which full fill all the requirements of primary key which is not null and have unique records is a candidate for primary key. So thus type of key is known as candidate key. Every table must have at least one candidate key but at the same time can have several.
What is Alternate Key?
If any table have more than one candidate key, then after choosing primary key from those candidate key, rest of candidate keys are known as an alternate key of that table. Like here we can take a very simple example to understand the concept of alternate key. Suppose we have a table named Employee which has two columns EmpID and EmpMail, both have not null attributes and unique value. So both columns are treated as candidate key. Now we make EmpID as a primary key to that table then EmpMail is known as alternate key.
What is Normalization?
Normalization is the process of efficiently organizing data in a database. There are two goals of the normalization process: eliminating redundant data (for example, storing the same data in more than one table) and ensuring data dependencies make sense (only storing related data in a table). Both of these are worthy goals as they reduce the amount of space a database consumes and ensure that data is logically stored. There are several benefits for using Normalization in Database.
Benefits:
- Eliminate data redundancy
- Query optimization
- Faster update due to less number of columns in one table
- Index improvement
What are the Different Normalization Forms?
1NF: Eliminate Repeating Groups -
- Eliminate duplicate columns from the same table.
- Create separate tables for each group of related data and identify each row with a unique column or set of columns (the primary key).
- Remove duplicate groups.
- Create primary key.
| Name | State | Country | Phone1 | Phone2 | Phone3 |
| John | 101 | 1 | 488-511-3258 | 781-896-9897 | 425-983-9812 |
| Bob | 102 | 1 | 861-856-6987 | ||
| Rob | 201 | 2 | 587-963-8425 | 425-698-9684 | |
| PK | [ Phone Nos ] | ||||
| ? | ? | ||||
| ID | Name | State | Country | Phone | |
| 1 | John | 101 | 1 | 488-511-3258 | |
| 2 | John | 101 | 1 | 781-896-9897 | |
| 3 | John | 101 | 1 | 425-983-9812 | |
| 4 | Bob | 102 | 1 | 861-856-6987 | |
| 5 | Rob | 201 | 2 | 587-963-8425 | |
| 6 | Rob | 201 | 2 | 425-698-9684 | |
2NF: Eliminate Redundant Data -
- Meet all the requirements of the first normal form.
- Remove subsets of data that apply to multiple rows of a table and place them in separate tables.
- Remove columns which create duplicate data in a table and related a new table with Primary Key – Foreign Key relationship.
| ID | Name | State | Country | Phone | |||
| 1 | John | 101 | 1 | 488-511-3258 | |||
| 2 | John | 101 | 1 | 781-896-9897 | |||
| 3 | John | 101 | 1 | 425-983-9812 | |||
| 4 | Bob | 102 | 1 | 861-856-6987 | |||
| 5 | Rob | 201 | 2 | 587-963-8425 | |||
| 6 | Rob | 201 | 2 | 425-698-9684 | |||
| ID | Name | State | Country | PhoneID | ID | Phone | |
| 1 | John | 101 | 1 | 1 | 488-511-3258 | ||
| 2 | Bob | 102 | 2 | 1 | 781-896-9897 | ||
| 3 | Rob | 201 | 3 | 1 | 425-983-9812 | ||
| 4 | 2 | 587-963-8425 | |||||
| 5 | 3 | 587-963-8425 | |||||
| 6 | 3 | 425-698-9684 |
3NF: Eliminate Columns Not Dependent On Key -
- Meet all the requirements of the second normal form.
- Remove columns that are not dependent upon the primary key.
Third Normal form applies that every non-prime attribute of table must be dependent on primary key, or we can say that, there should not be the case that a non-prime attribute is determined by another non-prime attribute. So this transitive functional dependency should be removed from the table and also the table must be in Second Normal form. For example, consider a table with following fields.
Student_Detail Table:
| Student_id | Student_name | DOB | Street | city | State | Zip |
|---|
In this table Student_id is Primary key, but street, city and state depends upon Zip. The dependency between zip and other fields is called transitive dependency. Hence to apply 3NF, we need to move the street, city and state to new table, with Zip as primary key.
New Student Table following 1NF will be:
| Student_id | Student_name | DOB | Zip |
|---|
| Zip | Street | city | state |
|---|
- Amount of data duplication is reduced.
- Data integrity achieved.
BCNF: Boyce-Codd Normal Form - A database table is said to be in BCNF if it is in 3NF and contains each and every determinant as a candidate key.The process of converting the table into BCNF is as follows:
- Remove the non trival functional dependency.
- Make separate table for the determinants.
- A relation is in Boyce-Codd Normal Form (BCNF) if every determinant is a candidate key. (See the links in the box at right for definitions of determinant and candidate key)
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 De-normalization?
De-normalization is the process of attempting to optimize the performance of a database by adding redundant data. It is sometimes necessary because current DBMSs implement the relational model poorly. A true relational DBMS would allow for a fully normalized database at the logical level, while providing physical storage of data that is tuned for high performance. De-normalization is a technique to move from higher to lower normal forms of database modeling in order to speed up database access.
What is Truncate command in SQL ?
Use truncate table if you need to delete all rows, since truncate doesn't allow you to specify WHERE clause. truncate removes data by deallocating space used by table which removes lot of overhead in terms of logging and locking and that's why truncate is faster than delete.What you need to take care is rollback, data deleted by truncate can not be rolled back until data server specifically supports it e.g. MSSQL Server which allows to commit or rollback truncate table statement transactional. Another caveat with truncate table statement is that it doesn't fire a trigger and you can not truncate a table when a foreign key references any column to the table to be truncated. Only situation I see which is perfect for using truncate is purging tables with huge data, though there is another solution exists to drop table and recreated it if that make sense.
What is Delete command in SQL ?
Delete is another sql command available for removing records from table. Delete is even more flexible than truncate like it provides support to WHERE Clause which can be use to remove selective data. It logs each row which allows operation to be rolled back and it also fires triggers. One disadvantage of using delete is speed and locking. Delete acquires lock on table and its also very slow operation because of logging, which makes it unsuitable for removing records from large tables. One workaround for this is batch-delete in which you remove batch of records instead on one record at a time. Delete is most suitable fore removing selective data and use it where you want to rollback transaction in database. It’s not useful to purge large amount of data from tables and should not be used, otherwise it could lock the table for very long time, blew log segment and can take ages to complete.
Difference between truncate and delete command in SQL
This is an important point to understand before using truncate or delete on production environment, or writing any script which purges data from tables.
1. truncate is fast delete is slow.
2. truncate doesn't do logging delete logs on per row basis.
3. rollback is possible with delete not with truncate until specifically supported by vendor.
4. truncate doesn't fire trigger, delete does.
5. Don't delete, truncate it when it comes to purge tables.
6. truncate reset identity column in table if any, delete doesn't.
7. truncate is DDL while delete is DML (use this when you are writing exam)
8. truncate doesn't support where clause, delete does.