postgresql
DESCRIPTION
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), - PowerPoint PPT PresentationTRANSCRIPT
PostgreSQL
S511
Create Table SchemaCREATE TABLE "lab"."tblpeople" ("peopleId" int4 DEFAULT 0 NOT
NULL,"fName" varchar(70),"mName" varchar(70),"lName" varchar(100),PRIMARY KEY ("peopleId"));
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 SchemaCREATE 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 "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 SchemaCREATE 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 "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 SchemaCREATE 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"));
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"));
Add instances to tablesINSERT 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');
tblpublications
PK publicationId
publicationTitle publicationDate ...... venueChapter
tblpeoplePK peopleId
fName mName lName
brdgauthorship
PK authorshipId
peopleId publicationId
tblgrantsPK grantId
grantName amount ……
brdgcopisPK coPIId
peopleId grantId
tblresearch
PK researchId
fullTitle shortTitle ……
brdgteamcollabs
PK teamCollabsId
peopleId publicationId ……
brdgfunding
PK fundingId
grantId researchId
Example
SQL: Creating/Dropping table
• Create TableCREATE TABLE “lab"."tblpeople1" ("peopleId" int4 DEFAULT 0 NOT NULL,"fName" varchar(70),"mName" varchar(70),"lName" varchar(100),PRIMARY KEY ("peopleId"));
• Drop tableDROP 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";
QueriesSELECT * 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 GroupingSELECT * 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 tablesSELECT * 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";