140 likes | 282 Views
This document outlines the SQL commands necessary to create a structured database in PostgreSQL for managing research-related data. It includes the definitions of several tables such as publications, people, grants, research, authorship, and collaborations, along with their respective fields and constraints. Additionally, it describes how to insert initial data entries into these tables, ensuring data integrity through primary and foreign key relationships. This schema facilitates the efficient organization and retrieval of valuable research information.
E N D
PostgreSQL S511
Create Table Schema CREATE TABLE "lab"."tblpublications" ( "publicationId" int4 DEFAULT 0 NOT NULL, "publicationTitle" varchar(250), "publicationDate" date, "publicationVenue" varchar(250), "venueVolume" varchar(10), "venueNumber" varchar(10), "venuePages" varchar(50), "type" varchar(2), "publisher" varchar(150), "venueChapter" varchar(10), PRIMARY KEY ("publicationId") ); CREATE TABLE "lab"."tblpeople" ( "peopleId" int4 DEFAULT 0 NOT NULL, "fName" varchar(70), "mName" varchar(70), "lName" varchar(100), PRIMARY KEY ("peopleId") );
Create Table Schema CREATE TABLE "lab"."tblgrants" ( "grantId" int4 DEFAULT 0 NOT NULL, "grantName" text, "amount" numeric(18), "grantTitle" varchar(250), "startDate" date, "endDate" date, "organizationId" int4, "receivedAmount" varchar(18), PRIMARY KEY ("grantId") ); CREATE TABLE "lab"."tblresearch" ( "researchId" int4 DEFAULT 0 NOT NULL, "fullTitle" varchar(250), "shortTitle" varchar(50), "startDate" date, "endDate" date, "description" text, PRIMARY KEY ("researchId") );
Create Table Schema CREATE TABLE "lab"."brdgcopis" ( "coPIId" int4 DEFAULT 0 NOT NULL, "peopleId" int4, "grantId" int4, PRIMARY KEY ("coPIId"), FOREIGN KEY ("peopleId") REFERENCES "lab"."tblpeople"("peopleId"), FOREIGN KEY ("grantId") REFERENCES "lab"."tblgrants"("grantId") ); CREATE TABLE "lab"."brdgauthorship" ( "authorshipId" int4 DEFAULT 0 NOT NULL, "peopleId" int4, "publicationId" int4 DEFAULT 0 NOT NULL, PRIMARY KEY ("authorshipId"), FOREIGN KEY ("peopleId") REFERENCES "lab"."tblpeople"("peopleId"), FOREIGN KEY ("publicationId") REFERENCES "lab"."tblpublications"("publicationId") );
Create Table Schema CREATE TABLE "lab"."brdgteamcollabs" ( "teamCollabsId" int4 DEFAULT 0 NOT NULL, "peopleId" int4, "researchId" int4, "startDate" date, "endDate" date, PRIMARY KEY ("teamCollabsId"), FOREIGN KEY ("peopleId") REFERENCES "lab"."tblpeople"("peopleId") ); CREATE TABLE "lab"."brdgfunding" ( "fundingId" int4 DEFAULT 0 NOT NULL, "grantId" int4, "researchId" int4, PRIMARY KEY ("fundingId"), FOREIGN KEY ("grantId") REFERENCES "lab"."tblgrants"("grantId"), FOREIGN KEY ("researchId") REFERENCES "lab"."tblresearch"("researchId") );
Add instances to tables INSERT INTO “lab"."tblpeople" VALUES ('1', 'Thomas', '\\N', 'Hunt'); INSERT INTO “lab"."tblpeople" VALUES ('2', 'Yan', '\\N', 'Sun'); INSERT INTO “lab"."tblpeople" VALUES ('3', 'Christopher', '\\N', 'Essex'); INSERT INTO “lab"."tblpeople" VALUES ('4', 'Caroline', '\\N', 'Beebe'); …… INSERT INTO “lab"."tblpublications" VALUES ('3', 'Examining the Evolution and Distribution of Patent Classifications', '2004-01-01', 'IV2004 Conference, London, UK', '\\N', '\\N', '983-988', 'cp', '\\N', '\\N'); INSERT INTO “lab"."tblpublications" VALUES ('13', 'Mapping Topics and Topic Bursts in PNAS', '2004-01-01', 'Proceedings of the National Academy of Sciences of the United States of America', '101', '101', '5287-5290', 'ja', '101', '101');
Example tblpublications brdgauthorship tblpeople brdgteamcollabs tblresearch PK publicationId PK authorshipId PK peopleId PK teamCollabsId PK researchId publicationTitle publicationDate ...... venueChapter peopleId publicationId fName mName lName peopleId publicationId …… fullTitle shortTitle …… brdgcopis PK coPIId peopleId grantId tblgrants brdgfunding PK grantId PK fundingId grantName amount …… grantId researchId
SQL: Creating/Dropping table • Create Table CREATE TABLE “lab"."tblpeople1" ( "peopleId" int4 DEFAULT 0 NOT NULL, "fName" varchar(70), "mName" varchar(70), "lName" varchar(100), PRIMARY KEY ("peopleId") ); • Drop table DROP TABLE “lab".“tblpeople1";
Modifying table data INSERT INTO "lab"."tblpeople" VALUES ('1', 'Thomas', '\\N', 'Hunt'); SELECT * FROM "lab"."tblpeople"; UPDATE "lab"."tblpeople" SET "lName" ='Hunt' WHERE "peopleId" = 1; DELETE FROM "lab"."tblpeople" WHERE "peopleId" = 1;
Altering tables ALTER TABLE "lab"."tblpeople" ADD "nickName" VARCHAR(70); ALTER TABLE "lab"."tblpeople" DROP "nickName";
Queries SELECT * FROM "lab"."tblpeople" LIMIT 3; SELECT * FROM "lab"."tblpeople" WHERE "lName" = 'Hunt'; SELECT * FROM "lab"."tblpeople" WHERE "lab"."tblpeople"."peopleId" IN (SELECT DISTINCT "lab"."brdgauthorship"."peopleId" FROM "lab"."brdgauthorship"); SELECT DISTINCT "lab"."tblresearch"."fullTitle", "lab"."tblgrants"."grantTitle" FROM "lab"."tblresearch", "lab"."brdgfunding", "lab"."tblgrants" WHERE "lab"."tblresearch"."researchId" = "lab"."brdgfunding"."researchId" AND "lab"."tblgrants"."grantId" = "lab"."brdgfunding"."grantId";
Sorting and Grouping SELECT * FROM "lab"."tblpeople" ORDER BY "fName", "lName"; SELECT "lab"."tblpeople"."fName", "lab"."tblpeople"."lName", COUNT("lab"."brdgauthorship"."publicationId") as "pubs" FROM "lab"."tblpeople", "lab"."brdgauthorship" WHERE "lab"."tblpeople"."peopleId" = "lab"."brdgauthorship"."peopleId" GROUP BY "lab"."tblpeople"."peopleId", "lab"."tblpeople"."fName", "lab"."tblpeople"."lName" ORDER BY "pubs" DESC; SELECT "lab"."tblresearch"."fullTitle", sum("lab"."tblgrants"."amount") as "funding" FROM "lab"."tblresearch", "lab"."tblgrants", "lab"."brdgfunding" WHERE "lab"."brdgfunding"."researchId" = "lab"."tblresearch"."researchId" AND "lab"."brdgfunding"."grantId" = "lab"."tblgrants"."grantId" AND "lab"."tblgrants"."amount" IS NOT NULL GROUP BY "lab"."tblresearch"."researchId", "lab"."tblresearch"."fullTitle" ORDER BY "funding" DESC LIMIT 5;
Joining tables SELECT * FROM "lab"."tblresearch" INNER JOIN "lab"."brdgfunding" ON "lab"."tblresearch"."researchId" = "lab"."brdgfunding"."researchId" INNER JOIN "lab"."tblgrants" ON "lab"."tblgrants"."grantId" = "lab"."brdgfunding"."grantId"; SELECT * FROM "lab"."tblresearch" LEFT JOIN "lab"."brdgfunding" ON "lab"."tblresearch"."researchId" = "lab"."brdgfunding"."researchId" LEFT JOIN "lab"."tblgrants" ON "lab"."tblgrants"."grantId" = "lab"."brdgfunding"."grantId"; SELECT * FROM "lab"."tblresearch" RIGHT JOIN "lab"."brdgfunding" ON "lab"."tblresearch"."researchId" = "lab"."brdgfunding"."researchId" RIGHT JOIN "lab"."tblgrants" ON "lab"."tblgrants"."grantId" = "lab"."brdgfunding"."grantId";