SQL Server Management Studio & SQL DDL

advertisement
•SQL Server Management
•SQL DDL
•CREATE TABLE
•Constraints
•ALTER TABLE
•DROP TABLE
•The GUI way
Studio
Steen Jensen, autumn 2013
1




A lot of the time you will be working in the Query window
Most of the commands can be executed in a graphical way, but start
training typing in the SQL commands
The management studio is huge and a lot of functionality is aimed for
administrators
And now for a live demo!
2



SQL stands for Structured Query Language and can be divided into two
groups of commands:
• DDL: Data Definition Language for handling the structure
• DML: Data Manipulation Language for handling the content
In SQL Server Management Studio you will be using a dialect of SQL
called T-SQL (Transact SQL), which is largely compliant with ANSI/ISO
SQL standards
All queries work on objects – a fully qualified object name:
• [ServerName.[DatabaseName.[SchemaName.]]]ObjectName
• A schema can be used for separating database objects into logical
groups – default schema = dbo (= database owner)
• An object name must always be part of a query – the rest are optional
3

The command CREATE TABLE is used to create a table and have the
below general structure:
4



Identity:
• SQL Server automatically assigns a sequenced number for every
inserted/new row (other DBMS’s calls this “auto_increment”)
• Seed is the start value, and increment how much it should be
increased
Computed columns:
• Used for derived attributes/columns
Column constraints:
• Covered in chapter 6 (e.g. Primary key)
5

Selected rules for table & column names:
• Capitalization: start each word with a capital letter (“camel casing”)
• Name length: keep names as short as possible
• Limit abbreviations: only use well-known abbreviations (e.g. “no” )
• Eliminate spacing between words: use camel casing
• Avoid underscore : “tricky” to type, difficult to read (underline)
6
7
8
9
10

The below create command creates a table called Employees
11

Different constraints can be added for a table

Primary key constraints:
12

Foreign key constraints:
13

Foreign key constraints – self-referencing:
14

Unique constraints:
• Often referred as alternate keys
15

Check constraints:
• Can make a lot of validation
16

Below is a little subset of, what is possible using the check constraint
17

Default constraints:
• If a value is supplied in an insert, then no default value will be used
• If no value is supplied in an insert, then the default value will be used
18

Continue with your Amazon system and create the necessary tables in
SQL Server Management Studio
19

The command ALTER TABLE is used to change an existing table and
have the below general structure:
20

The below figure shows two examples of adding new fields/columns to
an existing table:
21

The command DROP TABLE is used to delete the structure and the
content of an existing table:
22

Instead of typing in SQL commands you can do it in a graphical way

Try this by following the instructions page 165bot – 170bot
23
Download