Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add column using alter in mysql?

Tags:

sql

mysql

How to add the column using alter by inputting the values on it? As the example, I want to add tempID's column which has value "3" on every row

Maybe it's something like this

ALTER TABLE NAMEYOURTABLE
   ADD COLUMN last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

but the code above is only for the timestamp, not for integer values. Thanks in advance

like image 708
r34627673 Avatar asked May 19 '17 10:05

r34627673


People also ask

How do you use alter to add a column?

Syntax. The basic syntax of an ALTER TABLE command to add a New Column in an existing table is as follows. ALTER TABLE table_name ADD column_name datatype; The basic syntax of an ALTER TABLE command to DROP COLUMN in an existing table is as follows.

How do I add a column to a table in MySQL?

The syntax for adding a column with ALTER statement: ALTER TABLE table_name ADD new_column_name column_definition [FIRST | AFTER column_name];

How do I add a column to an existing table?

In Object Explorer, right-click the table to which you want to add columns and choose Design. Select the first blank cell in the Column Name column. Type the column name in the cell. The column name is a required value.

How do you add multiple columns in alter statement?

Let's look at an example that shows how to add multiple columns to a table in SQL Server using the ALTER TABLE statement. For example: ALTER TABLE employees ADD last_name VARCHAR(50), first_name VARCHAR(40);


1 Answers

You should check out this https://dev.mysql.com/doc/refman/5.7/en/alter-table.html

For your case it would be something like

ALTER TABLE NAMEYOURTABLE ADD COLUMN tempID int NULL DEFAULT 3;
like image 75
Jovana Avatar answered Sep 18 '22 07:09

Jovana