Add Data to a MySQL Database

Print E-mail
MySql General
Current revision posted to MySQL Tutorials by admin on 12/19/2008 7:27:50 PM

You can insert data to the tables directly from mysql> prompt or by loading a file containing the data. The values will be tab separated one line represent one record.

To insert the data directly from mysql use the INSERT statement. The format for INSERT is INSERT INTO <table name> (column1, column2, ....) values ( 'value1', 'value2', ...). For example to insert a species name into the table species the command is like this :

mysql> INSERT INTO species (name) values ('Cat');
Query OK, 1 row affected (0.00 sec)

Remember to put single quotes around a value it is a string.

Notice that i don't have to set the value of id because id have AUTO_INCREMENT attribute. Whenever you insert a new record to the table the value of id is set automatically by mysql with increasing values. The AUTO_INCREMENT attribute is commonly used to create unique identity for new rows

Next example will show how to insert data from a text file, you can get the file here and try in on your computer.

mysql> LOAD DATA LOCAL INFILE "insert.txt" INTO TABLE species;
Query OK, 3 rows affected (0.00 sec)

You can also run SQL queries directly from the DOS prompt. Assuming insert.txt is in C: you can run the query in insert.txt like this

C:mysql < insert.txt


Read Full Article
 

Subscribe By Email

Enter your email address:

Delivered by FeedBurner

Donate

Development & maintainance needs time & money.
With your donation you can help us to keep this project alive
Donate:
  Monthly Monthly
Currency
Amount

Translate

Amazon

Copyright @ 2010 | Tutorialsforu.info | Developed by Open Source Coders | Add your link.