Presentation is loading. Please wait.

Presentation is loading. Please wait.

CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.

Similar presentations


Presentation on theme: "CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP."— Presentation transcript:

1 CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP

2 Agenda o Introduction to MS SQL Server and installation o Exercise o Assignment o Quiz

3 Syntax Conventions o See Table 1-5 on Page 14

4 Entity-Relationship (ER) o See Figure 1-1 on Page 19

5 SQL Server Management Studio o Windows Authentication o SQL Server Authentication o See Figure 2-1 and 2-2 on Page 26

6 Creating Database Objects o Two types of databases: o System Databases: master Tempdb Model msdb o User Databases: User created

7 Creating Database Objects (cont.) CREATE DATABASE db_name [ON [PRIMARY] file_spec1 {,file_spec2}…] [LOG ON file_spec3 {, file_spec4}…] [COLLATE collation_name] [FOR {ATTACH | ATTACH_REBUILD_LOG}] o db_name is database name. o LOG ON is to define where to keep the log. o COLLATE is to define the collation.

8 Creating Database Objects (cont.) o To create new database: USE master CREATE DATABASE cn2180 o To create a snapshot of the database: CREATE DATABASE database_name ON file_spec1 {, file_spec2}…] AS SNAPSHOT OF source_db_name

9 Creating Table CREATE TABLE tbl_name (col_name1 type1 [NOT NULL|NULL] [{, col_name2 type2 [NOT NULL|NULL]}…]) o By default all field are null able. o See Example 4.3 on P79. CREATE TABLE RMStudent(s_id integer NOT NULL, s_name char(20) NOT NULL, s_lname char(20) NOT NULL)

10 UNIQUE Clause o Candidate keys – all fields that has unique value and could be used as primary key. o Use UNIQUE clause with CREATE TABLE or ALTER TABLE statement. [CONSTRAINT c_name] UNIQUE [CLUSTERED | NONCLUSTERED] (col_name1 [{, col_name2} …]) o CONSTRAINT is to assign explicit name. o By default it is non clustered, which means the constraint will not be indexed.

11 PRIMARY KEY Clause o Use with CREATE TABLE or ALTER TABLE [CONSTRAINT c_name] PRIMARY KEY [CLUSTERED | NONCLUSTERED] (col_name1 [{,col_name2}…]) o Primary key must be NOT NULL USE cn2180 DROP TABLE RMStudent CREATE TABLE RMStudent(s_id integer NOT NULL, s_name char(20) NOT NULL, s_lname char(20) NOT NULL CONSTRAINT prim_sid PRIMARY KEY (s_id)))

12 FOREIGN KEY Clause [CONSTRAINT c_name] [[FOREIGN KEY] (col_name1 [{, col_name2}…]]) REFERENCES tbl_name (col_name3 [{, col_name4}…]) [ON DELETE {NO ACTION|CASCADE|SET NULL|SET DEFAULT}] [ON UPDATE {NO ACTION|CASCADE|SET NULL| SET DEFAULT}] o FOREIGN KEY must match the number and the data types of the columns in the REFERENCES clause.

13 FOREIGN KEY (Cont.) USE cn2180 CREATE TABLE ClassRm(c_id integer NOT NULL, c_loc char(20) NOT NULL, c_student char(20) NOT NULL, c_sid integer NOT NULL CONSTRAINT prim_cid PRIMARY KEY (c_id))) CONSTRAINT foreign_class FOREIGN KEY (c_sid) REFERENCES RMStudent (s_id))

14 Referential Constraints o Case 1: Add new data, new student, in foreign key field to ClassRm with data that is not exist in parent table (RmStudent). REJECT! o Case 2: Update the data in foreign key field with data that is not exist in parent table. REJECT! o Case 3: Update the primary key field in parent table to new value. The reference table has that key. REJECT! o Case 4: Delete the row of the parent table with the the primary key field that is referred from reference table. REJECT!

15 ON DELETE and ON UPDATE Options To work around referential constraints: o No Action – by default, you can modify only those value that is not referred to o Cascade – The data in parent table can be modified and will update the referencing table accordingly. o Set Null – All modification in parent table will affect the referencing table to be null if there is inconsistencies o Set Default – Same as Set Null, except the value are set to a default value.

16 ON DELETE and ON UPDATE Options USE cn2180 CREATE TABLE ClassRm(c_id integer NOT NULL, c_loc char(20) NOT NULL, c_student char(20) NOT NULL, c_sid integer NOT NULL CONSTRAINT prim_cid PRIMARY KEY (c_id))) CONSTRAINT foreign_class FOREIGN KEY (c_sid) REFERENCES RMStudent (s_id) ON DELETE CASCADE, CONSTRAINT foreign_class FOREIGN KEY (c_sid) REFERENCES RMStudent (s_id) ON UPDATE CASCADE)

17 CHECK Clause o Same as Validation Rule in MS Access. [CONSTRAINT c_name] CHECK [NOT FOR REPLICATIION] expression USE sample CREATE TABLE newtbl (cust_no INTEGER NOT NULL, cust_grp CHAR(3) NULL, CHECK (cust_grp IN (‘c1’,’c2’,’c10’)))

18 SQL Basic Objects o Comment /* */ o Reserved keywords o Data Type o Numeric – See page 53 o String – See page 54 o VARCHAR o Date and Time – See page 56 o User defined

19 SQL_VARIANT Data type o SQL_VARIANT can be used to store values of various data types at the same time.

20 Modifying database o Altering a table ALTER TABLE tbl_name ADD col_name type [NULL|IDENTITY] [{, col_name type [NULL|IDENTITY]}…] DROP COLUMN col_name [{, col_name} …] ALTER COLUMN col_name type {NULL|IDENTITY} [{, col_name type NULL|IDENTITY}…] ADD tbl_constraint DROP tbl_constraint

21 Assignment o Instruction: o Capture the screen for each question o Put question number under each screen o E.4.1 o E.4.6 – E.4.14 o E.4.16 – E.4.19


Download ppt "CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP."

Similar presentations


Ads by Google