How To Add, Alter Too Drib Column Amongst Default Value, Non Zippo 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 score programming chore interviews. Though syntax of SQL query to add together column amongst default value varies footling combat from database to database, it e'er been performed using ALTER keyword of ANSI SQL. Adding column inwards existing tabular array inwards MySQL database is rather slowly together with direct forrad together with nosotros volition run across instance of SQL query for MySQL database which adds a column amongst default value. You tin likewise 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 together with 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 produce that. By the agency this is adjacent inwards our SQL tutorials e.g. How to bring together 3 tables inwards SQL together with SQL query to notice duplicate records inwards table. If y'all haven’t read them yet, together with thence y'all may notice them useful.
Add, Modify together with 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;
+-------+----------+
| cite | 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;
+-------+----------+---------------+
| cite | 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)
+-------+----------+
| cite | 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;
+-------+----------+---------------+
| cite | 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 likewise withdraw column inwards existing tabular array past times using modify tabular array drib column SQL query equally 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 across SQL query to add together to a greater extent than or less other column inwards existing tabular array amongst NOT NULL constraints. When y'all add together column amongst NOT NULL constraints together with without default value together with thence at that topographic point value volition survive 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;
+-------+----------+-------+
| cite | 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 survive NULL
Query OK, 18 rows affected (0.22 sec)
Records: 18 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM Contacts;
+-------+----------+-------+
| cite | 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 survive NULL
Now y'all tin run across that electronic mail column is non accepting nada values because its created amongst NOT NULL constraints.
That’s all on How to add, modify together with drib column inwards a tabular array inwards SQL. We lead keep seen MySQL database instance simply examples are generic together with should operate on other database equally good e.g. Oracle, SQL Server together with Sybase. Effectively using NULL together with NOT NULL constraints tin significantly ameliorate code lineament of both database together with Server. By carefully applying constraints similar NULL together with NOT NULL y'all tin effectively validate each inserted tape inwards table.
Further Learning
How to notice instant highest salary of Employee inwards SQL
Belum ada Komentar untuk "How To Add, Alter Too Drib Column Amongst Default Value, Non Zippo Constraint – Mysql Database Example"
Posting Komentar