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;"
Related
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 am having a simple shell script which will grep a sequence from the log and displays the result on terminal when i run the script. How can i store this result into a MySQL table directly in the script itself?
For Example:
#!/bin/bash
echo "Enter the two numbers to be added:"
read n1
read n2
answer=$(($n1+$n2))
echo $answer
In the above example, how can i store n1, n2 and answer values in a database table.
Create an insert query, and pipe it to stdin of mysql.
echo "insert into myTable values ($n1, $n2, $answer);" | mysql -u .. -h ..
First you need to have a table to hold the value of n1, n2, answer
suppose you have "mytable" in database "test" with 3 columns for n1 n2 and answer, then just insert like this:
echo "insert into test.mytable values ($n1, $n2, $n1+$n2);" | mysql -h hostname -u username -ppassword
or
mysql -h hostname -u username -ppassword -e "insert into test.mytable values ($n1, $n2, $n1+$n2);"
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
I have two databases.
First database is used by site.
Second is used for debuging by test site.
Several times per day I have to update test site database.
I don't have root access to drop and create debug database.
So I have to drop all tables ignoring foreign keys; backup and restore tables from firts database to second.
All tables are InnoDB with foreign keys.
#!/bin/bash
USERNAME=root
PASSWORD=xxx
DBFROM=xxx
DBTO=xxx
HOST=localhost
MYSQL_OPTS="-u $USERNAME -p$PASSWORD -h $HOST"
TABLES=$(mysql $MYSQL_OPTS -BNe "show tables" $DBTO | tr '\n' ',' | sed -e 's/,$//' | awk '{print "SET FOREIGN_KEY_CHECKS = 0;DROP TABLE IF EXISTS " $1 ";SET FOREIGN_KEY_CHECKS = 1;"}')
mysql $MYSQL_OPTS -BNe "$TABLES" $DBTO
mysqldump $MYSQL_OPTS $DBFROM | mysql $MYSQL_OPTS $DBTO
I want to use this to get data from row out of mysql database into text files (one entry under another, 50 entries per file):
$ mysql --user=XXX --password=XXX --batch --skip-column-names \
-e "SELECT userid, displayname FROM Users" stackoverflowdb | \
split -l 50 -a 5 - "result."
but I also don't want to copy duplicate entries to these files. Will this code remove duplicates or do i need to add something to it to don't copy duplicate entries?
Modifying the SQL to use the DISTINCT directive
e.g.
SELECT DISTINCT userid,displayname FROM Users
will ensure that only unique combinations of userid and displayname are selected.
However this will not prevent userids that have identical displaynames.
SELECT DISTINCT userid,displayname FROM Users
.. or outside the database
mysql --user=XXX --password=XXX --batch --skip-column-names \
-e "SELECT userid, displayname FROM Users" stackoverflowdb | \
sort -u | \
split -l 50 -a 5 - "result."