Friday, July 18, 2014

Some Important MySQL Commands

DB Backup Related

Database Backup

C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqldump -u root -p123 DatabaseName > C:\FileName

Database Restore

C:\Program Files\MySQL\MySQL Server 5.0\bin>mysql -u root -p123 DatabaseName < C:\FileName

Export

 select * from TableName into outfile 'C:\\filename'  

Import

 load data local infile 'C:\\filename.txt' into table TableName;  

Delete duplicates for making unique column value

  1. Approach 1
  2.  ALTER IGNORE TABLE table_name ADD UNIQUE (col_name1, col_name2, ...);  
    
  3. Approach 2
  4.  CREATE TABLE new_table as select col1, ..., colN from old_table group by col1, ..., colN having count(*) &gt; 1;  
     DROP TABLE old_table;  
     RENAME TABLE new_table to old_table;   

Change column property

 ALTER TABLE table_name   
 ADD|DROP|CHANGE|MODIFY old_name new_name data_type;  

Max nth value

 SELECT * FROM employee ORDER BY salary DESC LIMIT (n - 1), 1  
Note: limit x,y
x: the record that you want to display(n-1) because it starts from 0.
y: the how many records you want to display.

Join

Self Join / Inner Join

 SELECT t1.name as Employee, t2.name as Manager   
 FROM table t1 , table t2   
 WHERE t1.id = t2.id;  

Outer Join

 SELECT table_1.name as Employee, COALESCE(table_2.name, 'No manager') as Manager  
 FROM table_1 LEFT OUTER JOIN table_2   
 ON table_1.id = table_2.id;  

Inner Join (bad way)

 SELECT column_name(s)  
 FROM table_1, table_2  
 WHERE table_1.column_name = table2.column_name  

Inner, Left, Right Join

 SELECT column_name(s)  
 FROM table_1  
 INNER | LEFT | RIGHT JOIN table_2  
 ON table_1.column_name = table_2.column_name  
Note: MySQL does not support FULL JOIN

Order by use in multiple column

 SELECT * FROM table_name   
 ORDER BY col1 DESC, col2 ASC, ... , col N ASC|DESC;  

Set Operation

Union

 SELECT column_name(s) FROM table_name1  
 UNION  
 SELECT column_name(s) FROM table_name2  

 SELECT column_name(s) FROM table_name1  
 UNION ALL  
 SELECT column_name(s) FROM table_name2  

Intersection

 SELECT column_name(s) FROM table_name1  
 WHERE column_name IN  
 (SELECT column_name FROM table_name2)  

Subtraction

 SELECT column_name(s) FROM table_name1  
 WHERE column_name NOT IN  
 (SELECT column_name FROM table_name2)  

Thursday, July 17, 2014

Basic Git Command

Install

# Installing git
$ apt-get install git

# Installing ui version of git
$ sudo apt-get install gitk

Configure

# Set your user name and e-mail address
--global option, because then Git will always use that information for anything you do on that system.
$ git config --global user.name "Ratul"
$ git config --global user.email 13ratul@gmail.com

# Configuring default editor
$ git config --global core.editor emacs

# Color console
$ git config --global color.diff auto
$ git config --global color.status auto
$ git config --global color.branch auto

# List all the settings Git
$ git config --list

# Check what Git thinks a specific key’s value
$ git config user.name

# Three ways to get the manual page (manpage) help for any of the Git commands
$ git help <verb>
$ git <verb> --help
$ man git-<verb>

Uses (Local)

# If you’re starting to track an existing project in Git, you need to go to the project’s directory and type
$ git init

# Few git add commands that specify the files you want to track
# Also used for Staging Modified Files
$ git add *.c
$ git add README

# For ignoring folder or files -

  1. create a file named '.gitignore'
  2. write the name of those file, folder or git regular expression

$ cat .gitignore

# Determine which files are in which state
$ git status

# To see what you’ve changed but not yet staged, type git diff with no other arguments
$ git diff

# Compares your staged changes to your last commit:
$ git diff --cached

# Committing Your Changes
$ git commit

# You can type your commit message inline with the commit command
$ git commit -m "my commit messages"

# Don’t have to run 'git add' command
$ git commit -a -m ’added & committed new files’

# Forget to add some files, or you mess up your commit message. If you want to try that commit again
$ git commit --amend

#Remove file from your tracked files (more accurately, remove it from your staging area) and then commit.
$ git rm fileName

# Rename or move a file in Git
# Equivalent to: $mv README.txt README | $git rm README.txt | $git add README
$ git mv fileNameOld fileNameNew

# Viewing the Commit History
$ git log

# Unstaging a Staged File
$ git reset HEAD fileName

# Unmodifying a Modified File
$ git checkout -- fileName

Uses (Remote)

Adding remote repository 

  1. first create a repository in github or others
  2. then, Adding Remote Repositories
  3. git remote add [shortname] [url]:
  4. use the string of [shortname] on the command line to get the whole URL

$ git remote add tst https://github.com/Asraf-Uddin-Ahmed/vbb.git

# To see which remote servers you have configured
$ git remote
$ git remote -v

Cloning or make copy of remote project
git clone URL
$ git clone git://github.com/schacon/ticgit.git

Pushing to Remotes
# Using -f not a good parctice
$ git push vbb master
$ git push -f vbb master

Fetch
# Get data from remote projects
# The command goes out to that remote project and pulls down all the data from that remote project that you don’t have yet but NOT MERGE THEM
git fetch [remote_name]
$ git fetch tst

Pull
# Get data from remote prjects & merge with desired branch
git pull [remote_name] [branch_name]
$ git pull test master

Removing Remotes
$ git remote rm vbb

Renaming Remotes
$ git remote rename tst test

# See more information about a particular remote
$ git remote show vbb

Advanced Linux Command

File

# Make change to file system (gui)
press -> alt + F2
type -> gksudo nautilus

Copy a file or folder with progress report.
# P => progress, h => huaman readable, r => recursive for folder.
# if folder name does not exist, 'rsync' auto create those folder.
$ rsync -Phr /path/of/source/file /path/of/destination/file

Detail info about a file or folder
$ stat file_or_folder_name

System

# Install *.deb files
sudo dpkg -i *.deb

# Start mysql
$  mysql --user=root --password=123

# Run a program or application without blocking terminal
$ program_name &
# Another approach
$ program_name
^Z # (ctrl + z) for stopping program
$ bg # run this program in background
$ fg # to run this program in foreground

# Format pendrive

  1. first unmount it
  2. then format

$ sudo mkfs.vfat /dev/sdb1 # vfat for fat32

# Shortcut creation
$ ln -s /path/of/main/folder/or/file /path/of/shortcut

# Check internet usage bandiwdth
$ nload -i 1024 -o 128