creating tables, setting constraints, and datatypes what is a constraint and why do we use it? what...

26
Creating Tables, Creating Tables, Setting Constraints, Setting Constraints, and Datatypes and Datatypes What is a constraint and why do we What is a constraint and why do we use it? use it? What is a datatype? What is a datatype? What does CHAR mean? What does CHAR mean? Page 97 in Course Guide

Upload: tre-horn

Post on 31-Mar-2015

228 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

Creating Tables, Setting Creating Tables, Setting Constraints, and DatatypesConstraints, and Datatypes

Creating Tables, Setting Creating Tables, Setting Constraints, and DatatypesConstraints, and Datatypes

What is a constraint and why do we use it?What is a constraint and why do we use it?

What is a datatype?What is a datatype?

What does CHAR mean?What does CHAR mean?

Page 97 in Course Guide

Page 2: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 2

What can this mean?

Page 3: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 3

Language explained

Language explained

CREATE TABLE tablename (column1 datatype, column2 datatype, column3 datatype );

create table means create a table( begin defining fields with a () end of fields is signaled by ); end of statement

Add the ) mark!

Page 4: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 4

Validation, Constraints

• The database automatically checks that entered data is appropriate to the field type

• If the field is a phone number, you can create a constraint that input is to be numbers only and no letters are allowed.

• We will not cover "Input Masks" this semester

NOT NULL means that the column must have a value in each row.

If NULL is used, that column may be left empty in a given row.

Page 5: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 5

What is a constraint?

A constraint is basically a rule associated with a column that the data entered into that column must follow.

• "Not Null" -- a column can't be left blank

• VARCHAR(30) -- entry of varying length, maximum possible is 30 characters, can be less

• See SAMS book, lessons 17 and 22

Page 6: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 6

Design View as it looked in MySQL-Front

: • How do we determine what to accept in each field?

• These are the table properties:

Unsigned means than no sign is accepted in

front of a number. That means it won’t

accept –2, for example.

SAMS Lesson 17

Page 7: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 7

Doing nearly the same thing in SQLyog

DemoForClass CREATE TABLE 'DemoForClass' ( ‘FirstName' varchar(12) NOT NULL default 'First Name', 'LastName' varchar(12) NOT NULL default 'Last Name', 'Year' year(4) NOT NULL default '2006', 'SSN' varchar(11) NOT NULL default '000-00-0000')

Not in your Course Guide

Page 8: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 8

What the table looks like

Not in your Course Guide

Note: the name Kingfishersmith was truncated

Page 9: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 9

Creating a Table using MySQL-Front

If this box is checked, then the User cannot leave the field empty

Page 10: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 10

Creating a table in SQLyog

Not in your Course Guide

Page 11: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 11

Number value in a field

No negative numbers allowed, and field will not take letters!

PK is for Primary Key

Binary is for pictures, etc.

Page 12: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 12

Starting to make sense?

CREATE TABLE employee

(ssn CHAR(11) NOT NULL, first VARCHAR(15),

last VARCHAR(20) NOT NULL,

age INT(3),

address VARCHAR(30),

city VARCHAR(20),

state CHAR(2));

Field names:

Table name

Datatype:

*Note: MySQL may change CHAR to VARCHAR in this situation—see the MySQL Manual.

Page 13: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 13

"column1" "datatype" [constraint],

ssn CHAR(11) NOT NULL,

Language layout

Page 14: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 14

Default value in SQL

CREATE TABLE tablestudents (FirstName VARCHAR (12)DEFAULT ‘First Name’ NOT NULL (etc.)

Page 15: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 15

NULL Values and calculations

• If you add a value to another value that is NULL, the answer that MySQL gives you is NULL! The same thing sometimes happens in our Compass GradeBook—if a score is missing, the total score is not computed. It all depends on what software you use.

Page 16: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 16

Alter Table – Using a Query

Page 17: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 17

Variation 1 of the Insert Statement

• If you know the order of the fields, you don’t have to specify the field names

INSERT INTO CustomersVALUES('1000000001', 'Village Toys', '200 Maple

Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', '[email protected]');

Page 18: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 18

Variation 2 of Insert Statement

• You don’t have to insert a value for every field, unless it is a required (NOT NULL, PRIMARY KEY) field

INSERT INTO Customers(cust_id, cust_name, cust_email)VALUES('1000000001', 'Village Toys',

'[email protected]');

Page 19: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 19

Update Statement

• See page 131 and 132 of SAM’s book

• Modifies data in a table

UPDATE players SET firstname= 'fred'WHERE ssn='899-98-9989'

Page 20: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 20

Before running Update

Not in your Course Guide

Page 21: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 21

After running the statement

Not in your Course Guide

Page 22: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 22

Primary Key as shown in old clientPrimary Key as shown in old client

The Primary Key specifies the field that uniquely identifies each record in the Table

Page 23: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 23

Setting a Primary Key Setting a Primary Key

• A primary key is a column (field) that uniquely identifies the rest of the data in any given row.

• More than one field can be combined to establish a primary key (e.g., Deanne + Smith rather than Smith)

• (MySQL may give you trouble, but trust us)

Page 24: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 24

Datatypes

Datatypes specify what the type of data can be for that particular field.

• A field called "Last_Name“ should have a "VARCHAR" (variable-length character) datatype.

• A field called “SSN” should have a “char” datatype--that would constrain the size to exactly 9 or 11 characters. Except, when we tried to do that with a primary key, MySql-Front balked. So far, SQLyog is OK.

Page 25: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 25

Popular datatypes – see Appendix D

• char(size) Fixed-length character string. Size is specified in parenthesis. Max 255 bytes.

• varchar(size) Variable-length character string. Max size is specified in parenthesis.

• TEXT – a character string that does not have a fixed length

Page 26: Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course

CS 105 Spring 2006 Slide 26

More datatypes

• number(size) • unsigned (no negative numbers)

– Tinyint (integer)• This is very complicated—do not worry

about it for our course. For details see http://www.mysql.com/doc/

• date Date value– Timestamp ---YYYY-MM-DD