Set Search - Path To Sales CREATE TABLE Customers (Id Integer NOT NULL, Lastname Text NOT NULL
The document provides instructions for setting up database objects like schemas, tables, and users. It includes steps to create databases and schemas, move tables between schemas, grant and revoke privileges, and import data. The goal is to set up the database and configure access for a new user.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
14 views
Set Search - Path To Sales CREATE TABLE Customers (Id Integer NOT NULL, Lastname Text NOT NULL
The document provides instructions for setting up database objects like schemas, tables, and users. It includes steps to create databases and schemas, move tables between schemas, grant and revoke privileges, and import data. The goal is to set up the database and configure access for a new user.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
Do the exercise as DBA (but try to check if the things work connecting with the user that
you’ll create):
1.- Create a database with name ‘p07’.
Create database p07; 2.- Revoke all permissions to the schema ‘p07.public’ to PUBLIC. REVOKE ALL PRIVILEGES ON SCHEMA public FROM PUBLIC; 3.- Create a schema ‘sales’ and a ‘schema’ resources. create schema sales; create schema resources; 4.- Create the following tables inside the schema ‘sales’. CREATE TABLE customers ( id integer NOT NULL, lastname text NOT NULL, firstname text NOT NULL, address text NOT NULL, zipcode integer NOT NULL, phonenumber text NOT NULL, recommended_id integer, registerdate timestamp not null, CONSTRAINT customers_pk PRIMARY KEY (id), CONSTRAINT fk_customers_recommended_id FOREIGN KEY (recommended_id) REFERENCES customers(id) ON DELETE SET NULL );
CREATE INDEX idx_customers_fk ON customers (recommended_id);
set search_path to sales;
CREATE TABLE customers ( id integer NOT NULL, lastname text NOT NULL, firstname text NOT NULL, address text NOT NULL, zipcode integer NOT NULL, phonenumber text NOT NULL, recommended_id integer, registerdate timestamp not null, CONSTRAINT customers_pk PRIMARY KEY (id), CONSTRAINT fk_customers_recommended_id FOREIGN KEY (recommended_id) REFERENCES customers(id) ON DELETE SET NULL ); CREATE INDEX idx_customers_fk ON customers (recommended_id); 5.- Create the following tablea inside the schema ‘resources’: ● facilities: ○ id integer NOT NULL ○ name text NOT NULL ○ cust_cost money NOT NULL ○ guest_cost money NOT NULL ○ purchase_cost money NOT NULL ○ maintenance_cost money NOT NULL ● bookings: ○ fac_id integer NOT NULL (this is a foreign key) ○ cust_id integer NOT NULL (this is a foreign key) ○ start_datetime timestamp NOT NULL ○ nhours integer NOT NULL Idem af before but you must be careful with the FK referencing customers: create table facilities( fac_id serial not null, name text not null, cust_cost money not null, guest_cost money not null, purchase_cost money not null, maintenance_cost money not null, PRIMARY KEY (fac_id) ); create table bookings ( fac_id int not null, cust_id int not null, start_datetime timestamp not null, nhours int not null, constraint pk_bookings PRIMARY KEY (fac_id, cust_id, start_datetime), constraint fk_bookings_cust_id FOREIGN KEY (cust_id) REFERENCES sales.customers (id), constraint fk_bookings_fac_id FOREIGN KEY (fac_id) REFERENCES facilities (fac_id) ); create index idx_bookings_fk_1 on bookings(fac_id); create index idx_bookings_fk_2 on bookings(cust_id); 6.- Import the following data to those tables: ● bookings.csv ● customers.csv ● facilities.csv copy resources.facilities from 'C:\FP\Base de datos\Tema 7\practica\facilities.csv' with (DELIMITER ';',FORMAT CSV, HEADER true); copy resources.bookings from 'C:\FP\Base de datos\Tema 7\practica\bookings.csv' with (DELIMITER ';',FORMAT CSV, HEADER true); copy sales.facilities from 'C:\FP\Base de datos\Tema 7\practica\customers.csv' with (DELIMITER ';',FORMAT CSV, HEADER true); 7.- Revoke all permission on the schemas ‘sales’ and ‘resources’ to PUBLIC. Revoke all privileges on schema sales from public; 8.- Create a user ‘user1’; Create user user1; 9.- Create a new schema ‘app’ and revoke all privileges from public to it. Create schema app; 10.- Set the search path for ‘user’1’ to the schema ‘app’ (Clue: ALTER ROLE). Alter role user1 set search_path to app; 11.- Move all the tables to schema ‘app’ (clue: alter table, this is not in the slides). Check if it worked with \dt. Alter table sales.customers set schema app; Alter table resources.booking set schema app; Set seach_path to app; \dt 12.- Grant USAGE permission on the schema ‘app’ to user ‘user1’. What can he/she do with the tables? Grant usage on schema app to user1; 13.- VERY IMPORTANT EXERCISE: Grant all privileges to “user1” on database “p07”. Can he/she access data now? Grant connect on database p07 to user1; Grant usage on schema app to user1; Grant all privileges on all tables in schema app to user1; 14.- Make ‘user1’ the owner of the 3 tables. Alter table app.bookings owner to user1; Alter table app.facilities owner to user1; Alter table app.customers owner to user1; \dt