CS157a
Chris Pollett
Sep 10, 2018
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) );