sql table basics. database objects tables temporary tables (begin with #) views keys indexes

17
SQL Table Basics

Post on 21-Dec-2015

231 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

SQL

Table Basics

Page 2: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Database Objects

• Tables

• Temporary tables (begin with #)

• Views

• Keys

• Indexes

Page 3: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Tables

CREATE TABLE table_name

(

applicant_name CHAR(50) NOT NULL,

applicant_address CHAR(255) NULL

)

DROP TABLE table_name

Page 4: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Object Names

• All SQL Server object names are up to 30 characters in length, and are case-sensitive. No embedded blanks or punctuation allowed.

• Full pathname:

database_name.owner_name.object_name

Page 5: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

System Procedures

• The Swiss Army Knife: sp_help

• Without parameters: all objects

• With parameters: structure of object

sp_help table_name

Page 6: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Basic Datatypes

• CHAR(): a fixed-length string field.

• VARCHAR(): a variable-length string field.

• TEXT: strings up to 2GB. Note – difficult to manage in T-SQL. Easy through ODBC.

• INTEGER: -231 to 231

• DATETIME: date and time. See convert command for formats.

Page 7: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Column Properties

• NULL (a value does not need to be specified)

• NOT NULL (a value must be specified)• IDENTITY: sequential, unique numbering.

identity(seed,increment)

CREATE TABLE table_name(Column_name int identity(1,1) )

Page 8: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

NULL• If possible, avoid allowing null columns.

• 3 valued logic = complexity and bugs.create table truth_table (

x int NULL )

go

insert truth_table(x) values(NULL)

go

select 1 from truth_table where (x >= 0 or x <= 0)

go

What does the select statement return?

Page 9: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Temporary Tables• Temporary tables are like regular tables, but they

are for temporary storage of records. • All SELECT, INSERT, UPDATE and DELETE

operations are allowed on them.• If not explicitly dropped, they will be dropped by

the server when the batch completes.• Are created in the database tempdb.• Most importantly, the table name begins with a

pound sign (#).• CREATE TABLE #temp(a int,b int)

Page 10: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Views

• A named select statement, a virtual table.CREATE VIEW view_name

(

SELECT a.applicant_name,b.skill_code

FROM applicant a, applicant_skill b

WHERE a.applicant_id = b.applicant_id

)

go

select * from view_name order by skill_code

go

Page 11: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Primary Keys• PKs require all values in a column be

unique. Defaults to a clustered index.ALTER TABLE table_name

ADD CONSTRAINT PK_table_name_id

PRIMARY KEY (table_name_id)

sp_helpconstraint table_name

ALTER TABLE table_name DROP CONSTRAINT PK_table_name_id

Page 12: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Foreign Key• One or more columns of a table whose

values must be equal to a primary key in another table.

• SQL Server enforces the referential integrity.

ALTER TABLE table_name

ADD CONSTRAINT FK_table_name_id

FOREIGN KEY (table_name_id)

Page 13: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Indexes

• Maintain uniqueness of the indexed columns (primary keys).

• Provide fast access to the tables. Avoidance of evil table scans.

CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX table_name_idx ON table_name (column [,…])

Page 14: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Clustered Indexes

• One per table.

• Physical sort of table data on indexed column.

• Good for range retrievals because of adjacency.

Page 15: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

From SQL Server 6 Unleashed.

Page 16: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

Nonclustered Indexes

• Separate structure independent of physical sort order.

• Up to 249 nonclustered indexes on a table.

• Index keys are in sorted order.

Page 17: SQL Table Basics. Database Objects Tables Temporary tables (begin with #) Views Keys Indexes

From SQL Server 6 Unleashed