How To Add, Alter Together With Driblet Column Alongside Default Value, Non Nix Constraint – Mysql Database Example
How to add together column inwards existing tabular array amongst default value is to a greater extent than or less other pop SQL interview question asked for Junior marking programming project interviews. Though syntax of SQL query to add together column amongst default value varies piffling chip from database to database, it ever been performed using ALTER keyword of ANSI SQL. Adding column inwards existing tabular array inwards MySQL database is rather slow in addition to guide forrad in addition to nosotros volition encounter instance of SQL query for MySQL database which adds a column amongst default value. You tin every bit good supply constraints similar NULL or NOT NULL spell adding novel column inwards table. In this SQL tutorial we are adding 3rd column inwards a tabular array called Contacts which contains name in addition to phone of contacts. Now nosotros desire to add together to a greater extent than or less other column electronic mail amongst default value "abc@yahoo.com". We volition purpose ALTER ascendancy inwards SQL to create that. By the means this is side past times side inwards our SQL tutorials e.g. How to bring together 3 tables inwards SQL in addition to SQL query to respect duplicate records inwards table. If you lot haven’t read them yet, in addition to thus you lot may respect them useful.
Add, Modify in addition to Drop Column inwards MySQL tabular array amongst ALTER keyword
1) How to add together to a greater extent than or less other column inwards existing tabular array amongst default value inwards MySQL database.
2) How to add together column inwards a MySQL tabular array amongst NOT NULL constraints
mysql> SELECT * FROM Contacts;
+-------+----------+
| elevate | telephone |
+-------+----------+
| James | 80983243 |
| Johny | 67543212 |
| Harry | 12341234 |
| Ron | 44446666 |
+-------+----------+
4 rows IN SET (0.00 sec)
mysql> ALTER TABLE contacts ADD COLUMN electronic mail varchar(20) DEFAULT "abc@yahoo.com"
-> ;
Query OK, 4 rows affected (0.20 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM Contacts;
+-------+----------+---------------+
| elevate | telephone | electronic mail |
+-------+----------+---------------+
| James | 80983243 | abc@yahoo.com |
| Johny | 67543212 | abc@yahoo.com |
| Harry | 12341234 | abc@yahoo.com |
| Ron | 44446666 | abc@yahoo.com |
+-------+----------+---------------+
4 rows IN SET (0.00 sec)
+-------+----------+
| elevate | telephone |
+-------+----------+
| James | 80983243 |
| Johny | 67543212 |
| Harry | 12341234 |
| Ron | 44446666 |
+-------+----------+
4 rows IN SET (0.00 sec)
mysql> ALTER TABLE contacts ADD COLUMN electronic mail varchar(20) DEFAULT "abc@yahoo.com"
-> ;
Query OK, 4 rows affected (0.20 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM Contacts;
+-------+----------+---------------+
| elevate | telephone | electronic mail |
+-------+----------+---------------+
| James | 80983243 | abc@yahoo.com |
| Johny | 67543212 | abc@yahoo.com |
| Harry | 12341234 | abc@yahoo.com |
| Ron | 44446666 | abc@yahoo.com |
+-------+----------+---------------+
4 rows IN SET (0.00 sec)
SQL query to drib column inwards MySQL table
You tin every bit good take column inwards existing tabular array past times using modify tabular array drib column SQL query every bit shown inwards below example:
mysql> ALTER TABLE Contacts DROP COLUMN email;
Query OK, 4 rows affected (0.27 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM Contacts;
+-------+----------+
| elevate | telephone |
+-------+----------+
| James | 80983243 |
| Johny | 67543212 |
| Harry | 12341234 |
| Ron | 44446666 |
+-------+----------+
4 rows IN SET (0.00 sec)
Query OK, 4 rows affected (0.27 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM Contacts;
+-------+----------+
| elevate | telephone |
+-------+----------+
| James | 80983243 |
| Johny | 67543212 |
| Harry | 12341234 |
| Ron | 44446666 |
+-------+----------+
4 rows IN SET (0.00 sec)
SQL query to add together NOT NULL constraints to a column inwards MySQL table
Now nosotros volition encounter SQL query to add together to a greater extent than or less other column inwards existing tabular array amongst NOT NULL constraints. When you lot add together column amongst NOT NULL constraints in addition to without default value in addition to thus at that topographic point value volition last empty.
mysql> ALTER TABLE contacts ADD COLUMN electronic mail varchar(20) NOT NULL;
Query OK, 18 rows affected (0.22 sec)
Records: 18 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM Contacts;
+-------+----------+-------+
| elevate | telephone | electronic mail |
+-------+----------+-------+
| James | 80983243 | |
| Johny | 67543212 | |
| Harry | 12341234 | |
| Ron | 44446666 | |
+-------+----------+-------+
4 rows IN SET (0.00 sec)
mysql> INSERT INTO Contacts VALUES ("Ruby", 12345678, NULL);
ERROR 1048 (23000): COLUMN 'email' cannot last NULL
Query OK, 18 rows affected (0.22 sec)
Records: 18 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM Contacts;
+-------+----------+-------+
| elevate | telephone | electronic mail |
+-------+----------+-------+
| James | 80983243 | |
| Johny | 67543212 | |
| Harry | 12341234 | |
| Ron | 44446666 | |
+-------+----------+-------+
4 rows IN SET (0.00 sec)
mysql> INSERT INTO Contacts VALUES ("Ruby", 12345678, NULL);
ERROR 1048 (23000): COLUMN 'email' cannot last NULL
Now you lot tin encounter that electronic mail column is non accepting nothing values because its created amongst NOT NULL constraints.
That’s all on How to add, modify in addition to drib column inwards a tabular array inwards SQL. We receive got seen MySQL database instance exactly examples are generic in addition to should spill out other database every bit good e.g. Oracle, SQL Server in addition to Sybase. Effectively using NULL in addition to NOT NULL constraints tin significantly ameliorate code character of both database in addition to Server. By carefully applying constraints similar NULL in addition to NOT NULL you lot tin effectively validate each inserted tape inwards table.
Further Learning
How to respect minute highest salary of Employee inwards SQL

0 Response to "How To Add, Alter Together With Driblet Column Alongside Default Value, Non Nix Constraint – Mysql Database Example"
Post a Comment