Altering Database Tables: Drop a Column : Alter Table « Table Index « SQL / MySQL






Altering Database Tables: Drop a Column

   
/*
mysql> CREATE TABLE Employee (
    ->    Name    VARCHAR(50) PRIMARY KEY NOT NULL,
    ->    PhoneNo VARCHAR(15) DEFAULT 'Unknown Phone',
    ->    Age     INT CHECK (Age BETWEEN 20 and 30));
Query OK, 0 rows affected (0.14 sec)

mysql> Describe Employee;
+---------+-------------+------+-----+---------------+-------+
| Field   | Type        | Null | Key | Default       | Extra |
+---------+-------------+------+-----+---------------+-------+
| Name    | varchar(50) |      | PRI |               |       |
| PhoneNo | varchar(15) | YES  |     | Unknown Phone |       |
| Age     | int(11)     | YES  |     | NULL          |       |
+---------+-------------+------+-----+---------------+-------+
3 rows in set (0.01 sec)


mysql> Select * from Employee;
+----------+---------------+------+
| Name     | PhoneNo       | Age  |
+----------+---------------+------+
| John Doe | Unknown Phone |   31 |
+----------+---------------+------+
1 row in set (0.00 sec)

mysql> ALTER TABLE Employee DROP COLUMN PhoneNo;
Query OK, 1 row affected (0.05 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> Describe Employee;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Name  | varchar(50) |      | PRI |         |       |
| Age   | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> Select * from Employee;
+----------+------+
| Name     | Age  |
+----------+------+
| John Doe |   31 |
+----------+------+
1 row in set (0.00 sec)


*/
Drop TABLE Employee;
       
CREATE TABLE Employee (
   Name    VARCHAR(50) PRIMARY KEY NOT NULL, 
   PhoneNo VARCHAR(15) DEFAULT 'Unknown Phone',
   Age     INT CHECK (Age BETWEEN 20 and 30));

Describe Employee;

INSERT INTO Employee (Name, Phone, Age) VALUES ('Joe Wang', '666 2323', 26);
INSERT INTO Employee (Name, Age) VALUES ('John Doe', 31);

Select * from Employee;
  
ALTER TABLE Employee DROP COLUMN PhoneNo;

Describe Employee;

Select * from Employee;


           
         
    
    
  








Related examples in the same category

1.Altering Database Tables: Add a Column
2.Altering Database Tables: Add More Columns
3.Altering Database Tables: Drop Unique Constraint and Add Primary Key
4.Alter table: add unique
5.Alter table: drop primary and foreign key
6.Alter table: add primary key, foreign key and add column
7.Altering and Dropping Tables
8.Alter table to change the auto_increment setting and comments
9.Alter table to change the order
10.Alter table to add column
11.Alter table to change column sequence
12.Alter table to add columns
13.Alter table to drop primary key
14.Alter table to drop foreign key
15.Alter table to add fulltext search
16.Changing the Table Design (ALTER TABLE)
17.Use ALTER TABLE to change the column type
18.The Effect of ALTER TABLE on Null and Default Value Attributes
19.Create the table and then set the initial sequence value with ALTER TABLE
20.Alter table to set the primary key start
21.ALTER TABLE statement modifies the table accordingly
22.Alter table to add primary key, add new column, change column, drop column
23.Using ALTER TABLE command to add the names_num column to the cust_names table
24.Two ALTER TABLE statements can be combined
25.Alter table to add foreign key
26.Syntax for Altering a Column
27.Syntax for altering table and delete a Column
28.Storing Altered Character Strings
29.To drop a default value, use ALTER col_name DROP DEFAULT:
30.ALTER column to BINARY
31.Use the asterisk to try matching against the name ian or the alternative spelling, iain.
32.Specifying Multiple Alterations
33.Alter table to add an index
34.Alter table to add unique index
35.Alter table to Delete an Index
36.Alter table to add index
37.To drop an index with ALTER TABLE
38.Reduce the number of significant characters per index in the index to the first 16 characters
39.Add IGNORE to the statement, then use SELECT to have a look at the table contents to see how the duplicates ha
40.Change j from INT to BIGINT
41.MODIFY statement should specify the null and default value attributes explicitly
42.Redefine i to be NOT NULL, then try again
43.Change column definition