Tuesday, July 31, 2012

Default vs Override INSERT Signatures

INSERT statements seem simple enough; however, they still seem to give beginners a problem (even the vets have brain farts and get caught up with them). When you are employing an INSERT statement, you can use one of two methods: the Default Signature or the Override Signature.

The Default Signature assumes that you will be inserting data into ALL columns in the target table; therefore when you create an INSERT statement, you do not need to specify which columns you will be inserting into. You will, however, need to provide data for ALL columns in that table. Let's assume that you have created a table in Oracle with the following structure:

CREATE TABLE test
( test_id NUMBER
, test_name CHAR(10)
, test_date DATE
, CONSTRAINT pk_test PRIMARY KEY (test_id));

In MySQL, the same table can be created with the following code:

CREATE TABLE test
( test_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT
, test_name VARCHAR(10)
, test_date DATE );

Using the Default Signature, you would simply insert the data without specifying the columns:

INSERT INTO test
VALUES -- Single-row Inserts MUST have the VALUES keyword
( 1
, 'LSAT'
, '10-MAR-2013' );

Using the Override Signature, you would need to specify BEFORE the VALUES keyword which columns you're inserting into; you will also need to ensure that the columns match up:

INSERT INTO test
( test_id
, test_name
, test_date )
VALUES -- Single-row Inserts MUST have the VALUES keyword
( 2
, 'ACT'
, '23-JUL-2013' );

When I say 'ensure that the columns match up', I mean that the columns in the Override Statement have to correspond to the data that you're inserting after the VALUES keyword.

You'll notice in my code samples that I mentioned a Single-row Insert. A Single-row Insert is exactly what it sounds like - an INSERT statement that inserts a single row of data into the target table. We won't get into Multiple-row Inserts now, but we'll go over them in the next post. We'll also cover the differences in syntax between Oracle and MySQL INSERT statements.

I hope you've found this post helpful, and thank you for visiting!

Monday, July 30, 2012

New Blog for Database Tech Support

As I was taking a database class last semester, I came across loads of problems that I had a hard time figuring out.  I created this blog in order to address various details of beginning database design and development.  Some of the issues covered will include table structure, foreign key - primary key relationships, constraints, joins, merges, and a myriad of other topics that are often frustrating while attempting to learn the intricacies of databases.  I will spend most of my time focusing on Oracle 10g/11g XE and MySQL 5.x, but I'm also taking an advanced database class where we will be getting into SQL Server.

It is important to note that this will not be a comprehensive guide (we will not cover installation of software or creation of users/schemas); instead, I will assume that you have a basic understanding of how databases operate.  This blog will simply be a complementary aid to common road blocks often encountered.

If you come across anything that is unclear or incorrect, please do not hesitate to contact me; I do not by any means consider myself a professional, and I believe we can all learn from one another.  Also, if you've come across any problems during your own database adventures that aren't covered here, please feel free to send me a message either requesting help or (if you've already figured it out) with a detailed solution so we can all benefit from your experience.  I will be sure to credit you for your work if you do send me additional information.

I hope you find this guide helpful!

Jonathan Lundberg