Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch 3. Working with Tables and Views. Data type Specify type of data to be entered into a column (text, number, datetime, etc) Unicode (National) Datatypes.

Similar presentations


Presentation on theme: "Ch 3. Working with Tables and Views. Data type Specify type of data to be entered into a column (text, number, datetime, etc) Unicode (National) Datatypes."— Presentation transcript:

1 Ch 3. Working with Tables and Views

2 Data type Specify type of data to be entered into a column (text, number, datetime, etc) Unicode (National) Datatypes allow user to enter data in another language (nText, nChar, nVarchar)

3 Data type Common datatypes DataTypeRangeSize BitTrue/false1 bit Int (Integer)whole number -2^31 thru 2^314 byte DecimalPartial number -10^38 thru 10^38 -15-17 bytes DateTimeStore date and time accurate to 0.003 s8 byte SmallDateTimeStore date and time accurate to 1 minute4 byte VarcharAny text in EnglishDepend on number of characters max 4000 nVarcharAny text in any languageDepend on number of characters max 4000 nTextAny text in any language2gb

4 Creating tables Open SSMS, Right click Sybex in Object Explorer Select New Table to open table designer In the first row, under Column Name, enter ProductID Select Int as Data Type Under Column Properties, expand Identity Specification, and change (is Identity) to YES. This gives us an auto-incrementing ID

5 Creating tables In the 2 nd row, under Column Name, enter Product, Select varchar(100) as data type In the 3 rd row, under Column Name, enter InStock, select bit as datatype Uncheck all Allow Nulls Click Save button in tool bar, save as Products

6 Input Data into a table Right Click on Products table in Object explorer, Select Open Table Enter the following data, an ID is automatically generated for each product

7 Exercise: Create a Customers Table

8 Exercise: Create an Orders Table

9 Check Constraint Checks and restricts data to be entered into a table. for a Zip Code field: “91763” ok “Abcde” not ok

10 Check Constraint In Object Explorer, expand Sybex database -> Tables -> dbo.Customers Expand dbo.Customers table Right-click New Constraint In New Constraint dialog box, enter CK_Zip as the name of the constraint

11 Check Constraint Enter (Zip like ‘[0-9][0-9][0-9][0-9][0-9]’) in Expression text box Enter “Check for valid zipcode” as description Click Close

12 Check Constraint To test your check constraint: Open Customers table Enter following data into the table FirstName: Gary LastName: McKee Address: 111 Main City: Palm Springs State: CA Zip: AAB1C Phone: 7605551212 Go to next row, an error will occur Change Zip to 94312 to add the data

13 Default Constraint A default value to be automatically entered into a table. In Customers table, if user does not specify which State, automatically enter “CA”

14 Default Constraint In Object Explorer, expand Sybex database -> Tables -> dbo.Customers Right-click dbo.Customers table, Select Modify Select State Column Under column properties, in Default Value of Binding, type “CA” Close window and save.

15 Default Constraint To test your default constraint: Open Customers table Enter following data into the table FirstName: Tom LastName: Smith Address: 609 Georgia City: Fresno State: Zip: 33405 Phone: 5105551212

16 Default Constraint Right-click anywhere on the screen, select Execute SQL “CA” is automatically entered into state field.

17 Views A virtual table contains a subset or a superset of tables Usually read-only (recommended) An permanent table is created internally

18 Creating a View In Object Explorer, expand AdventureWorks database Right-click on Views, select New View In Add Table Dialog Box, select Contact, and click Add Click Close to open View Designer

19 Creating a View Check LastName, FirstName, Phone in Contact (Person) window Execute view (Ctrl-R) Notice there are 19972 records

20 Creating a View

21 Filter a View We use filter in a view to display only a subset of data In View Designer, move to Phone column, under filter, enter Like '398%‘ Ctrl-R will now return only 13 records (only phone numbers start with ‘398’ is displayed ) Close View designer and save view as Contact_in_398

22 To open a view from SSMS Expand Database->Adventureworks- >Views Right Click dbo.Contacts_in_398, select Open View

23 Creating a Joined View Tables can be Joined with a view Input follow data into Sybex -> Orders

24 Creating a Joined View In Object Explorer, expand Sybex database Right-click on Views, select New View In Add Table Dialog Box, select Customers, and click Add In Add Table Dialog Box, select Orders, and click Add In Add Table Dialog Box, select Products, and click Add Click Close to open View Designer

25 Creating a Joined View Drag CustomerID in Customers to CustomerID in Orders Drag ProductID in Products to ProductID in Orders Select Firstname, LastName,Product,InStock, OrderDate Execute the view

26 Creating a Joined View


Download ppt "Ch 3. Working with Tables and Views. Data type Specify type of data to be entered into a column (text, number, datetime, etc) Unicode (National) Datatypes."

Similar presentations


Ads by Google