I created a table in MYSQL using a shell script shown below in which the attributes are pre-defined.
dbstring="mysql -usample -psample12 -Dsampledb -h127.0.0.1 -A "
echo "CREATE TABLE info (id bigint(20) NOT NULL,email varchar(128) NOT NULL,
createddate datetime DEFAULT NULL)" >> create_table.sql
$dbstring < create_table.sql
But the thing is i wanted a script that takes the number of attributes as input and takes each attribute at run-time and creates the table in my MYSQL database with those specified attributes.
A simple example of a script which takes two parameters and juggles them into an SQL-statement to be fed to mysql. Obviously this is the idea in theory, you should adjust appropriately mysql switches and so on.
#!/bin/sh
cat << EOF | mysql mydatabase
CREATE TABLE info (id bigint NOT NULL, $1 varchar(128), $2 varchar(128));
EOF
Related
I have a folder containing around 20.000 png images with size of around 500kb (barcodes) and i want to batch insert them into a mysql table.
I found this script but i could not get it to work properly
#! /bin/bash
dir=/folder/barcodes
ext=png
chmod a+r $dir/*.$ext
mysql -u root -p DBNAME <<eot
USE DBNAME;
drop table if exists t1;
create table t1 (name varchar(128), data mediumblob, PRIMARY KEY(ID));
USE DBNAME;
eot
ls -1 $dir/*.$ext | perl -e 'print "insert into t1(name,data) values ".join(",",map {chop;$f="\"".$_."\""; "($f,load_file($f))"} <>);' | mysql -u root -p DBNAME
my issues are:
table does not get a primary key (i add one afterwords using phpmyadmin)
the pictures are somehow not loades into the database (all values are NULL)
How can I check if the column in MySQL exists using Bash script.
I want to write an if statement and create the column if it does not exist. For example:
col="thecolumn"
if [[ !col ]]; then
db="use myDB; alter table myTable add column name varchar(30) not null;"
mysql -u root -p123456aB "$db"
fi
All the table structure info is in information_schema and you can query that.
https://dev.mysql.com/doc/refman/5.0/en/information-schema.html
I have the following script which fetches employee id from Employee table in every database allocated to the employee and alters the EmployeeLedger_$empId table in the corresponding database
cls_ip="localhost";
mysql="mysql -h $cls_ip -u root"
list=`echo "show databases like '$dbPattern'" | $mysql| grep -v Database`
for db in $list
do
echo "altering EmployeeLedger table for database $db";
${mysql} ${db} -e "use $db";
empId=`${mysql} ${db} -e "select EMPID from Employee"`;
echo "$empId";
${mysql} ${db} -e "alter table concat('EmpTimeLedger',$empId) add column HOLIDAY tinyint(1) not null default 1;";
done
Here I am unsuccessful in concatenating the employee id which i retrieve from Employee table with EmplyeeLedger to form EmployeeLedger_$empId table. How do i do it?
I think the easiest way to do this would be to simply concatenate the empId to the name of your table:
table="EmpTimeLedger_$empId";
Then you would alter your table like this:
ALTER TABLE $table
ADD COLUMN HOLIDAY TINYINT(1) NOT NULL DEFAULT 1;
why dont you try manual string concatenation like this
foo="EmpTimeLedger"
foo+=$empId
${mysql} ${db} -e "alter table $foo add column HOLIDAY tinyint(1) not null default 1;"
Does anyone know if it is possible to add multiple columns at one time using Percona's pt-online-schema-change for MySQL? I have tried literally every variant I can think of in order to add multiple columns at one time. It seems like something you should be able to do, but I cannot find any evidence online that you can, nor can I get the syntax to work out. A few of the statements I've attempted are below, to give you an idea what I've tried (username and password edited out, for obvious reasons)
Statement 1:
pt-online-schema-change --print --progress time,5 --max-load Threads_running=1000
--critical-load Threads_running=10000 --chunk-time 5 --set-vars "innodb_lock_wait_timeout=600"
--nocheck-plan --execute -h webdb -u xxxx --p xxxx --alter "ADD first_seen datetime NOT NULL
DEFAULT 0, last_seen datetime NOT NULL DEFAULT 0 AFTER days_running" D=mydata,t=mytable
Statement 2:
pt-online-schema-change --print --progress time,5 --max-load Threads_running=1000
--critical-load Threads_running=10000 --chunk-time 5 --set-vars "innodb_lock_wait_timeout=600"
--nocheck-plan --execute -h webdb -u xxxx --p xxxx --alter "ADD first_seen datetime NOT NULL
DEFAULT 0, ADD last_seen datetime NOT NULL DEFAULT 0 AFTER days_running" D=mydata,t=mytable
Statement 3:
pt-online-schema-change --print --progress time,5 --max-load Threads_running=1000
--critical-load Threads_running=10000 --chunk-time 5 --set-vars "innodb_lock_wait_timeout=600"
--nocheck-plan --execute -h webdb -u xxxx --p xxxx --alter "ADD first_seen datetime NOT NULL
DEFAULT 0 AFTER days_running, ADD last_seen datetime NOT NULL DEFAULT 0 AFTER days_running"
D=mydata,t=mytable
And so forth. Basically I've tried every variant I can think of for ADD first_seen datetime NOT NULL
DEFAULT 0, ADD last_seen datetime NOT NULL DEFAULT 0 AFTER days_running" (moving/removing the after, moving/removing the ADD, moving/removing the comma, etc)
According to the manual this is possible, just separate the statements with commas. Quoting the --alter section of the manual:
--alter
type: string
The schema modification, without the ALTER TABLE keywords. You can perform
multiple modifications to the table by specifying them with commas. Please
refer to the MySQL manual for the syntax of ALTER TABLE.
In your case this --alter should work (assuming the rest of your statement is correct):
--alter "ADD first_seen datetime NOT NULL DEFAULT 0, ADD last_seen datetime NOT NULL DEFAULT 0 AFTER days_running"
I have just realized that I need to add more columns into my table. I have been running my database live for about a year, and wanted to add a few date fields (logged in date).
I exported the schema and data. I edited the exported sql file to add the following lines in my table definition
lastLoginDate date NULL,
fromAndroid tinyint(1) DEFAULT '0' NULL,
But I get the above error Column count doesn't match value count at row 1.
Is there a way to add rows, without modifying each line in the data ? I mean I can go thru each line one by one and add null, null, but thats a crude way to doing it.
I have checked out the other answers, but none of them have my scenario. I actually dont match the value count.
Edit : I am not doing a insert, I am doing a import mysql -u username -ppassword dbname < dbfile.sql
Try this:
ALTER TABLE tablename
ADD COLUMN (lastLoginDate date NULL, fromAndroid tinyint(1) DEFAULT '0' NULL)
You may save the above script into a file, for example, modify_table.sql and then execute this following:
mysql -u username -ppassword dbname < modify_table.sql