How to Insert data into MySQL table
Hello programming gurus,
I am novice for learning MySQL, and I wanted to know how to insert data into MySQL table, that table should contain the number of table fields,and also i should be able to insert at 25-30 records at a time. And the program should be so capable of inserting number of fields later on.
Thanks for your help...
Re: How to Insert data into MySQL table
Inserting data into employee_data table with employee.dat file
On Windows
1. Move the file to c:\mysql\bin.
2. Make sure MySQL is running.
3. Issue the following command
mysql employees <employee.dat
On Linux
1. Migrate to the directory that contains the downloaded file.
2. Issue the following command
mysql employees <employee.dat -u username -p
3. Enter your password.
The INSERT SQL statement impregnates our table with data. Here is a general form of INSERT.
Code:
INSERT into table_name (column1, column2....)
values (value1, value2...);
where table_name is the name of the table into which we want to insert data; column1, column2 etc. are column names and value1, value2 etc. are values for the respective columns.
The following statement inserts the first record in employee_data table.
Code:
INSERT INTO employee_data
(f_name, l_name, title, age, yos, salary, perks, email)
values
("Manish", "Sharma", "CEO", 28, 4, 200000,
50000, "manish@bigne..com");
Re: How to Insert data into MySQL table
The purpose of the SQL INSERT statement is to add new rows of data to a specified database table. Using the INSERT statement, it is possible to insert a complete row, a partial row, multiple rows or rows generated as the result of a database query.
Inserting a Complete Row
Code:
INSERT INTO products(
product_id,
product_name,
product_description,
product_location,
product_quantity)
VALUES(
NULL,
'CD-RW Model 4543',
'CD Writer',
'Shelf 4B',
10
};
Note that the product_id is specified as NULL. This is because the product_id in our imaginary table is set to AUTO_INCREMENT so we do not specifically set this. Instead, the MySQL database engine will automatically create a new value for us.
Re: How to Insert data into MySQL table
Here I will also provide you details for inserting data through PHP.
Inserting data to MySQL is done by using mysql_query() to execute INSERT query. Note that the query string should not end with a semicolon. Below is an example of adding a new MySQL user by inserting a new row into table user in database mysql :
Example : insert.php
Source code : insert.phps
Code:
<?php
include 'library/config.php';
include 'library/opendb.php';
mysql_select_db($mysql);
$query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_ priv) VALUES ('localhost', 'phpcake', PASSWORD('mypass'), 'Y', 'Y', 'Y')";
mysql_query($query) or die('Error, insert query failed');
$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');
include 'library/closedb.php';
?>
Re: How to Insert data into MySQL table
Quote:
Originally Posted by
Khushal
Inserting data into employee_data table with employee.dat file
On Windows
1. Move the file to c:\mysql\bin.
2. Make sure MySQL is running.
3. Issue the following command
mysql employees <employee.dat
This isn't working for me. When I use MySQL Command Line Client, and type "mysql employees <employee.dat" I get the "->" prompt indicating that my command is incomplete, if I try to add a ; I get the following error "ERROR 1064 (42000): You have an error in your SQL syntax [..]". Since I am already in mysql (the prompt is "mysql>") I have tried both "employees <employee.dat", and "employees <employee.dat;", with the same results.