Create a mysql database, tables and insert data

by Vivek Gite on December 13, 2006 · 62 comments

Q. How do I create a MySQL database and tables?

A. If you want to create a database and set up tables for the same use the following two sql commands:
=> CREATE DATABASE - create the database

=> CREATE TABLE - create the table

=> INSERT - To add/insert data to table

CREATE DATABASE creates a database with the given name. To use this statement, you need the CREATE privilege for the database

CREATE TABLE creates a table with the given name. You must have the CREATE privilege for the table.

INSERT inserts new rows into an existing table.

Procedure for creating a database and a sample table

Login as the mysql root user to create database:
$ mysql -u root -p
Output:

mysql>

Add a database called books
mysql> CREATE DATABASE books;

Now database is created. Use a database with use command:
mysql> USE books;

Now create a table called authors with name, email and id:
mysql> CREATE TABLE authors (id INT, name VARCHAR(20), email VARCHAR(20));
Display your table name just created:
mysql> SHOW TABLES;
Output:

+-----------------+
| Tables_in_books |
+-----------------+
| authors         |
+-----------------+
1 row in set (0.00 sec)

Now add a data / row to table books using INSERT statement:
mysql> INSERT INTO authors (id,name,email) VALUES(1,"Vivek","xuz@abc.com");
Output:

Query OK, 1 row affected (0.00 sec)

Add few more rows:
mysql> INSERT INTO authors (id,name,email) VALUES(2,"Priya","p@gmail.com");
INSERT INTO authors (id,name,email) VALUES(3,"Tom","tom@yahoo.com");

Now display all rows:
mysql> SELECT * FROM authors;
Output:

+------+-------+---------------+
| id   | name  | email         |
+------+-------+---------------+
|    1 | Vivek | xuz@abc.com   |
|    2 | Priya | p@gmail.com   |
|    3 | Tom   | tom@yahoo.com |
+------+-------+---------------+
3 rows in set (0.00 sec)

Now you know how to create a database and a table. For further information please see MySQL data types and official documentation.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 62 comments… read them below or add one }

1 ninja May 7, 2008

thank you very much, you helped me out! :)

Reply

2 vaheedulla March 4, 2011

thank you very much, you helped me out! :)

Reply

3 Anil December 30, 2011

thanks its so good example……….

Reply

4 Ashish July 12, 2008

Hi, i saw this site very helpful for me for crating table with mysql thanks for supporting new users;

Reply

5 gh August 22, 2008

very cool and very helpfull…
continue the good work vivek gite
we are expecting many tutorial of urs …
thanks for this one very useful

Reply

6 ST4R January 24, 2009

Hi very good tutorial :D keep up the good work you helped me alot :) i was wondering (if there wasnt any previous sessions opened) how do i use this table so it will show at my website (like linking php and sql or somthing like that) :) sorry for the stupid question but im just learning sql and php…oh and allso could it be possible to use php in a html website (so you can include some php code in you html)
Thanks! :D

Reply

7 Rohit February 11, 2009

Nice start.
Thanx.

Reply

8 Andreas moliana April 22, 2009

I think if you can try to make more example one can easily have a picture of how to get in use with MySQL . And your example may reflect data been put into table so as one can easily see how to use the data he has to create a table or tables.
By the by, this is also of help can train people who wants to use mySQL their studies. many thanks!

Reply

9 izhar April 23, 2009

Thanks a lot. u make easy for me dude bcoz i am try a couple day to create sinog blegle tables but is not work…..good blees you.

Reply

10 Sumit April 23, 2009

Thanks a lot. Helped me very much.

Reply

11 chaminda June 23, 2009

how can we insert a column with data to existing table

Reply

12 neha singh September 12, 2011

alter table name add column new column name varchar(x);

Reply

13 shiva shankar August 4, 2009

pritty good for start up freshers,helped alot .

could have some more command.

Reply

14 srinivas August 27, 2009

thanks for helping me

Reply

15 wawan September 11, 2009

THANKS U SO MUCH,,, 4 help me problem

Reply

16 Eric October 2, 2009

Thanks very much for that short tutorial. It was worth it and very articulate. God Bless.

Reply

17 chaitan kumar yadav October 3, 2009

how to enter data in tables…..

Reply

18 Damian November 24, 2009

Just learning SQL now. This was an incredibly useful tutorial. Thanks!

Reply

19 soundarya January 15, 2010

this information was v useful to me thanks a lot

Reply

20 Geetha January 20, 2010

Hi,
How to connect this database with drupal

Reply

21 sowmiya March 15, 2010

very very nice and simple

Reply

22 gizachew March 17, 2010

hi how are u.it is fine and helpful for me so it is better u keep such a help and support.
please keep it up

Reply

23 suryaprakash April 1, 2010

this is suryaprakash
i am working on swing project in Java
i want to insert Jtable information into DataBase table

can it is possible..?
if yes
plz send a sample program
waiting for u r Reply

My email address is ::suryaprakash.pisay@gmail.com
Thank u in Advance ..!

Reply

24 Anonymous June 30, 2010

thanks

Reply

25 Mahesh August 22, 2010

Nice info. But please specify the PHP code to be used to add data into MySQL database.

Reply

26 Cosmile D'souza November 23, 2010

Hey amigo, Thanks your help is greatly appreciated………

Reply

27 jigs November 27, 2010

how to connect data with the databse in the php

Reply

28 even December 5, 2010

thz alot

Reply

29 ahmad hussain December 17, 2010

I spending hours in search , finally founded this code , really very helpful . Just want to entitle you 5 start. Thx lot.

Reply

30 Nidhi Shah January 7, 2011

How to recover data from .ibd file if innodb is corrupted
I have one test database and many table with the extension of .frm and .idb file. from frm file i got strucure of database but how to restore data from individiual file of .idb file.

Reply

31 Nidhi Shah January 7, 2011

this is suryaprakash
i am working on swing project in Java
i want to insert Jtable information into DataBase table

can it is possible..?
if yes
plz send a sample program
waiting for u r Reply

My email address is ::suryaprakash.pisay@gmail.com
Thank u in Advance ..!

Reply

Reply

32 thiyagi January 10, 2011

thanks for the tut, was helpful..

Reply

33 Soumyadeep Ghosh January 11, 2011

hi!!!!!!!!!!!!this tutorial is very helpful to me.thanks for the post.

Reply

34 shoes February 21, 2011

can i get a full sample from this code..?
thanks

Reply

35 jhalak roy February 23, 2011

thank u sooooooooooo much……helped me a lot……..

Reply

36 jay February 24, 2011

it really helped me about about this tutorial… i’m just a beginner but now i understand the syntax in using the mysql client. thank you very much.

Reply

37 aashish February 27, 2011

thankzzzzzzzzzzzz alot my frnd*****************************************
very helpful for my practical exams

Reply

38 cute programmer April 8, 2011

thanks for this one, im quite confused why my code dont work even though its correct.

Reply

39 Chris April 10, 2011

I am using a webhost and has no shell access so unable to use command line.
Here’s the problem:
I have tried to “import” a table with its content into the existing database but it kept showing a 1062 error about key 1 ………… which I have no idea why and what to do.
I tried to create a new table from within phpmyadmin but it requires me to indicate how many fields and then to fill in those empty boxes…which I have no idea what to put in there.

So, i thought of using bigdump to try to import this table into the database or to do a manual import by copying the content of the table into the SQL tab’s Sql Query box from within phpmyadmin.
Are the above two options suitable?
What would be your best suggestion and guidance on how to proceed so as to succeed in getting this table into the database?

Any assistance, guidance and advice, would be much appreciated.

Thanks in advance and best wishes.
Chris

Reply

40 ThankfulGuy April 18, 2011

Thanks dude! it helped me!

Reply

41 VIVEK.R May 9, 2011

THANK YOU FOR GIVING SUCH A HELPFUL WEBSITE.

Reply

42 panda June 12, 2011

Thanks alotttt:)

Reply

43 dariel June 26, 2011

thank you it’s helpful to find my error in syntax…. tnx

Reply

44 Ken June 29, 2011

Sir what command should i use if i want to copy in mysql or wamp.?

Reply

45 rashid Iqbal July 1, 2011

I want to change the password of user from users table, but the password values shown in table are in encrypted form. how can I change the user password

Reply

46 george davis July 3, 2011

try to change the password in xammp or wampp depending on the server you are using.log in and in the specific database for the specific user, you can change the password. make sure you click GO..
hope this helps…

Reply

47 george davis July 3, 2011

Thanks a lot. Am a beginner in mysql programming and this is good start from here.
George

Reply

48 suranga wickramasinghe July 20, 2011

A great work ,i was able to add huge knowledge within few minitues. thankX lot dear teacher…

Reply

49 Gamachis August 18, 2011

it is really helpful.. wonderful dude!!!

Reply

50 Anuj August 18, 2011

Thanks bro

very use full

i wasn’t able to understand what was the matter but now i understood

once again thnks

Reply

51 Anuj August 18, 2011

How can i deleabte a table?

Reply

52 satz October 29, 2011

to delete a database,
DROP DATABASE ;

Reply

53 rohan August 30, 2011

thankxxxxxxxxxxx a lot..

Reply

54 Marifel September 9, 2011

…’it’s very simple/…

Reply

55 Marifel September 9, 2011

i learned this when i was first year college…

Reply

56 prabhamuralidharan October 18, 2011

Really thank you very much!!!!!!!! because i was struggling with mysql how to create table and databases, now am clear all about that!!!!. In all aspects basic knowledge is very important. so this website have given that knowledge and the explanations also was very understandable! thank you very munch!!!!!!!!
prabhamuralidharan

Reply

57 Naresh October 18, 2011

thanks for helping in such a simple way

Reply

58 satz October 29, 2011

This is awesome for beginners. Thnx

Reply

59 Rak October 31, 2011

Great Article for beginners like me.

Appriciate your help. Keep up the good work.

Thanks,

Reply

60 suresh November 15, 2011

hi ..thnx a lot.
u helped me a lot..

Reply

61 Amol January 18, 2012

Hey thnkx….its realy good ….

Reply

62 assu February 9, 2012

hai,thank u so much……………………….

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 3 + 4 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.



Previous post:

Next post: