diff --git a/.vscode/PythonImportHelper-v2-Completion.json b/.vscode/PythonImportHelper-v2-Completion.json new file mode 100644 index 0000000..f43f97f --- /dev/null +++ b/.vscode/PythonImportHelper-v2-Completion.json @@ -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": {} + } +] \ No newline at end of file diff --git a/1_Entity_DB.sql b/1_Entity_DB.sql new file mode 100644 index 0000000..06d7eb6 --- /dev/null +++ b/1_Entity_DB.sql @@ -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; \ No newline at end of file diff --git a/2._ERD.png b/2._ERD.png new file mode 100644 index 0000000..11558de Binary files /dev/null and b/2._ERD.png differ diff --git a/3._ERD.png b/3._ERD.png new file mode 100644 index 0000000..da28cc4 Binary files /dev/null and b/3._ERD.png differ diff --git a/exercise4.py b/exercise4.py new file mode 100644 index 0000000..890db3f --- /dev/null +++ b/exercise4.py @@ -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() \ No newline at end of file diff --git a/exercise4_DB.sql b/exercise4_DB.sql new file mode 100644 index 0000000..3be30d1 --- /dev/null +++ b/exercise4_DB.sql @@ -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; \ No newline at end of file diff --git a/exercise5.py b/exercise5.py new file mode 100644 index 0000000..5118d8b --- /dev/null +++ b/exercise5.py @@ -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() \ No newline at end of file diff --git a/exercise5_DB.sql b/exercise5_DB.sql new file mode 100644 index 0000000..3be30d1 --- /dev/null +++ b/exercise5_DB.sql @@ -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; \ No newline at end of file