Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions .vscode/PythonImportHelper-v2-Completion.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[
{
"label": "psycopg2",
"kind": 6,
"isExtraImport": true,
"importPath": "psycopg2",
"description": "psycopg2",
"detail": "psycopg2",
"documentation": {}
},
{
"label": "time",
"kind": 6,
"isExtraImport": true,
"importPath": "time",
"description": "time",
"detail": "time",
"documentation": {}
},
{
"label": "sleep",
"importPath": "time",
"description": "time",
"isExtraImport": true,
"detail": "time",
"documentation": {}
},
{
"label": "sleep",
"importPath": "time",
"description": "time",
"isExtraImport": true,
"detail": "time",
"documentation": {}
},
{
"label": "connect",
"kind": 2,
"importPath": "exercise4",
"description": "exercise4",
"peekOfCode": "def connect():\n connection = None\n sleep(0.5)\n try:\n print('Connect to the PostgreSQL database server...')\n connection = psycopg2.connect (\n database='pagila-data', \n user=\"postgres\", \n password=\"Anahi0615.\")\n connection.autocommit = True",
"detail": "exercise4",
"documentation": {}
},
{
"label": "connect",
"kind": 2,
"importPath": "exercise5",
"description": "exercise5",
"peekOfCode": "def connect():\n connection = None\n sleep(0.8)\n try:\n print('Connect to the PostgreSQL database server...')\n connection = psycopg2.connect (\n database='PyCoders', \n user=\"postgres\", \n password=\"Anahi0615.\")\n connection.autocommit = True",
"detail": "exercise5",
"documentation": {}
}
]
72 changes: 72 additions & 0 deletions 1_Entity_DB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
BEGIN;


CREATE TABLE IF NOT EXISTS public."Team"
(
"Employee_ID" serial NOT NULL DEFAULT nextval('tem_id_seq'::regclass),
"Employee_Name" character varying COLLATE pg_catalog."default" NOT NULL,
"Employee_Phone" integer NOT NULL,
"Employee_Roll" character varying COLLATE pg_catalog."default" NOT NULL,
"Employee_Email" character varying NOT NULL,
CONSTRAINT team_pkey PRIMARY KEY ("Employee_ID")
);

CREATE TABLE IF NOT EXISTS public."Custumers"
(
"Customer_ID" serial NOT NULL DEFAULT nextval('tem_id_seq'::regclass),
"Customer_Name" character varying COLLATE pg_catalog."default" NOT NULL,
"Customer_Phone" integer NOT NULL,
"Customer_Email" character varying NOT NULL,
CONSTRAINT "Custumers_pkey" PRIMARY KEY ("Customer_ID")
);

CREATE TABLE IF NOT EXISTS public."Product"
(
"Product_ID" serial NOT NULL DEFAULT nextval('tem_id_seq'::regclass),
"Product_Name" character varying COLLATE pg_catalog."default" NOT NULL,
"Product_Developer" character varying NOT NULL,
CONSTRAINT "Product_pkey" PRIMARY KEY ("Product_ID")
);

CREATE TABLE IF NOT EXISTS public."Orders"
(
"Order_ID" serial NOT NULL DEFAULT nextval('tem_id_seq'::regclass),
"Customer_ID" integer NOT NULL,
"Order_Date" date NOT NULL,
"Product_ID" integer NOT NULL,
"Order_Developer" integer NOT NULL,
CONSTRAINT "Order_pkey" PRIMARY KEY ("Order_ID")
);

ALTER TABLE IF EXISTS public."Product"
ADD CONSTRAINT "fk_Product_Developer" FOREIGN KEY ("Product_Developer")
REFERENCES public."Team" ("Employee_ID") MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID;


ALTER TABLE IF EXISTS public."Orders"
ADD CONSTRAINT "fk_Custumer_ID" FOREIGN KEY ("Customer_ID")
REFERENCES public."Custumers" ("Customer_ID") MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID;


ALTER TABLE IF EXISTS public."Orders"
ADD CONSTRAINT "fk_Product_ID" FOREIGN KEY ("Product_ID")
REFERENCES public."Product" ("Product_ID") MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID;


ALTER TABLE IF EXISTS public."Orders"
ADD CONSTRAINT "fk_Developer_ID" FOREIGN KEY ("Order_Developer")
REFERENCES public."Product" ("Product_Developer") MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID;

END;
Binary file added 2._ERD.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 3._ERD.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions exercise4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import psycopg2
from time import sleep


def connect():
connection = None
sleep(0.5)
try:
print('Connect to the PostgreSQL database server...')

connection = psycopg2.connect (
database='pagila-data',
user="postgres",
password="Anahi0615.")

connection.autocommit = True


# All rows from actor table fetchall
print("+-------------------------------+")
print("| All rows from actor table |")
print("+-------------------------------+")
cursor = connection.cursor()
query = """ SELECT * FROM actor """
cursor.execute(query)
result = cursor.fetchall()

for r in result:
print(r)
sleep(0.069)

# First row of category table with fetchone
print()
print("+-------------------------------+")
print("| First row of category table |")
print("+-------------------------------+")
cursor = connection.cursor()
query = """ SELECT * FROM category """
cursor.execute(query)
result = cursor.fetchone()
print(result,"\n")
sleep(0.3)


# 50 rows of address table fetchmany
print("+-------------------------------+")
print("| 50 rows of address table |")
print("+-------------------------------+")
cursor = connection.cursor()
query = """ SELECT * FROM address """
cursor.execute(query)
result = cursor.fetchmany(size=50)

for r in result:
print(r)
sleep(0.069)

cursor.close()

except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if connection is not None:
connection.close()
sleep(0.3)
print('\nDatabase connection closed.\n')

if __name__ == '__main__':
connect()
17 changes: 17 additions & 0 deletions exercise4_DB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
BEGIN;


CREATE TABLE IF NOT EXISTS public.students
(
student_id integer NOT NULL,
student_name character varying(120) COLLATE pg_catalog."default",
CONSTRAINT pk_students PRIMARY KEY (student_id)
);

CREATE TABLE IF NOT EXISTS public.teachers
(
teacher_id integer NOT NULL,
teacher_name character varying(120) COLLATE pg_catalog."default",
CONSTRAINT pk_teachers PRIMARY KEY (teacher_id)
);
END;
101 changes: 101 additions & 0 deletions exercise5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import psycopg2
import time
from time import sleep


def connect():
connection = None
sleep(0.8)
try:
print('Connect to the PostgreSQL database server...')

connection = psycopg2.connect (
database='PyCoders',
user="postgres",
password="Anahi0615.")

connection.autocommit = True

# CREATE TABLE Students

cursor = connection.cursor()
query = """ CREATE TABLE Students (
student_ID INT NOT NULL,
student_name VARCHAR(120),
CONSTRAINT PK_Students PRIMARY KEY (student_ID)) """

cursor.execute(query)


# CREATE TABLE Teachers

# cursor = connection.cursor()
query = """ CREATE TABLE Teachers (
teacher_ID INT NOT NULL,
teacher_name VARCHAR(120),
CONSTRAINT PK_Teachers PRIMARY KEY (teacher_ID)) """
cursor.execute(query)

# insert tData INTO Students

query = """ INSERT INTO Students
(student_ID, student_name)
VALUES
(
1, 'Shatha Al-Ashwal'
),
( 2, 'Ghassan Alabsi'
),
( 3, 'Juan Obregon'
) """

cursor.execute(query)

# insert Data INTO Teachers
query = """ INSERT INTO Teachers
(teacher_ID, teacher_name)
VALUES
(
1, 'Irfan Karadeniz'
),
( 2, 'Semith'
),
( 3, 'Irem Ugurlu'
) """

cursor.execute(query)

# viewData Student
query = 'Select student_ID, student_name from students'
cursor.execute(query)
sleep(0.8)
print('Students List :')
for fila in cursor:
print(fila)
sleep(0.4)
print()


# viewData Teachers
query = 'Select teacher_ID, teacher_name from Teachers'
cursor.execute(query)
sleep(0.8)
print('Teachers List :')
for fila in cursor:
print(fila)
sleep(0.4)
print()

cursor.close()


except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if connection is not None:
connection.close()
sleep(0.8)
print('Database connection closed.')

if __name__ == '__main__':
connect()
17 changes: 17 additions & 0 deletions exercise5_DB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
BEGIN;


CREATE TABLE IF NOT EXISTS public.students
(
student_id integer NOT NULL,
student_name character varying(120) COLLATE pg_catalog."default",
CONSTRAINT pk_students PRIMARY KEY (student_id)
);

CREATE TABLE IF NOT EXISTS public.teachers
(
teacher_id integer NOT NULL,
teacher_name character varying(120) COLLATE pg_catalog."default",
CONSTRAINT pk_teachers PRIMARY KEY (teacher_id)
);
END;