Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to create simple tables, insert values in it, and do queries. How to perform them in the terminal?

share|improve this question
add comment

2 Answers 2

You make a database by typing

 mysql

In the prompt you enter, you then start by creating your database (as explained by onik in the comments):

 CREATE DATABASE dbname

Once you have made that database, you can experiment with it. You can simply type mysql in a terminal and you can get do anything sql related you want. It is possible that you have create to a role in your database with your username.

As described in Sauruv's answer you can also connect to the database as follows (without the space between p and your password or better, just do not use the -p option and you will get a password prompt [credits go to onik]):

Syntax:

mysql -u user_name -ppassword dbname

-u : Specify mysql database user name
-p : Prompt for password
dbname : Specify database name
share|improve this answer
2  
Actually, the syntax you specified only connects to the specified database, it doesn't create it. You'll have to first connect without specifying a database and run CREATE DATABASE [dbname]. Furthermore, if you specify the password on the command line (not recommended, the syntax is mysql -u user_name -ppassword database, without the space between p and your password. You can also omit the password here and MySQL will prompt you for it, which is safer. –  onik Oct 4 '13 at 7:21
    
@onik you are absolutely right. Next time, feel free to either edit my answer or to copy it and elaborate it into your own answer. By way of thanks, I have found a good answer of you on another question and I have upvoted it. –  don.joey Oct 5 '13 at 10:58
add comment

Here is the syntax to execute sql statement from terminal

I'm assuming that you are using MySQL.

Syntax:

mysql -u user_name -p password -e 'SQL Query' database

Clearificance:

-u : Specify mysql database user name
-p : Prompt for password
-e : Execute sql query
database : Specify database name

Example:

  • If you want to create a table person then:

    mysql -u root -p -e 'Create table person(PersonID int, LastName varchar(255), FirstName varchar(255))' mydb
    

    where root is the username, mydb is the name of the database. Similary you can execute any query you want.

  • If you want to insert values in person:

    mysql -u root -p -e 'Insert into person(PersonID,LastName,FirstName) Values(100,"Kumar","Saurav")' mydb
    
  • If you want to select all the information from person and want to save in a file:

    mysql -u root -p -e 'Select * from person' mydb > personinfo
    

And of-course you can create a database using terminal itself

  • To create database mydb execute following command in terminal:

    mysql -u root -p -e 'create database mydb'
    

    it will silently create a database mydb without giving any message/output.

  • To list all the databases execute this command in terminal:

    mysql -u root -p -e 'show databases'
    

Hope it helps you.. Reply if you need further assistance..

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.