23
SQL Server Management Studio & SQL DDL 1 SQL Server Management Studio SQL DDL CREATE TABLE Constraints ALTER TABLE DROP TABLE The GUI way Steen Jensen, autumn 2013

SQL Server Management Studio & SQL DDL

  • Upload
    mostyn

  • View
    98

  • Download
    3

Embed Size (px)

DESCRIPTION

SQL Server Management Studio & SQL DDL. SQL Server Management Studio SQL DDL CREATE TABLE Constraints ALTER TABLE DROP TABLE The GUI way Steen Jensen, autumn 2013. SQL Server Management Studio. A lot of the time you will be working in the Query window - PowerPoint PPT Presentation

Citation preview

Data modeling using ER

SQL Server Management Studio & SQL DDL1SQL Server Management StudioSQL DDLCREATE TABLEConstraintsALTER TABLEDROP TABLEThe GUI way

Steen Jensen, autumn 2013

SQL Server Management StudioA 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!2SQL DDLSQL stands for Structured Query Language and can be divided into two groups of commands:DDL: Data Definition Language for handling the structureDML: 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.]]]ObjectNameA 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 optional3Creating tables overall structureThe command CREATE TABLE is used to create a table and have the below general structure:

4

CREATE TABLE selected partsIdentity: SQL Server automatically assigns a sequenced number for every inserted/new row (other DBMSs 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)5CREATE TABLE table & column namesSelected 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)

6CREATE TABLE data types 17

CREATE TABLE data types 28

CREATE TABLE data types 39

CREATE TABLE data types 410

Creating tables exampleThe below create command creates a table called Employees

11

Constraints primary keyDifferent constraints can be added for a table

Primary key constraints:

12

Constraints foreign keyForeign key constraints:

13

Constraints foreign key, self-referencingForeign key constraints self-referencing:

14

Constraints uniqueUnique constraints: Often referred as alternate keys

15

Constraints check 1Check constraints: Can make a lot of validation

16

Constraints check 2Below is a little subset of, what is possible using the check constraint

17

Constraints defaultDefault 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

Exercise in CREATE TABLEContinue with your Amazon system and create the necessary tables in SQL Server Management Studio19Altering tables overall structureThe command ALTER TABLE is used to change an existing table and have the below general structure:

20

Altering tables an exampleThe below figure shows two examples of adding new fields/columns to an existing table:

21

Deleting tablesThe command DROP TABLE is used to delete the structure and the content of an existing table:

22

The GUI way of doing thingsInstead of typing in SQL commands you can do it in a graphical way

Try this by following the instructions page 165bot 170bot

23