Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Chapter 07-1 Create Table Structure and Modifying Data

The document discusses SQL statements used to create and modify database objects and data. It covers: 1) The four categories of SQL statements: DDL, DML, DCL, TCL and examples of each. 2) DDL statements like CREATE, DROP, ALTER used to define database schema and objects. 3) DML statements like SELECT, INSERT, UPDATE, DELETE used to manipulate data. 4) Data types like CHAR, VARCHAR, INTEGER, DATE and examples of creating tables and columns with these types.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Chapter 07-1 Create Table Structure and Modifying Data

The document discusses SQL statements used to create and modify database objects and data. It covers: 1) The four categories of SQL statements: DDL, DML, DCL, TCL and examples of each. 2) DDL statements like CREATE, DROP, ALTER used to define database schema and objects. 3) DML statements like SELECT, INSERT, UPDATE, DELETE used to manipulate data. 4) Data types like CHAR, VARCHAR, INTEGER, DATE and examples of creating tables and columns with these types.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Create Table Structure and Modify Data

Chapter 7
SQL Statements: A Review
• SQL statements are categorized into four categories:
– DDL (Data Definition Language)
– DML (Data Manipulation Language)
– DCL (Data Control Language)
– TCL (Transaction Control Language)

2
DDL (Data Definition Language): A Review
• SQL commands used to create and modify the structure of
database objects:
– CREATE – create the database or its objects (table, index, function,
views, store procedure and triggers)
– DROP – delete objects from the database
– ALTER – alter the structure of the database
– TRUNCATE – remove all rows (records) from a table
– COMMENT – add comments to the data dictionary
– RENAME – Rename an object existing in the database
3
DML (Data Manipulation Language): A Review
• SQL commands that deal with the manipulation of data in a
database:
– SELECT – retrieve data from the a database
– INSERT – insert data into a table
– UPDATE – update existing data within a table
– DELETE – delete rows (records) from a database table

4
Creating a Schema
• A schema is an object that serves as a container for database
objects, such as tables, views, indexes, stored procedures, and
other object types
• A schema is created by entering:

CREATE SCHEMA myschema;

5
Database Table
• A base table is a database object that stores physical data
– Often referred to as just table
• Stored in schema
• Uniquely identified by name
• Layout of the table must be described and created:
– Includes defining the characteristics of columns, including names, sizes,
data type, valid values, default value, and others
– Determined from the relational model

6
Rules for Naming Tables and Columns: A Review
• Names must start with an alpha character
• After position 1, names can contain letters, number and
underscores (_)
• Names cannot exceed 30 characters
• Names cannot contain spaces
• Do not use any SQL reserved keywords, such as "select",
"create" or "insert" as names for tables or column names

7
ANSI SQL Data Types

8
Character Data Types
• Character data can be:
– Alphabetic letters, numbers, and special characters (e.g., punctuation
marks, currency symbols)
– One character occupies a single byte CREATE TABLE customers

• CHAR (… address CHAR(35),


email VARCHAR(256), …)
– Fixed length character string
– All values in column have same length
 A CHAR(50) field will reserve 50 bytes of storage
 Value is padded to the right with blanks

9
VARCHAR Data Type
• VARCHAR(max-length)
CREATE TABLE customers
– Variable length character string (… address CHAR(35),

– Column length is uncertain email VARCHAR(256), …)

 Not padded with blanks


– The actual data is preceded by a two-byte integer that contains the
actual length

10
Numeric Data
• Limited to numeric digits 0−9
• SQL defines numeric data with precision and scale
– Precision … total number of digits available in the number
– Scale … how many digits are to right of decimal point
• Thousands separators (commas) and decimal point are not stored
• Data type determines how variable is represented and stored
– DECIMAL/NUMERIC
– INTEGER, SMALLINT, BIGINT

11
Integer Data Types
• SMALLINT, INTEGER, BIGINT
– Integers store numeric values in binary format
– Most compact numeric format
– Each bit is either "on" (1) or "off" (0)
– The decimal value is determined by adding the "on" bits together

12
Integer Data Types
Data Type Bytes Bits Digits Min Value Max Value
SMALLINT 2-byte binary 15 5 -32,768 32,767
INTEGER 4-byte binary 31 10 -2,147,483,648 2,147,483,647
BIGINT 8-byte binary 63 19 -9,223,372,036,854,775,808 9,223,372,036,854,775,807

Value to Represent Bits (2 bytes)


136 0000 0000 1000 1000

CREATE TABLE customers


(… id INTEGER,…)

13
Date and Time Data Types
• Represent dates and times
– Neither character nor numeric data types
– Do not specify scale or precision
• DATE CREATE TABLE customers

• TIME (… renewal DATE,


call_time TIME,
• TIMESTAMP last_update TIMESTAMP, …)

– Represents combination of date and time

14
Relational Model

15
Create Table Command
CREATE TABLE employees(
employee_id INTEGER,
first_name VARCHAR (30),
last_name VARCHAR (30),
birth_date DATE,
gender VARCHAR(1),
hire_date DATE,
pension_plan VARCHAR(1),
insurance_plan DECIMAL(1),
hourly_rate DECIMAL (5,2) );

16
Qualifying Table Names
• CREATE TABLE employees

• CREATE TABLE myschema.employees

17
Drop Table
• To delete (DROP) a table, the keywords DROP TABLE followed by
the name of the table
• Dropping the table also delete the data stored in the table

• DROP TABLE employees;

18
SQL - Free-Format Language
• One statement can occupy multiple lines and include blanks
between words
• Not case-sensitive

19
DML Statements
DML Language
• Data Manipulation Language

21
DML Statements
• The SELECT statement does not actually modify a database table
– Used to retrieve data from the database
• INSERT, UPDATE, and DELETE statements:
– Can modify a single row or set of rows in a table
– Cannot modify more than one table in a single statement
– The WHERE clause is used to specify the set of rows to be inserted,
updated, or deleted

22
INSERT Statement
• Insert (add) a row into a table
• The INSERT statement requires three values:
– Table name
– Column names in the table to populate (optional)
– VALUES clause specifies the actual data to be inserted
• Can add one row or several rows at a time
• INSERT statement can be specified two ways:
– Specifying column names with values (Explicit)
– Specifying values without column names (Implicit)
23
INSERT Statement
Explicit Column Names

24
Verify INSERT Worked

25
INSERT Statement
Implicit Column Names

26
INSERT Statement
Implicit Column Names

27
INSERT Statement
• Character and date values are encloded in single quotes
• Numeric data is not enclosed in quotes
• If a column list is not provided, a value must be assigned to each
column in the table

28
Apostrophe

29
INSERT Multiple Rows with Batch INSERT (DB2)

30
UPDATEing Data

31
Copy Tables Before Inserting
• Altering tables in your schema
– Make a copy of each table to complete the practice exercises
– Name each table copy_tablename

• The table copies do not inherit the associated primary-to-


foreign-key integrity rules of the original tables
• The column data types are inherited in the copied tables

32
Copy Tables Before Inserting
• Example:
CREATE TABLE copy_f_customers
AS (SELECT * FROM f_customers);

• To verify and view the copy of the table, use the following:

SELECT * FROM copy_f_customers;

33
UPDATE Statement
• Modifies existing rows in a table
• Requires four values:
– Table name
– Column names to be updated
– New value for each of the column(s) being modified
– WHERE clause that identifies which rows in the table will be modified

34
UPDATE Statement
One Row in the Table
• Update a specific row (primary key value in the WHERE clause)
• The SET clause specifies the column(s) to be updated

35
UPDATE Statement
One Row in the Table
• Change the phone number of one customer

36
UPDATE Statement
Multiple Rows in the Table

37
UPDATE Statement
One Row in the Table
• Change several columns and/or rows in one UPDATE
statement using a search condition

38
UPDATE Statement
• Which rows would be updated in the following transaction?

39
SET Clause with Multiple Columns

40
DELETE Statement
• Removes one or more existing rows from a table
• The statement requires two values:
– Table name
– WHERE clause that identifies the row(s) to be deleted

41
DELETE Statement

42
DELETE Statement
Multiple Rows from a Table
• Delete multiple rows in one DELETE statement using a
search condition

43
DELETE Statement
• What happends when this statement is executed?

44
DELETE Statement
• Clear all rows from a table – intentionally or accidentally –
by entering a DELETE statement with no WHERE clause

45
Integrity Constraint Errors
• Integrity constraints ensure that the data conforms to the
company's business rules
• Constraints are automatically checked whenever a DML
statement is executed against a column
• If any constraint rule would be broken as a result of the
update, the table is not updated and an error is returned

46
DBeaver
• DBeaver is a graphical user interface for managing and
administering databases
• It can be used to create, change, rename and delete SQL objects
• Among the supported SQL objects are schemas, tables, views
and indexes

47
48

You might also like