Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

DATABASE DESIGN & SQL

Practical Exercise @5
CREATE TABLE USING VARIOUS CONSTRAINTS.
Database Design & SQL | Meet Chandrakantbhai Patel
Practical Activity @1
Create a table as shown in below figure using SQL script.
In SQL, we use CREATE TABLE command to create a new table.
Before creating a table we need to know table name, column names, data type of
every column and size.
Last thing we can able set table name in plural form which I use in ERD diagram as
singular.
Syntax of creating a table:
CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
column3 datatype,
....
);
For above table I use below syntax code to create table same like as above but I
didn’t set primary key
which I will set in practical activity @2.

CREATE TABLE Students_C0912711


(Student_ID VARCHAR(25),
FirstName VARCHAR(50),
LastName VARCHAR(100),
Telephone VARCHAR(10),
Age INTEGER,
City VARCHAR (10)
);
-Let see the result in Oracle software.
Database Design & SQL | Meet Chandrakantbhai Patel
Database Design & SQL | Meet Chandrakantbhai Patel
CREATE ANOTHER TABLE AS PER SHOWN IN FIGURE BELOW.

I write below syntax for above table to create the Courses table
CREATE TABLE Course_C0912711
(Student_ID VARCHAR(25),
CourseCode VARCHAR(50),
Marks INTEGER);
WRITE DESC table_name command
Database Design & SQL | Meet Chandrakantbhai Patel
Database Design & SQL | Meet Chandrakantbhai Patel
command to show table with
Database Design & SQL | Meet Chandrakantbhai Patel
Practical Activity @2
• Add a Primary Key Constraint to Student_ID (Table Level Constraint)
- Primary key is the combination of NOT NULL and UNIQUE. Uniquely identifies each
row in a table.
- Each table have only one Primary key constraint.
- There are two types to add constraint 1. Using constraint name 2. Without
constraint name
- but mostly I prefer to add constraint with constraint name.it is easier to remove
constraint any time using its
name.
-SYNTAX of set primary key constraint at table level
CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
column3 datatype,
....
CONSTRAINT constraint_name PRIMARY KEY(column_name)
);
Let’s we try using above syntax to create STUDENTS_C0912711 with student_ID
primary key.
Firstly, we need to drop STUDENTS_C0912711 table because earlier created
this table.

You might also like