add column to all tables after first column mysql

-- ALTER TABLE tbl_name ADD COLUMN column_name column_definition 
--		[FIRST|AFTER existing_column];
ALTER TABLE office ADD COLUMN phone VARCHAR(200) DEFAULT '000' AFTER name;
ALTER TABLE office ADD COLUMN flag INT(1) FIRST;
ALTER TABLE office ADD COLUMN last_col INT(2);	-- Last column is default position
-- ↓ Test it (Fiddle)

4
7
Awgiedawgie 440215 points

                                    SELECT CONCAT('ALTER TABLE ', table_schema,'.', TABLE_NAME,' ADD COLUMN `hash` VARCHAR(50) NULL DEFAULT UUID() AFTER ', first_column, ';') AS ddl

FROM (

	SELECT
		(
			SELECT `COLUMN_NAME`
			FROM `INFORMATION_SCHEMA`.`COLUMNS`
			WHERE `TABLE_SCHEMA`=t.TABLE_SCHEMA AND `TABLE_NAME`=t.TABLE_NAME
			LIMIT 1
		) AS 'first_column',
		t.*
	FROM
	information_schema.tables t
	WHERE table_schema = 'your_table_name' AND table_type = 'base table'
	
) AS x;

4 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
add column in mysql after specific column add column in mysql with default value add column in mysql Store add column in mysql mysql add column default value string mysql add column text default mysql insert column with default value add column mysql after column mysql add column to table before another column add column description in mysql add column to all tables after first column mysql mysql add another column add column to all tables in database mysql mysql add colum to table mysql addin new column with value add column to table in mysql alter table add column after another column mysql mysql add column after specific column add column mysql with default value add field to table mysql alter table mysql add column with default value alter table mysql add column alter table add column datetime default MYSQL alter table add column datetime default now MYSQL mysql add column default value how to add new column in mysql table add column with a default value mysql how to add column in mysql table how to alter table add column in mysql add column in mysql query mysql add new column with default value from another column mysql add column default string mysql add column default current_date mysql adding column mysql add column date mysql alter table add new column with default value how to add new columns after specific column in mysql set column to default value mysql mysql select add column from another table adding column in mysql how to add additional column in mysql add column alter table mysql add column with default value 0 mysql add data in column mysql alter table add column mysql with default value mysql add column enum default how to create a column in mysql add column in mysql in existing table add column to table mysql insert column into table mysql create column in mysql how to add column in mysql in existing table alter table add column mysql MYSQL create column mysql add new column mysql add column GENERATED add column mysql default value add cloumn in mysql add column to mysql table add default value to column in mysql mysql add default value to column how to add colum mysql how to add column in existing table mysql how to add column in mysql using python how to add column mysql at the beginning mysql add new column with default null mysql add new column with default value description add a collumn in mysql default add column after another column in mysql add column mysql mysql add column datetime mysql add column after another column mysql add column to existing table with default value mysql alter table add column default value mysql add or "add column" add column to table mysql without default datetime value add column to table mysql with default value mysql alter table add column add new column to the table mysql add column in mysq mysql add columns mysql add column between mysql add column before first mysql add column before mysql alter table add column first position mysql alter table add column first mysql add column after another mysql add column after mysql add column with default value mysql add column
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source