SQL UNIQUE uniquely identifies each record in a database table.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
We can have many UNIQUE constraints in a table, but only one PRIMARY KEY .
Example
mysql> CREATE TABLE `new_users` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE (Id));
Query OK, 0 rows affected (0.10 sec)
SQL NOT NULL make sure that a column does not accept null values
Example
mysql> CREATE TABLE `new_users` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.09 sec)
Here the field will never have a null value
Constraints are used to limit the type of data that can go into a table and it can be specified when a table is created or after the table is created.
Below given are some important constraints
PRIMARY KEY
FOREIGN KEY
NOT NULL
UNIQUE
CHECK
DEFAULT
SQL CREATE TABLE is used to create a table.
Syntax
CREATE TABLE table_name
Example
mysql> CREATE TABLE `new_users` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.09 sec)
SQL CREATE DATABASE is used to create a database.
Syntax
CREATE DATABASE database_name
Example
CREATE DATABASE phpcode;
SQL SELECT INTO statement selects data from one table and inserts it into a different table
Mainly it can be used to keep the backup of the table .
Syntax
SELECT * INTO new_table_name [IN another_DB] FROM old_tablename
SQL UNION can be used to combine the result two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.
Syntax
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
MySQL Table – user1
+—-+——+
| id | name |
+—-+——+
| 1 | AAAA |
| 2 | BBBB |
| 3 | CCCC |
| 4 | DDDD |
+—-+——+
MySQL Table – user2
+—-+——+
| id | name |
+—-+——+
| 1 | PPPP |
| 2 | QQQQ |
| 3 | RRRR |
| 4 | RRRR |
+—-+——+
Example
mysql> SELECT * FROM user1 UNION SELECT * FROM user2 ;
+—-+——+
| id | name |
+—-+——+
| 1 | AAAA |
| 2 | BBBB |
| 3 | CCCC |
| 4 | DDDD |
| 1 | PPPP |
| 2 | QQQQ |
| 3 | RRRR |
| 4 | RRRR |
+—-+——+
8 rows in set (0.00 sec)
SQL FULL JOIN return rows when there is a match in one of the tables
Syntax
SELECT * FROM table_name1 FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
MySQL Table – users
|+—-+——+
| id | name |
+—-+——+
| 1 | AAAA |
| 2 | BBBB |
| 3 | CCCC |
| 4 | DDDD |
| 5 | EEEE |
+—-+——+
MySQL Table – Orders
+—–+—–+——–+
| oid | uid | items |
+—–+—–+——–+
| 1 | 1 | Pen |
| 2 | 2 | Watch |
| 3 | 3 | shoe |
| 4 | 4 | mobile |
+—–+—–+——–+
Example
mysql> SELECT u.name,o.items FROM users as u FULL JOIN orders as o ON u.id=o.uid ;
+——+——–+
| name | items |
+——+——–+
| AAAA | Pen |
| BBBB | Watch |
| CCCC | shoe |
| DDDD | mobile |
+——+——–+
4 rows in set (0.00 sec)
SQL RIGHT JOIN returns all the rows from the right table even if there are no matches in the left table
Syntax
SELECT * FROM table_name1 RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
MySQL Table – users
|+—-+——+
| id | name |
+—-+——+
| 1 | AAAA |
| 2 | BBBB |
| 3 | CCCC |
| 4 | DDDD |
| 5 | EEEE |
+—-+——+
MySQL Table – Orders
+—–+—–+——–+
| oid | uid | items |
+—–+—–+——–+
| 1 | 1 | Pen |
| 2 | 2 | Watch |
| 3 | 3 | shoe |
| 4 | 4 | mobile |
+—–+—–+——–+
Example
mysql> SELECT u.name,o.items FROM users as u RIGHT JOIN orders as o ON u.id=o.uid ;
+——+——–+
| name | items |
+——+——–+
| AAAA | Pen |
| BBBB | Watch |
| CCCC | shoe |
| DDDD | mobile |
+——+——–+
4 rows in set (0.00 sec)
SQL LEFT JOIN returns all rows from the left table even if there are no matches in the right table.
Syntax
SELECT * FROM table_name1 LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
MySQL Table – users
|+—-+——+
| id | name |
+—-+——+
| 1 | AAAA |
| 2 | BBBB |
| 3 | CCCC |
| 4 | DDDD |
| 5 | EEEE |
+—-+——+
MySQL Table – Orders
+—–+—–+——–+
| oid | uid | items |
+—–+—–+——–+
| 1 | 1 | Pen |
| 2 | 2 | Watch |
| 3 | 3 | shoe |
| 4 | 4 | mobile |
+—–+—–+——–+
Example
mysql> SELECT u.name,o.items FROM users as u LEFT JOIN orders as o ON u.id=o.uid ;
+——+——–+
| name | items |
+——+——–+
| AAAA | Pen |
| BBBB | Watch |
| CCCC | shoe |
| DDDD | mobile |
| EEEE | NULL |
+——+——–+
5 rows in set (0.01 sec)
Posts navigation
Zend | Magento Certified PHP | eCommerce Architect