1 / 34

Lecture 9 : SQL-Based Database Table Creation

Lecture 9 : SQL-Based Database Table Creation. November 14 th. Reminder: Querying Tables using SQL. Retrieving data Ordering query results Limiting query results Grouping query results. Write the SQL Code for this Query. Write the SQL Code for this Query.

thalia
Download Presentation

Lecture 9 : SQL-Based Database Table Creation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 9: SQL-Based Database Table Creation November 14th

  2. Reminder: Querying Tables using SQL • Retrieving data • Ordering query results • Limiting query results • Grouping query results

  3. Write the SQL Code for this Query

  4. Write the SQL Code for this Query SELECT id, name, owner_id, birth_date, gender FROM horse WHERE gender = ‘M’ ORDER BY birth_date DESC;

  5. Write the SQL Code for this query SELECT id, name, owner_id FROM horse WHERE owner_id IN (‘00011’, ‘0850’, ‘02001’, ‘02003’);

  6. Naming Tables • When naming tables: • Use a descriptive name • Limit table names to 30 characters or less • Use only letters, numbers and the underscore • Avoid duplicating the names of other database objects

  7. Data Types • When creating a table, you specify each column that will be included in the table • Each column has a name and defined data type • The database uses the data type to determine how to store the data for each column

  8. Column Data Type Categories • Different databases provide different column data types. However, all databases contain data types that fit into the following common categories: • Character (TEXT in Access) • Whole number (INTEGER in Access) • Decimal (REAL in Access) • Date and time (DATETIME)

  9. Column Data Type Categories + - / * INTEGER BYTE TEXT + - / *

  10. Constraints • Constraints are clauses that you include in the creation of a table to enforce column-level rules

  11. Constraints • NOT NULL • Requires a value in a column • UNIQUE • Ensures that column values are unique • CHECK • Imposes a condition on a column • PRIMARY KEY • Determines a unique value for the identification of individual rows • FOREIGN KEY • Ensures data integrity between 2 tables

  12. Examples of Constraints • NOT NULL • In an orders table, every order must have an order_date • UNIQUE • Every order_no should be unique • CHECK • Check order_total column to ensure that no order less than 10 euro is placed in the orders table • PRIMARY KEY • Order_no could be used as a primary key

  13. How do I create a table in Access?

  14. How do I create a table in Access?

  15. How do I create a table in Access?

  16. Table Statements • CREATE TABLE • ALTER TABLE: ADD • ALTER TABLE: MODIFY • ALTER TABLE: DROP • DROP TABLE

  17. CREATE TABLE • This is a DDL statement that results in immediate change to the database CREATE TABLE table_name (column1_name datatype, column2_name datatype);

  18. CREATE TABLE Create a table named status with 3 columns, with one specified as a primary key .. • Create the table: CREATE TABLE status • 1st column is order_id, with datatype INTEGER and NOT NULL constraint: CREATE TABLE status (order_id integer NOT NULL …. • 2nd column is order_status, with datatype TEXT and NOT NULL constraint: CREATE TABLE status (order_id integer NOT NULL, order_status TEXT ..

  19. CREATE TABLE • 3rd column is ship_date, with datatype DATETIME : CREATE TABLE status (order_id integer NOT NULL, order_status TEXT, ship_date DATETIME • Order_id is the PRIMARY KEY : CREATE TABLE status (order_idinteger NOT NULL, order_status TEXT, ship_date DATETIME, PRIMARY KEY(order_id));

  20. CREATE THIS TABLE Primary Key CREATE TABLE personal (name TEXT, age INTEGER, PRIMARY KEY(name));

  21. How do I add a column to an existing table in Access? In Design View, add a new column attribute

  22. In SQL, ALTER TABLE: ADD • After using a database table you may find that it would be more useful if a column was either added to a table or deleted from the table • Database tables can be modified using ALTER TABLE • A column can be added using the ALTER STATEMENT with the ADD clause ALTER TABLE table_name ADD column_name datatype;

  23. ALTER TABLE: ADD Add a column to the status table • Alter the table: ALTER TABLE status • Add a column order_date and assign a datatype DATETIME: ALTER TABLE status ADD order_date DATETIME;

  24. How is the new column added to this table? ALTER TABLE personal ADD address TEXT;

  25. How do I modify a column in an existing table in Access? In Design View, alter a column characteristic

  26. In SQL, ALTER TABLE: MODIFY • You can modify many characteristics of the columns in a table ALTER TABLE table_name MODIFY column_name datatype;

  27. ALTER TABLE: MODIFY Modify ship_date column so that it does not allow null values • Alter the table: ALTER TABLE status • Modify a column ship_date and assign a constraint NOT NULL: ALTER TABLE status MODIFY ship_date NOT NULL;

  28. How is the AGE modified from INTEGER to TEXT? ALTER TABLE personal MODIFY age TEXT;

  29. ALTER TABLE: DELETE • Deletion of structural elements from a table is done using the ALTER TABLE statement with the DROP clause ALTER TABLE table_name DROP CHECK; ALTER TABLE table_name DROP UNIQUE(column_name);

  30. ALTER TABLE: DELETE Delete the PRIMARY KEY constraint from the status table • Alter the table: ALTER TABLE status • Delete the PRIMARY KEY constraint: ALTER TABLE status DROP PRIMARY KEY;

  31. How do I delete a table in Access?

  32. DROP TABLE • You can drop a table and all of its data by using the DROP TABLE statement DROP TABLE table_name;

  33. DROP TABLE Drop the status table from the database: • Delete the table: DROP TABLE status;

  34. Table Statements in Today’s Lecture • CREATE TABLE • ALTER TABLE: ADD • ALTER TABLE: MODIFY • ALTER TABLE: DROP • DROP TABLE

More Related