Skip to content

Commit d5462ca

Browse files
committed
completed till constraints
1 parent 16c5d76 commit d5462ca

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

Complete SQL & Databases - ZTM/07_Database_Management.sql

+38-1
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,41 @@ INSERT INTO test VALUES(
156156
ARRAY[1.23, 3.45, 6.78, 9.2345234]
157157
);
158158

159-
/*--------------------------------------------------------------------------------------------------------------*/
159+
/*--------------------------------------------------------------------------------------------------------------*/
160+
161+
/************ 19) Create Tables **************/
162+
163+
CREATE TABLE <name> (
164+
<col1> TYPE [Constraint],
165+
table_constraint [Constraint]
166+
)[INHERITS <existing_table>];
167+
168+
-- create table for ztm db
169+
-- first need to create extension, otherwise we can't use uuid_generate_v4() function.
170+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
171+
172+
CREATE TABLE student(
173+
student_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
174+
first_name VARCHAR(255) NOT NULL,
175+
last_name VARCHAR(255) NOT NULL,
176+
email VARCHAR(255) NOT NULL,
177+
date_of_birth DATE NOT NULL
178+
);
179+
180+
-- check the existing tables
181+
>> \dt
182+
183+
-- check the newly created student table
184+
>> \d student
185+
186+
/*--------------------------------------------------------------------------------------------------------------*/
187+
188+
/************ 21) Column Constraints **************/
189+
190+
/*
191+
NOT NULL : cannot be Null
192+
PRIMARY KEY : column will be primary key
193+
UNIQUE : can only contain unique values (Null is Unique)
194+
CHECK : apply a special condition check against the values in the column
195+
REFERENCES : used in Foreign Key referencing
196+
*/

0 commit comments

Comments
 (0)