Creating and Populating Tables

The CREATE statement is used to create a new table, and the INSERT statement is used to add a new record to a table.

The CREATE statement has this general format:

Format of SQL CREATE statement

Additional information

CREATE TABLE tablename(
    colName dataType      <— can repeat this line, separated by commas
optionalConstraint
);

Here is an example of the use of the CREATE statement:

Example of SQL CREATE statement

Additional information

CREATE TABLE Person (
       name      VARCHAR(100) PRIMARY KEY,   "PRIMARY KEYis a

       age       INTEGER,                    constraint
       lives_in  VARCHAR(100)
);

This statement will create the Person table that we saw earlier in the chapter. It will have three columns called “name,” “age,” and “lives in.” The “optionalConstraint” shown in the first example means that you can add or omit a constraint to a column, giving more information about what kind of values are legal there. We have added a constraint to the “name” column, saying that this is the primary key of the table. That has the effect of making sure that records always have a non-null unique value there when the records are inserted into the table. “Not null” and “unique” are also constraints that can be applied individually.

Try typing the SQL statement into the visual query tool and confirm you get these results. There is already a table called Person in this database, so you will get an error message to that effect. Enter the query again, changing the table name to Person2. You need to completely overwrite the old query with the new one, when using the Mckoi GUI tool.

Some datatypes that SQL understands and the corresponding Java types are shown in Table 23-6. The SQL keywords and datatypes can use any letter case. The convention is to write them all in uppercase.

Table 23-6. Some SQL datatypes

SQL datatype

Corresponding Java type

CHAR(n)

String, exactly n chars

VARCHAR(n)

String, up to n chars

INTEGER or INT

int

DOUBLE

double

DATE

java.sql.Date

TIMESTAMP

java.sql.Timestamp

BLOB

java.sql.Blob

ARRAY

java.sql.Array

DECIMAL, NUMERIC

java.math.BigDecimal

The BLOB datatype means “Binary Large Object.” There are also CLOBs, “Character Large Objects.” SQL arrays are sequences of data. Think back to earlier in the chapter where we said that first normal form means that there are no lists of items in an attribute. There's a question of where you draw the line though. If you are recording student marks over 12 weeks of homework assignments, it's overkill to store each mark in a new record. Instead, we'll have a student/course marks record, and the course marks will be held as a fixed length array that gets updated with a new mark each week. BLOBs, CLOBs, and arrays are represented by classes in the java.sql package, and they have methods to get their values.

The CREATE statement just creates an empty table. As yet it has no records in it. We will put records in using the INSERT statement, which has this general appearance:

Format of SQL INSERT statement

Additional information

INSERT INTO tablename
   (colName1 ,colName2 ,colName3 ...) <— can provide a value for all
  VALUES                                 attributes or just some
  (value1 ,value2 ,value3 ...)     <— can repeat this line, separated by
;                                           commas these values are inserted into
                                              the attributes listed 

Here is an example of the use of the INSERT statement:

Examples of SQL INSERT statement

Additional information

INSERT INTO Person ( name, age, lives_in )       Note the use of single
  VALUES ('Robert Bellamy', 24, 'England' ),     quotes to surround a
        ( 'Grayham Downer', null, 'Africa' ),    String
        ( 'Judith Brown', 34, 'Africa' );        Downer doesn't want to
                                                  give his age

This statement will start to populate (fill in with data) the Person table that we saw earlier in the chapter. The values are inserted into the record in the order in which the attributes are named. The number of values given in each of the value lists should match the number of attributes in the list before the “values” keyword. SQL is very picky about the requirement that character strings be enclosed in single quotes.

Note that some database vendors have not implemented support for inserting multiple rows with one statement. So to retain maximum portability you would want to restrict yourself to adding one record per insert statement.

Try typing the INSERT statement into the visual query tool and confirm you get these results. Put the data into the table named Person2. You should try each subsequent SQL statement in the visual tool from here on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.175.253