Let’s check new column college is created or not. ALTER TABLE studentADD COLUMN college VARCHAR(50) DEFAULT 'ABC Group of College' For example, I want to create a new column college with the default value ‘ ABC Group of College‘ because all the students belong to the same college. Suppose we want to create a new column along with the default value. Example: MySQL adds a column with a default value mysql> DESC student +-+-+-+-+-+-+| Field | Type | Null | Key | Default | Extra |+-+-+-+-+-+-+| roll_no | int | NO | PRI | NULL | auto_increment || first_name | varchar(50) | YES | | NULL | || last_name | varchar(50) | YES | | NULL | || date_of_admission | date | YES | | NULL | |+-+-+-+-+-+-+4 rows in set (0.00 sec)Īs you can see in the above example, A new column date_of_adimmision has been created with DATE TYPE. To check, use DESC table_name statement as follows. Example: Add column in MySQL with default value null ALTER TABLE studentADD COLUMN date_of_admission DATE NULL Īfter executing the above statement, A new column date_of_admission will create. Now, Let’s add a column called date_of_admission of the student with the DATE data type. Here we will add a new column in the student table in different-2 ways like with default value, after a column, end of the columns, with Null values, etc. Suppose after some time, Our requirement is to add a new column called date_of_adimmision with the DATE data type.Īdd a new Column in the Existing Table in MySQL using MySQL Client Now, we will add a new column in the existing table in MySQL. mysql> select * from student +-+-+-+| roll_no | first_name | last_name |+-+-+-+| 1 | Vishvajit | Rao || 2 | Jainy | Doe || 3 | Vaishali | Singh || 4 | Harshita | Kumari || 5 | Vishal | Rao || 6 | Karandeep | Singh || 7 | Vishvajit | Rao || 8 | Pankaj | Sharma |+-+-+-+8 rows in set (0.00 sec) To check whether records are inserted or not, use the SELECT statement. Now, I am going to insert some records into this table by using the INSERT INTO statement. If you are able to see the above output it means our table is created successfully. To check whether the table is created or not, use the DESC keyword which is as follows. Which as follow.ĬREATE TABLE student ( roll_no int NOT NULL AUTO_INCREMENT, first_name varchar(50) DEFAULT NULL, last_name varchar(50) DEFAULT NULL, PRIMARY KEY (roll_no))Īfter, executing the above statement, your table will be created successfully. To add multiple columns in MySQL, you have to specify all the new columns along with their definition separated by a comma (, ).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |