Query Set #0a – creating the PATIENT table

One way to create a table in our database is to specify its schema manually. Let's do that here with our first table, the PATIENT table:

sqlite> CREATE TABLE PATIENT(
Pid VARCHAR(30) NOT NULL,
Fname VARCHAR(30) NOT NULL,
Minit CHAR,
Lname VARCHAR(30) NOT NULL,
Bdate TEXT NOT NULL,
Street VARCHAR(50),
City VARCHAR(30),
State VARCHAR(2),
Zip VARCHAR(5),
Phone VARCHAR(10) NOT NULL,
Sex CHAR,
PRIMARY KEY (Pid)
);

In the preceding example, notice that the name of the table appears after the CREATE TABLE phrase. Following that, there is an open parenthesis, and on each line a new column is named (for example, Pid and Fname). On each line, following the column names, are the types of each column. We use VARCHAR() for most columns in this example, where  is the maximum number of characters that the column contains. A CHAR column contains just one character. Finally, some of the important fields (such as names and identifiers) we will not allow to be blank, and we specify that for those columns by using the NOT NULL phrase.

Now that we created the schema of our table, the next step is to populate the table with data. As we said, in the database we just have five patients, therefore the PATIENT table will have five rows. We use an INSERT command to insert each row into the table as shown here:

sqlite> INSERT INTO PATIENT (Pid, Fname, Minit, Lname, Bdate, Street, City, State, Zip, Phone, Sex)
VALUES ('1','John','A','Smith','1952-01-01','1206 Fox Hollow Rd.','Pittsburgh','PA','15213','6789871234','M');

sqlite> INSERT INTO PATIENT (Pid, Fname, Minit, Lname, Bdate, Street, City, State, Zip, Phone, Sex)
VALUES ('2','Candice','P','Jones','1978-02-03','1429 Orlyn Dr.','Los Angeles','CA','90024','3107381419','F');

sqlite> INSERT INTO PATIENT (Pid, Fname, Minit, Lname, Bdate, Street, City, State, Zip, Phone, Sex)
VALUES ('3','Regina','H','Wilson','1985-04-23','765 Chestnut Ln.','Albany','NY','12065','5184590206','F');

sqlite> INSERT INTO PATIENT (Pid, Fname, Minit, Lname, Bdate, Street, City, State, Zip, Phone, Sex)
VALUES ('4','Harold','','Lee','1966-11-15','2928 Policy St.','Providence','RI','02912','6593482691','M');

sqlite> INSERT INTO PATIENT (Pid, Fname, Minit, Lname, Bdate, Street, City, State, Zip, Phone, Sex)
VALUES ('5','Stan','P','Davis','1958-12-30','4271 12th St.','Atlanta','GA','30339','4049814933','M');

Notice that the INSERT statements first specify that fields that will be inserted, before using the VALUES keyword, after which the actual data elements are listed. If VARCHAR or CHAR is used, the data elements should be surrounded by single quotes.

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

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