Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 Guide

2 Slide 2 CS 105 Spring 2006 What can this mean?

3 Slide 3 CS 105 Spring 2006 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!

4 Slide 4 CS 105 Spring 2006 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.

5 Slide 5 CS 105 Spring 2006 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

6 Slide 6 CS 105 Spring 2006 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 wont accept –2, for example. SAMS Lesson 17

7 Slide 7 CS 105 Spring 2006 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 ' ') Not in your Course Guide

8 Slide 8 CS 105 Spring 2006 What the table looks like Not in your Course Guide Note: the name Kingfishersmith was truncated

9 Slide 9 CS 105 Spring 2006 Creating a Table using MySQL-Front If this box is checked, then the User cannot leave the field empty

10 Slide 10 CS 105 Spring 2006 Creating a table in SQLyog Not in your Course Guide

11 Slide 11 CS 105 Spring 2006 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.

12 Slide 12 CS 105 Spring 2006 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 situationsee the MySQL Manual.

13 Slide 13 CS 105 Spring 2006 "column1" "datatype" [constraint], ssn CHAR(11) NOT NULL, Language layout

14 Slide 14 CS 105 Spring 2006 Default value in SQL CREATE TABLE tablestudents (FirstName VARCHAR (12) DEFAULT First Name NOT NULL (etc.)

15 Slide 15 CS 105 Spring 2006 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 GradeBookif a score is missing, the total score is not computed. It all depends on what software you use.

16 Slide 16 CS 105 Spring 2006 Alter Table – Using a Query

17 Slide 17 CS 105 Spring 2006 Variation 1 of the Insert Statement If you know the order of the fields, you dont have to specify the field names INSERT INTO Customers VALUES (' ', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith',

18 Slide 18 CS 105 Spring 2006 Variation 2 of Insert Statement You dont 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_ ) VALUES (' ', 'Village Toys',

19 Slide 19 CS 105 Spring 2006 Update Statement See page 131 and 132 of SAMs book Modifies data in a table UPDATE players SET firstname= 'fred' WHERE ssn=' '

20 Slide 20 CS 105 Spring 2006 Before running Update Not in your Course Guide

21 Slide 21 CS 105 Spring 2006 After running the statement Not in your Course Guide

22 Slide 22 CS 105 Spring 2006 Primary Key as shown in old client The Primary Key specifies the field that uniquely identifies each record in the Table

23 Slide 23 CS 105 Spring 2006 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)

24 Slide 24 CS 105 Spring 2006 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.

25 Slide 25 CS 105 Spring 2006 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

26 Slide 26 CS 105 Spring 2006 More datatypes number(size) unsigned (no negative numbers) –Tinyint (integer) This is very complicateddo not worry about it for our course. For details see date Date value –Timestamp ---YYYY-MM-DD


Download ppt "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."

Similar presentations


Ads by Google