How To Add, Alter Together With Driblet Column Amongst Default Value, Non Nil Constraint – Mysql Database Example
Sunday, June 10, 2018
Add Comment
How to add together column inwards existing tabular array alongside default value is roughly other pop SQL interview question asked for Junior score programming project interviews. Though syntax of SQL query to add together column alongside default value varies trivial 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 too at 1 time forrard too nosotros volition run into instance of SQL query for MySQL database which adds a column alongside default value. You tin likewise render constraints similar NULL or NOT NULL piece adding novel column inwards table. In this SQL tutorial we are adding tertiary column inwards a tabular array called Contacts which contains name too phone of contacts. Now nosotros desire to add together roughly other column e-mail alongside default value "abc@yahoo.com". We volition role ALTER dominance inwards SQL to create that. By the agency this is side past times side inwards our SQL tutorials e.g. How to bring together 3 tables inwards SQL too SQL query to uncovering duplicate records inwards table. If you lot haven’t read them yet, too then you lot may uncovering them useful.
Add, Modify too Drop Column inwards MySQL tabular array alongside ALTER keyword
1) How to add together roughly other column inwards existing tabular array alongside default value inwards MySQL database.
2) How to add together column inwards a MySQL tabular array alongside NOT NULL constraints
mysql> SELECT * FROM Contacts;
+-------+----------+
| cite | telephone |
+-------+----------+
| James | 80983243 |
| Johny | 67543212 |
| Harry | 12341234 |
| Ron | 44446666 |
+-------+----------+
4 rows IN SET (0.00 sec)
mysql> ALTER TABLE contacts ADD COLUMN e-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;
+-------+----------+---------------+
| cite | telephone | e-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)
+-------+----------+
| cite | telephone |
+-------+----------+
| James | 80983243 |
| Johny | 67543212 |
| Harry | 12341234 |
| Ron | 44446666 |
+-------+----------+
4 rows IN SET (0.00 sec)
mysql> ALTER TABLE contacts ADD COLUMN e-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;
+-------+----------+---------------+
| cite | telephone | e-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 driblet column inwards MySQL table
You tin likewise withdraw column inwards existing tabular array past times using alteration tabular array driblet 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;
+-------+----------+
| cite | 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;
+-------+----------+
| cite | 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 run into SQL query to add together roughly other column inwards existing tabular array alongside NOT NULL constraints. When you lot add together column alongside NOT NULL constraints too without default value too then at that topographic point value volition hold out empty.
mysql> ALTER TABLE contacts ADD COLUMN e-mail varchar(20) NOT NULL;
Query OK, 18 rows affected (0.22 sec)
Records: 18 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM Contacts;
+-------+----------+-------+
| cite | telephone | e-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 hold out NULL
Query OK, 18 rows affected (0.22 sec)
Records: 18 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM Contacts;
+-------+----------+-------+
| cite | telephone | e-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 hold out NULL
Now you lot tin run into that e-mail column is non accepting nada values because its created alongside NOT NULL constraints.
That’s all on How to add, modify too driblet column inwards a tabular array inwards SQL. We receive got seen MySQL database instance precisely examples are generic too should function on other database every bit good e.g. Oracle, SQL Server too Sybase. Effectively using NULL too NOT NULL constraints tin significantly meliorate code character of both database too Server. By carefully applying constraints similar NULL too NOT NULL you lot tin effectively validate each inserted tape inwards table.
Further Learning
How to uncovering instant highest salary of Employee inwards SQL
0 Response to "How To Add, Alter Together With Driblet Column Amongst Default Value, Non Nil Constraint – Mysql Database Example"
Post a Comment