Building Database Tables - brianhigh/data-workshop GitHub Wiki
- Defining data types and constraints
- Creating a Schema given an ERD
- Database design tools (Examples: MySQL Workbench, pgAdmin III)
- Example: Create some tables (and relations) from ERD
- HoE: Creating more tables from ERD
- Discussion
- Action Items (readings, videos and tasks)
|
|
|
|
![]() |
|
- Create New Table (in MySQL Workbench)
- Installing MySQL Video
- MySQL WB: Intro Video
- MySQL WB: Connections and Queries Video
- MySQL WB: sqlinfo.net Course Prerequisite Video
- MySQL WB: sqlinfo.net Course Home Page
- Install PgSQL Video
- pgAdmin Video
- SQL Integrity Constraints
- Database Schema
- Schema
- SQLite vs MySQL vs PostgreSQL: A Comparison Of Relational Database Management Systems
We will create a new private schema (and destroy any old one with the same name) and create a new table roster in it. Then we will add some records to the table. Here is the SQL script which does all of this, saved as the file roster.sql, and assuming my own user-id is myuser. In this case, we run the script from a Bash shell.
-- Schema: "private"
DROP SCHEMA IF EXISTS private CASCADE;
CREATE SCHEMA private
AUTHORIZATION myuser;
-- Table: roster
DROP TABLE IF EXISTS private.roster;
CREATE TABLE private.roster
(
id serial PRIMARY KEY,
lname text,
fname text,
status text,
email text
)
WITH (OIDS=FALSE);
\d private.roster
ALTER TABLE private.roster OWNER TO myuser;
INSERT INTO private.roster (lname, fname, status, email) VALUES (
'Sanders', 'John', 'staff', '[email protected]');
INSERT INTO private.roster (lname, fname, status, email) VALUES (
'Smith', 'Andy', 'student', '[email protected]');
INSERT INTO private.roster (lname, fname, status, email) VALUES (
'Jones', 'Sally', 'student', '[email protected]');
INSERT INTO private.roster (lname, fname, status, email) VALUES (
'Adams', 'Henry', 'faculty', '[email protected]');
SELECT * FROM private.roster;And here is how we would run the script:
psql --quiet -f roster.sqlThis is the output we would expect to see:
psql:roster.sql:3: NOTICE: drop cascades to table private.roster
psql:roster.sql:11: NOTICE: table "roster" does not exist, skipping
psql:roster.sql:21: NOTICE: CREATE TABLE will create implicit sequence "roster_id_seq" for serial column "roster.id"
psql:roster.sql:21: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "roster_pkey" for table "roster"
Table "private.roster"
Column | Type | Modifiers
--------+---------+-------------------------------------------------------------
id | integer | not null default nextval('private.roster_id_seq'::regclass)
lname | text |
fname | text |
status | text |
email | text |
Indexes:
"roster_pkey" PRIMARY KEY, btree (id)
id | lname | fname | status | email
----+---------+-------+---------+----------------------
1 | Sanders | John | staff | [email protected]
2 | Smith | Andy | student | [email protected]
3 | Jones | Sally | student | [email protected]
4 | Adams | Henry | faculty | [email protected]
(4 rows)
