0% found this document useful (0 votes)
4 views7 pages

Lecture 6 Creating Database and Database Tables

This document provides an overview of creating databases and tables in SQL, specifically focusing on MySQL. It discusses case sensitivity for SQL keywords and identifiers, the process of creating a database and selecting it, and detailed examples of creating various tables with their respective columns and constraints. The document also explains the use of primary and foreign keys in table definitions and how to verify the setup of the database and tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Lecture 6 Creating Database and Database Tables

This document provides an overview of creating databases and tables in SQL, specifically focusing on MySQL. It discusses case sensitivity for SQL keywords and identifiers, the process of creating a database and selecting it, and detailed examples of creating various tables with their respective columns and constraints. The document also explains the use of primary and foreign keys in table definitions and how to verify the setup of the database and tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LECTURE 6

CREATING DATABASE AND DATABASE TABLES

Case Sensitivity

SQL keywords are not case sensitive. This is standard across database systems.

Case sensitivity for identifiers depends on the database system you are using. In MySQL,
whether database and table names are case sensitive depends on your operating system. The
reason for this is that generally each database will have an underlying directory in your
operating system, and each table will have an underlying file. These directory names and
filenames follow different rules depending on the operating system.

Identifiers in MySQL

An identifier is simply the name of an alias, a database, a table, a column, or an index. It is how
you uniquely identify that object.

Generally speaking, identifiers can contain any characters, with these exceptions:

• They can't contain quote characters, ACSII(0) and ASCII(255).


• Database names can contain any characters that are allowed in a directory name, but
not the characters that have special meaning in a directory name (/, \, and .) for obvious
reasons.

Table names can contain any characters that are allowed in filenames, except for . and /.

Creating a Database

After design, the first step in creating a database is, logically enough, to tell MySQL that we
want to create a new database. We do this with the CREATE DATABASE SQL statement, as
follows:

CREATE DATABASE restaurant;

You can check to see that this statement worked by executing the command

SHOW DATABASES;

You should now see the restaurant database listed among the databases in the system. We now
have an empty database, waiting for some tables to be created.

1
Selecting a Database

Before we can create any tables or do anything else with the restaurant database, we need to
tell MySQL that we want to work with our new database. We do this with the use statement, as
follows:

USE restaurant;

The restaurant database is now selected, and all actions we take from now on will be applied to
this database by default.

Creating Tables

To create the tables in the restaurant database, we use the CREATE TABLE SQL statement. The
usual form of this statement is

CREATE TABLE tablename ( table definition ) [type=table_type];

That is, we begin with the words create table, followed by the name we would like the table to
have, followed by a set of column definitions. At the end of the statement, we can optionally
specify the storage engine type we would like to use.

We will look at an example of table creation to illustrate this point. Listing 6.1 shows a set of
SQL statements that can be used to create the restaurant database.

Listing 6.1 SQL to create the restaurant Database

DROP DATABASE IF EXISTS restaurant;


CREATE DATABASE restaurant;

Use restaurant;

CREATE TABLE vendors(


vendorid CHAR(5) NOT NULL PRIMARY KEY,
companyname VARCHAR(30),
repfname VARCHAR(20),
replname VARCHAR(20),
referredby CHAR(5) REFERENCES vendors(vendorid)
);

2
CREATE TABLE ingredients(
ingredientid CHAR(5) NOT NULL PRIMARY KEY,
name VARCHAR(30),
unit CHAR(10),
unitprice NUMERIC(5,2),
foodgroup CHAR(15),
inventory INTEGER,
vendorid CHAR(5) REFERENCES vendors(vendorid)
);

CREATE TABLE items(


itemid CHAR(5) NOT NULL PRIMARY KEY,
name VARCHAR(30),
price NUMERIC(5,2),
dateadded DATE
);

CREATE TABLE madewith(


itemid CHAR(5) REFERENCES items(itemid),
ingredientid CHAR(5) REFERENCES ingredients(ingredientid),
quantity INTEGER,
PRIMARY KEY (itemid, ingredientid)
);

CREATE TABLE meals(


mealid CHAR(5) NOT NULL PRIMARY KEY,
name CHAR(20)
);

CREATE TABLE partof(


mealid CHAR(5) REFERENCES meals(mealid),
itemid CHAR(5) REFERENCES items(itemid),
quantity INTEGER,
discount DECIMAL(2,2),
PRIMARY KEY(mealid, itemid)
);

CREATE TABLE ads(


slogan VARCHAR(100)
);

CREATE TABLE menuitems(


menuitemid CHAR(5) NOT NULL PRIMARY KEY,

3
name VARCHAR(30),
price NUMERIC(5,2)
);

CREATE TABLE stores(


storeid CHAR(5) NOT NULL PRIMARY KEY,
street VARCHAR(25),
city VARCHAR(25),
state CHAR(2),
zip CHAR(10),
operator VARCHAR(41)
);

CREATE TABLE orders(


ordernumber INTEGER,
linenumber INTEGER,
storeid CHAR(5) REFERENCES stores(storeid),
menuitemid CHAR(5),
price NUMERIC(5,2),
time DATETIME
);

Let’s discuss the SQL statements one by one.

We begin with

DROP DATABASE IF EXISTS restaurant;

This statement checks whether a restaurant database already exists and deletes it if it does,
cleaning the slate if you like. This is not strictly necessary and could even be dangerous, but we
do it here to make sure that this database creation script should work, even if you have already
been experimenting with a restaurant database.

We then create the database and select it for use, as we have seen already:
CREATE DATABASE restaurant;
Use restaurant;

Now, we begin creating tables inside this database. We begin by creating the vendors table, as
follows:

CREATE TABLE vendors(


vendorid CHAR(5) NOT NULL PRIMARY KEY,
companyname VARCHAR(30),

4
repfname VARCHAR(20),
replname VARCHAR(20),
referredby CHAR(5) NOT NULL REFERENCES vendors(vendorid)
);

This table has five columns, and vendorid is the primary key. To declare the columns in the
table, we give a comma-separated list of the column declarations enclosed in parentheses.
Note that attributes of a column do not need to be comma separated—only the columns
themselves do.

This is our first multiline SQL statement. Whitespace is not important in SQL, so we can lay out
our queries in any way we like. Typically, with CREATE statements, you tend to put one item on
each line to increase readability. The SQL interpreter will not try to interpret your statement
until you have typed the final semicolon (;) and pressed Enter.

In this table, we are declaring 5 columns. Each column declaration begins with the name of the
column, which is followed by information about the type of that column. Look at the second
column first in this example because it's a little easier to understand. The declaration

companyname VARCHAR(30)

tells us that the column is called companyname and that its type is VARCHAR(30). The varchar
type is a variable-length string, in this case up to 30 characters. We could also have used char,
which is a fixed-length string. Choosing VARCHAR or CHAR does not make a difference in terms
of using the data, just in how the data is stored in memory. A VARCHAR(30) takes up only as
much room as the number of characters stored in it, whereas a CHAR(30) is always 30
characters wide, regardless of what is stored in it.

Now, look back at the first column definition. It looks like this:

vendorid CHAR(5) NOT NULL PRIMARY KEY,

The name of this column is vendorid, and it is of type CHAR. This is a unique number that we
will use to identify each vendor. After the type, there is some further information about the
column. First, we have specified that this column is NOT NULL—in other words, for every row in
this table, this column must have a value in it. Finally, we have specified that this column is to
be the primary key for this table. If the primary key consists of a single column, we can specify it
like this. For multicolumn primary keys, we must use a different approach, which we will look at
in a moment.

Look now at the second CREATE TABLE statement:


CREATE TABLE ingredients(
ingredientid CHAR(5) NOT NULL PRIMARY KEY,
name VARCHAR(30) NOT NULL,

5
unit CHAR(10) NOT NULL,
unitprice NUMERIC(5,2),
foodgroup CHAR(15),
inventory INTEGER,
vendorid CHAR(5) REFERENCES vendors(vendorid)
);

There is only one new piece of syntax in this statement. The last column in the ingredients table
is the id of the vendor in the vendors table. This is a foreign key. We declare this in the table
definition by adding the references clause as follows:

vendorid CHAR(5) REFERENCES vendors(vendorid)

This tells us that the vendorid in the ingredients table should be referenced back to the
vendorid column in the vendors table.

Now, look at the following statement:

CREATE TABLE madewith(


itemid CHAR(5) REFERENCES items(itemid),
ingredientid CHAR(5) REFERENCES ingredients(ingredientid),
quantity INTEGER,
PRIMARY KEY (itemid, ingredientid)
);

The most interesting thing about this table definition is that this table has a two-column
primary key. You can see that we declare the two columns in the table, itemid and ingredientid,
and then declare the primary key separately with the following line:

PRIMARY KEY (itemid, ingredientid)

You can check whether the tables in your database have been set up correctly using the
command

SHOW TABLES;

You should get the following output:

6
You can get more information about the structure of each table by using the DESCRIBE
command, for example,

DESCRIBE vendors;

This should give you something like the following output:

You might want to check the other tables at this point.

The general form of the CREATE TABLE statement can be found here:
http://dev.mysql.com/doc/refman/5.1/en/create-table.html

You might also like