Sei sulla pagina 1di 21

Database Fundamentals

Module 2: Getting Started with Tables


Chris Randall and Graeme Malcolm
Sales.Product
ProductID Name Price
1 Widget 12.99
2 Thingybob 3.75
3 Knicknack
4 Wotsit

CREATE TABLE Sales.Product


(ProductID INTEGER IDENTITY PRIMARY KEY,
Name VARCHAR(20),
Price DECIMAL NULL);
Sales.Product
ProductID Name Price Supplier
1 Widget 12.99 1
2 Thingybob 3.75 2
3 Knicknack 1
4 Wotsit 1

DROP
ALTERTABLE
TABLESales.Product;
Sales.Product
ADD Supplier INTEGER NOT NULL
CONSTRAINT def_supplier DEFAULT 1;
Create tables using the CREATE TABLE statement
• Specify a table name and column specifications
Modify table definition using the ALTER TABLE statement
• Add or remove columns, constraints, keys
Remove tables using the DROP TABLE statement
• Removes the table from the database
Product
ProductID Name Price Supplier
1 Widget 12.99 1
2 Thingybob 3.75 2
3 Knicknack NULL 1
4 Wotsit NULL 1
!! ERROR !!

INSERT INTO Product (Name)


(Name, Price, Supplier)
VALUES (‘Doodah’,
(‘Widget’, 0.00,
(‘Thingybob’,
(‘Knicknack’);
(‘Wotsit’, 12.99,
‘Free’,
NULL,
3.75,
NULL);
DEFAULT);
1);
1);
2);
Product
ProductID Name Price Supplier
1 Widget 12.99 1
2 Thingybob 3.75 2
3 Knicknack NULL 1
4 Wotsit NULL 1

SELECT Name,
Name Price
ProductID,
* FROMASProduct;
Product,
Name, Price
FROM Product
Product;
Price * 0.9 AS SalePrice
WHERE Supplier = 2;
FROM Product;
ProductID Name
Product Price
SalePrice Supplier
1 Thingybob
Widget 3.75
12.99
11.691 1
2 Thingybob 3.75
3.375 2
3 Knicknack NULL 1
4 Wotsit NULL 1
Insert data into a table using the INSERT statement
• Specify explicit columns where others support defaults or NULLs
Select data from a table using the SELECT statement
• Specify the columns or expressions you want to return
• Use aliases for column names in the result set
• Use the WHERE clause to filter rows
Creating a Table

Creating a table, and inserting and querying data


So one of the things we had to
do when creating a table was
to specify the column data That’s right. Let’s take a closer
types. look at the types of data we can
store in a table.
Character Strings Phone Email Transcript LastName
555-12345 joe@contoso.com {…} Smith
• Fixed length 555-54321 mary@adatum.org {…} Ásbjörnsson

• Variable length 555-11111 u1@northwind.com {…} 愛佳


555-55555 bill5@litwareinc.com {…} ‫عبد الحميد‬
• Large text
• Unicode Lorem ipsum
Lorem ipsum
dolor sit amet,
Lorem ipsum
dolorconsectetuer
sit amet,
Lorem ipsum
dolor sit amet,
consectetuer
adipiscing elit.
dolorconsectetuer
sit amet,
adipiscing
Maecenaselit.
consectetuer
adipiscing elit.
Maecenas
adipiscing elit.porttitor
Maecenas
porttitor
congue massa.
Maecenas
porttitor
congue massa.
porttitor
congue massa.
congue massa.
Numbers UnitsInStock Cost MarketInMillions
198 129.2701 23.5
• Integers 12 109.8923 12.1

• Exact decimals 67 27.6555 5.2


103 4.7600 69.3
• Approximate decimals
Temporal Values BirthDate ShiftStart OrderDate MeetingTime
1971-03-07 09:00 2016-04-01 09:17:00 2016-04-01 09:00 +8:00
• Dates 1987-12-18 10:30 2016-04-01 09:19:00 2016-04-01 09:00 +100

• Times 1967-07-22 11:15 2016-04-01 10:01:35 2016-04-01 09:00 +6:00


2005-03-17 12:05 2016-04-01 10:32:23 2016-04-01 09:00 +3:00
• Date and Time
• Offsets
Others InStock Photo StockID Spec Location Log
1 110101.. 12F51A28-12B.. <prd id=“1”/> 55.95,-3.188 1460077
• Bit (True/False) 0 111011.. 32BC762E-569.. <prd id=“3”/> 47.59,-122.3 1460198
• Binary 1 001010.. 734736BD-125.. <prd id=“4”> 51.51,-0.102 1476352
1 111110.. 329DE521-BC4.. <prd id=“2”/> -33.86,151.2 1479734
• Unique Identifier
• XML
• Spatial
• Timestamp
Choose the most appropriate data type and size for each
column
• SQL Server provides data types for strings, numbers, dates, and others
• Specify an appropriate size that will support all data without wasted space
Some data types can be implicitly converted, others must be
explicitly converted
• SQL Server provides functions to perform data conversion
Data Types

Working with data types


Product Supplier
ProductID Name Price Supplier SupplierID Name Phone
1 Widget 12.99 1 1 Contoso 555-12345
2 Thingybob 3.75 2 2 Northwind 555-54321
3 Knicknack NULL 1 3 A Datum 555-55555
4 Wotsit NULL 1
5 Doodah 1.99 3
!! ERROR !!
INSERT INTO
CREATETABLE
ALTER Product
TABLESupplier
Supplier
VALUES (‘Doodah’,
(‘McGuffin’,
(SupplierID
ADD CONSTRAINT
(3,
(2, 1.99,
‘Contoso’,
‘A
INTEGER 2.99,
(1, ‘Northwind’,
Datum’, 3);
5);
fk_product_supplier
PRIMARY
‘555-12345’);
‘555-55555’);
‘555-54321’);
KEY,
FOREIGN
Name VARCHAR(20)
KEY (Supplier)
NOT NULL,
REFERENCES Supplier(SupplierID);
Phone CHAR(9) NULL);
Most databases contain multiple tables
• This is a direct result of normalization
Use keys to determine relationships between rows in different
tables
• A primary key identifies a unique row in a table
• A foreign key references a row in another table
Create foreign key constraints to enforce referential integrity
• This prevents invalid data insertions
• It can be used to prevent deletions or updates that will “orphan” rows
So by creating a relationship
between primary keys and Right. This is often called
foreign keys, we can prevent enforcing referential integrity.
invalid data being inserted? Let’s see it in action…
Referential Integrity

Creating a Foreign key constraint


Use the CREATE TABLE statement to create tables
Use the INSERT statement to add rows to a table
Use the SELECT statement to retrieve rows from a table
Use constraints that define relationships between primary
keys and foreign keys to enforce referential integrity
© 2016 Microsoft Corporation. All rights reserved. The text in this document is available under the Creative Commons Attribution 3.0 License, additional terms may apply. All other content contained in this
document (including, without limitation, trademarks, logos, images, etc.) are not included within the Creative Commons license grant. This document does not provide you with any legal rights to any
intellectual property in any Microsoft product. You may copy and use this document for your internal, reference purposes.
This document is provided "as-is." Information and views expressed in this document, including URL and other Internet Web site references, may change without notice. You bear the risk of using it. Some
examples are for illustration only and are fictitious. No real association is intended or inferred. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Potrebbero piacerti anche