I need to alter a table (to change the length and type of a column) of a MySQL database.
The problem is that I need to do this from the command line (in Linux) and I don't really know how.
Can anyone help me?
I have root ssh access.
Something like this should do the trick:
Login to mysql:
mysql -u username -p;
enter mysql password
use databasename;
ALTER TABLE `tablename`
MODIFY COLUMN `columnname` varchar(200) NULL DEFAULT NULL AFTER `previouscolumnname`;
The varchar(200) bit is where you would enter the column type and value length like int(11) etc
Remember to be careful changing field types if the table contains data as it may empty the field or cut it down to the new length specified.
Obviously you need to update per your username, database, tablename and type/length:
mysql -u username -p
use database;
alter table `tablename` modify column `columnname` varchar(512);
Related
I'm currently trying to find a way to separate the mysql export function (using phpmyadmin and/or shell)
Following situation:
I have two databases which were the same at some state but over time only one got updated. So for example:
- database1 has table "users" with columns "UID" and "username"
- database2 has table "users" with columns "UID" "username" and "status"
Now i want to export database2 and import it to database1. database2 contains data I DONT need anymore. But database1 contains important data. So I need something that gives me the possibility to somehow "merge" these databases without data loss on database1 and without merging data from database2.
The merge should look if the table exists, if not -> create it
The merge should look if the columns exist in the table, if not -> create it
What I have tried so far:
I've first tried it with the inbuilt PHPmyadmin export function (advanced options). This would give me the following result:
CREATE TABLE IF NOT EXISTS `users` (
`UID` int(11) NOT NULL,
`username` varchar(25) NOT NULL,
`status` int(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
When I import this, of course, nothing special will happen since the table already exists and so the 'status' column does not get created.
On my research I found a possibility to export a database separated from table creation using shell using the command (blacked out database name and user/pass):
mysqldump -t --insert-ignore --skip-opt -u USER -p PASSWORD -h 127.0.0.1 database > database.sql
But this gives me an insert option with data (which I dont need)
INSERT IGNORE INTO `users` VALUES (1,"bla",1);
Is there any different possibility?
Sure I know I could write it by hand to alter the columns into the table but it sure has to be automatic since its not only the table I named in the example.
To be 100% clear what I'm trying to achieve, a pseudo handwritten sql script:
CREATE TABLE IF NOT EXISTS 'users';
For every column -> IF NOT EXISTS column in 'users' -> ALTER TABLE 'users' ADD column def;
You can use MySQLWorkBench as well. Look video instruction
Is there a proper place to store a high level description of a database? Something along the lines of "This database is used to store XYZ for use by ABC". It's not necessarily information one would need to query, but something that would useful for someone administering the system (i.e. me in a few months when I'm trying to remember what I was trying to accomplish a few months ago.).
This seems like something that someone would have asked before (or information that is readily findable), but none of my searching came up with anything relevant. Most of what I found was for displaying the structure of the database itself.
Comment metadata is not available for MySQL databases, but you can create a table to store some comments: (I have this table in a generic tools database)
-- Create a table to store db name and fields as need.
create table dbinfo(
id int not null primary key auto_increment,
db_name varchar(64) not null collate utf8_bin,
db_comment varchar(255),
unique (db_name)
) default charset utf8;
To fill/update dbinfo table with current databases:
insert into dbinfo (db_name)
select SCHEMA_NAME
from INFORMATION_SCHEMA.SCHEMATA
where SCHEMA_NAME not in (select db_name from dbinfo);
You will only need to maintain dbinfo table until MySQL enables database comments.
I optimized my table with an option in phpmyadmin as it was flashing below. I was using LONGTEXT type for my column file_data. Now, for some reason, huge values are not being entered into this column. It gives NULL. What can I do and do I have to change the data type to ENUM as suggested by propose table structure?
Thanks,
JJ
I think that you are trying to insert long text in the field.
The only option is to make (change it back) field as text field.
Please run following SQL, (replace the table name in SQL)
ALTER TABLE <<your table name>> CHANGE file_data file_data TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ;
So, the other guy at work created a table with a column called:
Max(`abs_spg_20090430`.`ID`)
this is giving me an error now that I am trying to run a dump of the database on a different server.
I am trying to rename it, but
ALTER TABLE abs_spgID_20090504 CHANGE Max(`abs_spg_20090430`.`ID`) id bigint default null;
as well as
ALTER TABLE abs_spgID_20090504 CHANGE `Max(`abs_spg_20090430`.`ID`)` id bigint default null;
give me an error. Does any of you friendly people have a hint? Many thanks!
you need to quote your quotes and the column too, e.g:
ALTER TABLE abs_spgID_20090504 CHANGE `Max(``abs_spg_20090430``.``ID``)` id BIGINT DEFAULT NULL;
I am confused. From references I have seen online, the command to execute a text file script is this:
mysql> --user=root --password=admin --database=zero <query.sql
However when I ran this, the command line said theres an error with mySQL syntax (error 1064).
I saved the query.sql script file within the C:program files...\MYSQL\MYSQL Server5.1.. (whichever folder directory that contains the mySQL command line terminal .exe)
I then did this:
mysql> USE db1 \g
mysql> source <query.sql \g
It also doesnt work; command line gave me the same error. mySQL version I have is different than other versions I have seen. As you can see, you have to add '\g' at the end of every query.
Please help, and let me know if the description is not very clear..thx
EDITED:
So this is the code I have inside the query.sql:
CREATE TABLE IF NOT EXISTS 'db1'(
'id' int(255) NOT NULL auto_increment,
'date' date NOT NULL,
'title' varchar(255) NOT NULL,
'introtext' text NOT NULL,
'maintext' text NOT NULL,
PRIMARY KEY ('id')
)
You can run an SQL file from within the client with:
\. query.sql
Alternatively, if you're not already in the client, you can use the following from the command line:
mysql --user=root --password=admin --database=zero < query.sql
Remove the quotes: 'db1'. Use backquotes where necessary, like for field called date to identify it from type date. And add a ; at the end of the statement:
CREATE TABLE IF NOT EXISTS db1(
id int(255) NOT NULL auto_increment,
`date` date NOT NULL,
title varchar(255) NOT NULL,
introtext text NOT NULL,
maintext text NOT NULL,
PRIMARY KEY (id)
) ;
You need to specify the DB so a statement like:-
USE (DATABASE NAME)
So above replace the (DATABASE NAME) with the name of the Database