How to alter a table without having null values - mysql

I am having a table in sql with 20 rows inserted.In future it may need to add a another column.
In case of adding column values for the particular column will starts from the 21 line,previous rows values in particular column will be null.
I don't want to have null values on those rows.How can i alter the table for that.

You need to do two steps
1 Alter the table
Alter table table_name add column new_col datatype
2 Update that column into some default value
Update table_name set new_col='some value'

Do it in this way:
alter table tbl_sample add COLUMN sampleadd_column VARCHAR(50) default 'just a sample';
this should work for you;

Related

How to add new date column while creating table from existing table in mysql

I want to add date_col to new_table_name, I am trying the query below but getting an error
CREATE TABLE IF NOT EXISTS new_table_name AS
SELECT A.employee AS employee_name,
A.loc AS location,
'America' country,
date_col TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
FROM existing_table_name;
The CREATE TABLE ... AS SELECT ... is not intended for adding new behaviors to the new table. Changes such as adding a default value to a column should happen in a separate ALTER statement. Therefore, use this approach:
CREATE TABLE IF NOT EXISTS new_table_name AS
SELECT employee,
location,
'America',
date_col,
FROM existing_table_name;
ALTER TABLE new_table_name
MODIFY COLUMN date_col TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
If you intended those aliases to be instructions to change the column names, then you'll need ALTER statements for those too:
ALTER TABLE new_table_name
RENAME COLUMN employee TO employee_name;

How to add column using alter in 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
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;
Along with Jovana's answer, you can specify after which column you need the new column with AFTER .
ALTER TABLE NAMEYOURTABLE ADD tempID int NULL DEFAULT 3
AFTER YOUREXISTINGCOLUMNNAME;
Refer : https://www.w3schools.com/sql/sql_alter.asp
Example to add a column:
ALTER TABLE table_name
ADD column_name datatype;

Mysql default value for new records only

Is it possible to set a default value for new records only in a mysql column?
I want all rows before today to be null if nothing has been entered, but any new rows added after today to be a specific string if nothing has be entered for that column.
Is this possible?
Search in all rows in the table and check if the specific column is empty, if yes set it to null.
UPDATE tablename SET column = CASE column WHEN '' THEN NULL ELSE column END
For future entries you can add to your table a Default Constraint like this:
ALTER TABLE tablename ALTER column SET DEFAULT 'specific string'
This is an old post but I do think this answer might help other folks.
ALTER TABLE object_table_name ADD `value` INT NULL;
ALTER TABLE object_table_name MODIFY COLUMN `value` INT DEFAULT 0;
Doing it this way will only impact newly inserted records, this code did not work for me
ALTER TABLE object_table_name ADD `value` INT DEFAULT 0
I'm using MySQL 5.6

How to add "ON update current timestamp" to existing table column

I have a column, say ts_activity, with datatype timestamp in MYSQL and default value to current timestamp. I want to add on update apply current time stamp to this column value.
I am not able to get alter query to do this. While create new table and its column I can add that, but not able to modify existing column by adding on update apply current timestamp.
Can someone tell me exact query to handle this? Can we alter this after creating table?
Devart's suggestion will work, but I prefer to use MODIFY COLUMN instead of CHANGE COLUMN if I'm not renaming the column.
I also assume that your ts_activity column is not nullable since you have a default, so I am setting it to NOT NULL, but that's up to you.
This is the statement I would use:
ALTER TABLE your_table
MODIFY COLUMN ts_activity TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Try this query -
ALTER TABLE table_name
CHANGE COLUMN column_name column_name TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Note, in the table only one field can be with 'ON UPDATE CURRENT_TIMESTAMP' option.

alter query in mysql

i want to add a new column noOfTry in a table which already have some columns. the column data will be integer type and default value will be 0. i know i need to use alter query, just asking for the correct format of the query for this case
The simplest option would be
ALTER TABLE MyTable ADD COLUMN noOfTry INT NOT NULL DEFAULT 0;
alter table `database`.`table`
add column `noOfTry` int(11) DEFAULT '0' NULL after `column`;
where column is the column before the on you want to add if you want to add in the middle of the table but this is optional.