Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Lab1 Intro SQL&Ddl

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab1 Intro SQL&Ddl

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab #1

• Installing SQL Server creates system databases as (master , model , tempdb and msdb) and
sample user databases such as:( pubs and Northwind)

1. There are 3 types of statements to use in SQL :


1. DDL Statements Create, Alter, Drop, and Truncate Tables. And Create, Drop
Views, Indices, and Constraints.
2. DML Statements: Insert , Update , Delete and Select from Tables or Views

Data Definition Language(DDL) Statements:-


It is used to create and modify the structure of database objects in database. Includes statements
like:
3. CREATE: Creates a new table or any other database object
4. ALTER: Modifies an existing database object, such as a table
5. DROP: Removes an entire table or any other object in database

Common Data Types for table attributes are:


1- Numeric: integer number ( INT, SMALLINT AND BIGINT), and floating number (FLOAT,
REAL).
2- Character: data types are either fixed length (CHAR (n), where n is the number of character) or
variable length (VARCHAR(n)).
3- Boolean: TRUE or FALSE.

SQL Constraint:-
Rules enforced on data columns on tables. These are used to limit the type of data that can go into a
table. This ensures the accuracy and reliability of the data in the database.
The following are commonly used constraints available in SQL:

• NOT NULL: A Constraint that ensures that a column cannot have NULL value.
• DEFAULT: A Constraint that provides a default value for a column when none is specified.
• UNIQUE: A Constraint that ensures that all values in a column are different or NULL.
• PRIMARY Key: A Constraint that uniquely identify each row/record in a database table (NOT
NULL + UNIQUE)
• FOREIGN KEY (FK): A Constraint that ensures referential integrity. A foreign key from 1
table to another is used link a tuple in the 1st table to a unique tuple in the 2nd table.
• CHECK: A constraint that ensures that all values in a column satisfy a certain condition.
Constraints can be specified within the CREATE TABLE statement after the attributes are
declared, or they can be added later using the ALTER TABLE command.

Eng-Abdallah Hamed database


DDL Synatx
1. Creating A New Database:
CREATE DATABASE <Database_Name>
2. Dropping Database: Used to drop an existing database.
DROP DATABASE <database_name >
3. Creating Tables :Used to create a table with a specific structure inside a selected database

CREATE TABLE <tablename>


(
<column name> <data type> [<column constraint>…]
PRIMARY KEY <column list>
FOREIGN KEY <column list> REFERENCES <table specifications>
)
Ex:
CREATE TABLE STAFF
(
STAFF_ID int Primary Key,
STAFF_NAME char(20) Not Null,
STAFF_SALARY real Null,
STAFF_PHOTO image Null
)

4. Altering Table: Used to change the table structure i.e. add column, change data type of a
column, or remove existent column.
• ALTER TABLE<table name>
ADD <column name> <data type>

• ALTER TABLE<table name>


DROP COLUMN <coulmn name>

• ALTER TABLE<table name>


ALTER COLUMN <column name> <new data type>

Ex: Add the column STAFF_EMAIL to the table STAFF

ALTER TABLE STAFF


ADD STAFF_EMAIL char(30)

5. Dropping Table: Used to drop a table structure and all the data stored in it:
DROP TABLE <table name>

Ex:
DROP TABLE STAFF

Eng-Abdallah Hamed database


Ex1: Consider the following schema of database “University”:
Student(SSN, Name, City, Age)
Course(CrsCode, Name(
Registered(SSN, CrsCode, Semester, Year)
Department(DeptCode, Name)
• Write SQL DDL statement for declaring the University Database and relations.
Specify appropriate keys and referential integrity constraints
• Note that: you must ensure that the Age of student cannot be below 18 years.

CREATE database University

--DEPARTMENT Table
CREATE TABLE Department(
DeptCode VARCHAR(5) NOT NULL PRIMARY KEY ,
Name VARCHAR(15) NOT NULL,
)
--Course Table
CREATE TABLE Course
(
CrsCode SMALLINT NOT NULL,
Name VARCHAR(45) UNIQUE,
PRIMARY KEY (CrsCode)
)
--Student Table
CREATE TABLE Student
(
SSN INT NOT NULL,
Name VARCHAR(45),
Age INT CHECK (AGE >= 18),
City VARCHAR(15) DEFAULT 'CAIRO',
)
--Add PK to Student table
ALTER TABLE STUDENT
ADD PRIMARY KEY (SSN),
--OR
ALTER TABLE STUDENT
ADD CONSTRAINT PK1 PRIMARY KEY (SSN) -- constraint name is unique across database

--Registered Table
CREATE TABLE Registered
(
SSN INT NOT NULL,
CrsCode SMALLINT NOT NULL REFERENCES COURSE(Crscode),
Semester VARCHAR (45) NOT NULL,
Year VARCHAR (45)
)

Eng-Abdallah Hamed database


-- Add pk and fk constraints to Registered Table
ALTER TABLE Registered
ADD PRIMARY KEY (SSN, CrsCode)
ALTER TABLE Registered
ADD FOREIGN KEY (SSN) REFERENCES Student (SSN)
ALTER TABLE Registered
ADD FOREIGN KEY (CrsCode) REFERENCES Course (CrsCode)

ALTER EXAMPLES:

• Add Anew field in “Department” Table to store Number of Student for


each Department.
 ALTER TABLE Department
ADD numberofstudents int

• Delete “Major” Column from “Student” Table.


 ALTER TABLE Student
DROP COLUMN Major

• Change the data type of Column “Name” in table “Student” to be


VarChar(100).
 ALTER TABLE Student
ALTER COLUMN [Name] VARCHAR (100)

• Add a check constraint to make sure that "DeptCode" will only take one
of these codes (IS, CS, IT, DS).
 ALTER TABLE DEPARTMENT
ADD CONSTRAINT CK_DEPTCODE CHECK (DEPTCODE IN
('IS','CS','IT','DS'))

• Delete the constraint that you created in the previous example


 ALTER TABLE DEPARTMENT
DROP CONSTRAINT CK_DEPTCODE

Eng-Abdallah Hamed database

You might also like