w3reference home
SQL Tutorial


Bookmark and Share

Inserting Data

The INSERT INTO statement is used to insert new rows into a table.
Syntax:
INSERT INTO table_name
(column_name1, column_name2, ....)
VALUES (value1, value2, ....);
Below is a table named, Emp_Add:
ssn fname lname address city state
512687458 Joe Smith 83 First Street Howard Ohio
758420012 Mary Scott 842 Vine Ave. Losantiville Ohio
102254896 Sam Jones 33 Elm St. Paris New York
876512563 Sarah Ackerman 440 U.S. 110 Upton Michigan

The SQL query to add a new row in this table would be:

INSERT INTO Emp_Add
(ssn, fname, address, city, state)
VALUES (202354897, 'Svendson', '82 Broadway', 'Bayonne', 'New Jersey');
This query inserts the data into the table, as a new row, column-by-column, in the pre-defined order. Note that the value of SSN is written without quotes while all the other values have quotes around them. All the text values need to be enclosed within quotes and numeric value are written without quotes. In this case, no data will be added in the column, "lname".

Output:

ssn fname lname address city state
512687458 Joe Smith 83 First Street Howard Ohio
758420012 Mary Scott 842 Vine Ave. Losantiville Ohio
102254896 Sam Jones 33 Elm St. Paris New York
876512563 Sarah Ackerman 440 U.S. 110 Upton Michigan
202354897 Svendson 82 Broadway Bayonne New Jersey

If you want to insert data in all the columns then there is another way. You don't have to mention the names of all the columns, only the values of the columns have to be entered in the correct order. Let's have a look at an example.

INSERT INTO Emp_Add
VALUES (202354897, 'Svendson', 'Ola', '82 Broadway', 'Bayonne', 'New Jersey');
Output:
ssn fname lname address city state
512687458 Joe Smith 83 First Street Howard Ohio
758420012 Mary Scott 842 Vine Ave. Losantiville Ohio
102254896 Sam Jones 33 Elm St. Paris New York
876512563 Sarah Ackerman 440 U.S. 110 Upton Michigan
202354897 Svendson Ola 82 Broadway Bayonne New Jersey

To insert system date in Oracle:

INSERT INTO company_events (event_name, event_date)
VALUES ( 'Event 1', SYSDATE ); 
Code Validator
Learn FTP
Color finder
Link Checker
Free web designs
Coming soon!
Interview Questions...
'w3reference : Learn by examples ... Advanced to beginner's tutorials ...'
Ajax: AJAX tutorial1 | Apache: Apache HTTP Server | Restarting Apache | CSS: CSS Border | CSS Syntax | CSS Selector | CSS Comment | CVS: CVS Release | CVS Login | CVS Logout | CVS Annotate | Databases: Rolap Tutorial | OLAP Tutorial | OLTP Tutorial | data warehousing | Expect: HTML: html | Linux: Dot (.) conf files | Linux Mount Point | Linux Filesystem | SSH Tutorial | Linux Commands: cal | cat | cfdisk | chroot | MySQL: MySQL Commands | PHP: PHP Basics | PHP Variables | PHP Output (echo/print) | PHP String Concat | PL/SQL: PL/SQL Data Types | PL/SQL Control Structures | PL/SQL File Extensions | PL/SQL DBMS_OUTPUT package | Python: My first Python program | Shell: Starting Bash | Bash Redirection | Bash Pipes | Bash Variables | SQL: SQL Transactions | SQL Constraints | SQL Drop | SQL Union & Union All | SVN: svn architecture | SVN Repository | SVN Import | SVN Checkout | Tech: soap | Web Designing: Web Hosting | HTML/XHTML/CSS code validator | Learn FTP | Search Engine Optimization Tips | www: XML: XML vs HTML | XML Syntax | XML Tags, Elements and Attributes | XML Namespaces |
Sitemap | Disclaim | Privacy Policy | Contact | ©2007-2009 w3reference.com All Rights Reserved.