CS157a
Chris Pollett
Sep 9, 2019
Before we give the syntax of CREATE TABLE in any detail, let's consider some of the supported attribute data types:
Which of the following is true?
The simplest form of declaration of a relation in SQL consists of the keywords CREATE TABLE followed by the name of the relation and a parenthesized, comma separated list of the attribute names and their types. Below are some examples:
CREATE TABLE Movies ( title CHAR(100), year INT, length INT, genre CHAR(10), studioName CHAR(30), producerC# INT ); CREATE TABLE MovieStar ( name CHAR(30), address VARCHAR(255), gender CHAR(1), birthdate DATE );
DROP TABLE R;would delete the table named `R` from the current database.
/* Add a column to an existing table (doesn't work sqlite). Existing tuples will have value null for this column. */ ALTER TABLE MovieStar ADD phone CHAR(16); /* Delete a column */ ALTER TABLE MovieStar DROP birthdate;
CREATE TABLE PERSON ( name VARCHAR(30), gender CHAR(1) DEFAULT '?', birthdate DATE DEFAULT DATE '0000-00-00' ); ALTER TABLE MovieStar ADD phone CHAR(16) DEFAULT 'unlisted';
/* Using declaration method 1 */ CREATE TABLE MovieStar ( name CHAR(30) PRIMARY KEY, address VARCHAR(255), gender CHAR(1), birthdate DATE ); /* Using declaration method 2 */ CREATE TABLE MovieStar ( first_name CHAR(30), last_name CHAR(30), ssn int UNIQUE, address VARCHAR(255), gender CHAR(1), birthdate DATE, PRIMARY KEY (first_name, last_name) );
The three most common operations on sets are union, intersection, and difference. For relations, we will use the following notations and definitions for their meaning:
{('Star Wars', 1977, 124, 'sciFi', 'Fox' 12345), ('Galaxy Quest', 1999, 104, 'comedy', 'DreamWorks', 67890), ('Waynes World', 1992, 95, 'comedy', 'Paramount', 99999)}.
{('Star Wars', 1977, 124), ('Galaxy Quest', 1999, 104), ('Waynes World', 1992, 95)}.
{('sciFi'), ('comedy')}. //notice its a set, so only one copy of comedy
{('Star Wars', 1977, 124, 'sciFi', 'Fox' 12345)}.