Download presentation
Presentation is loading. Please wait.
1
Database Management System
Lecture - 26 © Virtual University of Pakistan
2
© Virtual University of Pakistan
DDL Create Alter Drop © Virtual University of Pakistan
3
© Virtual University of Pakistan
Create Command Creating database CREATE DATABASE db_name Create database exam Next step is to create tables Two approaches: Through SQL Create command Through Enterprise Manager © Virtual University of Pakistan
4
© Virtual University of Pakistan
Create Table Command Create table command is used to: Create a table Define attributes of the table with data types Define different constraints on attributes, like primary and foreign keys, check constraint, not null, default value etc. © Virtual University of Pakistan
5
© Virtual University of Pakistan
Format of Create Table CREATE TABLE [ database_name.[ owner ] . | owner. ] table_name ( { < column_definition > | column_name AS computed_column_expression | < table_constraint > } | [ { PRIMARY KEY | UNIQUE } [ ,...n ] ] ) © Virtual University of Pakistan
6
© Virtual University of Pakistan
Chevrons Explained < column_definition > ::= { column_name data_type } [ DEFAULT constant_expression ] [ < column_constraint > ] [ ...n ] © Virtual University of Pakistan
7
© Virtual University of Pakistan
Chevrons Explained < column_constraint > ::= [ CONSTRAINT constraint_name ] { [ NULL | NOT NULL ] | [ { PRIMARY KEY | UNIQUE } ] | [ [ FOREIGN KEY ] REFERENCES ref_table [ ( ref_column ) ] [ ON DELETE { CASCADE | NO ACTION } ] [ ON UPDATE { CASCADE | NO ACTION } ] ] | CHECK( logical_expression ) } © Virtual University of Pakistan
8
© Virtual University of Pakistan
Create Table Command CREATE TABLE Program ( prName char(4), totSem tinyint, prCredits smallint) © Virtual University of Pakistan
9
© Virtual University of Pakistan
Create Table Command CREATE TABLE Student (stId char(5), stName char(25), stFName char(25), stAdres text, stPhone char(10), prName char(4) curSem smallint, cgpa real) © Virtual University of Pakistan
10
Create Table with Constraint
CREATE TABLE Student ( stId char(5) constraint ST_PK primary key constraint ST_CK check (stId like ‘S[0-9][0-9][0-9][0-9]'), stName char(25) not null, stFName char(25), stAdres text, stPhone char(10), prName char(4), curSem smallint default 1, cgpa real) © Virtual University of Pakistan
11
© Virtual University of Pakistan
stId char(5) constraint ST_PK primary key stId char(5) primary key © Virtual University of Pakistan
12
Create Table with Constraint
CREATE TABLE Student (stId char(5) primary key check (stId like 'S[0-9][0-9][0-9][0-9]'), stName char(25) not null, stFName char(25), stAdres text, stPhone char(10), prName char(4), curSem tinyint default 1, cgpa real) © Virtual University of Pakistan
13
Create Table with Constraint
CREATE TABLE semester ( semName char(5) primary key, stDate smalldatetime, endDate smalldatetime, constraint ck_date_val check (datediff(week, stDate, endDate) between 14 and 19)) © Virtual University of Pakistan
14
Database Management System
Lecture - 26 © Virtual University of Pakistan
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.