Copying one table to another table with and without content
CREATE TABLE `uday` LIKE `uday_new`;
INSERT INTO `uday` SELECT * FROM `uday_new`;
Normalising data means eliminating redundant information from a table and organizing the data so that future changes to the table are easier.
Denormalozation means allowing redundancy in a table.
PRIMARY KEY and UNIQUE KEY enforces the Uniqueness of the values (i.e. avoids duplicate values) on the column[s] on which it is defined. Also these key’s can Uniquely identify each row in database table. primary key can be referred as foreign key and unique key can not refer as foreign key
Below table lists out the major difference between PRIMARY KEY and UNIQUE KEY:
PRIMARY KEY | UNIQUE KEY | |
NULL | It doesn’t allow Null values. Because of this we refer PRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINT |
Allows Null value. But only one Null value. |
INDEX | By default it adds a clustered index | By default it adds a UNIQUE non-clustered index |
LIMIT | A table can have only one PRIMARY KEY Column[s] | A table can have more than one UNIQUE Key Column[s] |
Primary Key:
-1 Primary key is used to avoid the duplication of records under same column
-2 Primary key is used to uniquely identify the each record from table
-3 You can use only one primary key at a time on single table
-4 It does not allow any null value or duplicate value
Foreign Key :
-1 it is used to give the reference of another table primary key..when i update the child table
record then it does not allow me..first i need to update from parent table then it is updated in
child table
Foreign keys are used to reference unique columns in another table. So, for example, a foreign key
can be defined on one table A, and it can reference some unique column(s) in another table B.
Why would you want a foreign key? Well, whenever it makes sense to have a relationship between columns
in two different tables.