You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
4.2 KiB
73 lines
4.2 KiB
-- ///////////////////////////////////Создание таблиц/////////////////////////////////////////////// |
|
|
|
DROP TABLE person; |
|
|
|
CREATE TABLE person ( |
|
id int primary key generated always as identity (start with 1, increment by 1), |
|
full_name varchar(100) not null, |
|
birthday date |
|
); |
|
|
|
DROP TABLE BUILDING; |
|
|
|
CREATE TABLE building ( |
|
id int primary key generated always as identity (start with 1, increment by 1), |
|
address varchar(200) not null unique, |
|
date_build date, |
|
status varchar(50), |
|
CHECK (status in ('строится', 'сдано')), |
|
CHECK (status='строится' and date_build is null or status='сдано' and date_build is not null) |
|
); |
|
|
|
ALTER TABLE building ADD COLUMN person_id int; |
|
ALTER TABLE building ADD COLUMN COUNT_MEN int; |
|
ALTER TABLE building ADD CONSTRAINT fk_personId_person FOREIGN KEY (person_id) REFERENCES person(id); |
|
|
|
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS) VALUES ('Saint Petersburg', '2022-01-16', 'сдано'); |
|
INSERT INTO BUILDING (ADDRESS, STATUS) VALUES ('Saint Petersburg, Green st.', 'строится'); |
|
|
|
-- ////////////////////////////Наполнение таблиц данными////////////////////////////////////// |
|
INSERT INTO PERSON (FULL_NAME, BIRTHDAY) VALUES ('Muhin Vadim', '1985-10-25'); |
|
INSERT INTO PERSON (FULL_NAME, BIRTHDAY) VALUES ('Ivanov Andrey', '1999-11-15'); |
|
INSERT INTO PERSON (FULL_NAME, BIRTHDAY) VALUES ('Sergeev Sergey', '1991-05-02'); |
|
INSERT INTO PERSON (FULL_NAME, BIRTHDAY) VALUES ('Antonov Victor', '1989-09-10'); |
|
INSERT INTO PERSON (FULL_NAME, BIRTHDAY) VALUES ('Petrov Sergey', '1987-07-29'); |
|
|
|
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS, PERSON_ID) VALUES ('Saint Petersburg, Nevskiy', '1999-10-10', 'сдано', 6); |
|
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS) VALUES ('Saint Petersburg, Chkalovskiy', '1895-10-25', 'сдано'); |
|
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS, PERSON_ID) VALUES ('Saint Petersburg, Morskaya', '1990-05-06', 'сдано', 6); |
|
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS) VALUES ('Saint Petersburg, Konuschenaya', '1999-08-07', 'сдано'); |
|
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS, PERSON_ID) VALUES ('Saint Petersburg, Nalichnays', '1999-10-07', 'сдано', 7); |
|
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS) VALUES ('Saint Petersburg, Kostuschko', '1998-05-01', 'сдано'); |
|
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS, PERSON_ID) VALUES ('Saint Petersburg, Veteranov', '2003-12-26', 'сдано', 8); |
|
INSERT INTO BUILDING (ADDRESS, STATUS) VALUES ('Saint Petersburg, Kosmonavtov', 'строится'); |
|
|
|
DELETE FROM BUILDING WHERE ADDRESS LIKE '%N%'; |
|
DELETE FROM PERSON; |
|
|
|
UPDATE PERSON SET FULL_NAME = 'Krot Evgeniy' WHERE id=6; |
|
|
|
-- ////////////////////////////////Запросы данных из БД////////////////////////////////////////////// |
|
SELECT ID, ADDRESS, DATE_BUILD, STATUS, PERSON_ID FROM BUILDING; |
|
SELECT COUNT(ID) FROM BUILDING; |
|
SELECT DISTINCT(STATUS) FROM BUILDING; |
|
SELECT COUNT(DISTINCT(STATUS)) FROM BUILDING; |
|
SELECT * FROM BUILDING WHERE DATE_BUILD > '1995-01-01' AND STATUS='сдано'; |
|
|
|
SELECT * FROM PERSON LEFT JOIN BUILDING ON PERSON.ID = BUILDING.PERSON_ID; |
|
SELECT * FROM PERSON RIGHT JOIN BUILDING ON PERSON.ID = BUILDING.PERSON_ID; |
|
SELECT * FROM PERSON INNER JOIN BUILDING ON PERSON.ID = BUILDING.PERSON_ID; |
|
|
|
SELECT p.ID, p.FULL_NAME, b.ADDRESS FROM PERSON p LEFT JOIN BUILDING b ON p.ID = b.PERSON_ID WHERE b.PERSON_ID is null; |
|
SELECT * FROM PERSON RIGHT JOIN BUILDING ON PERSON.ID = BUILDING.PERSON_ID WHERE BUILDING.PERSON_ID is null; |
|
SELECT * FROM BUILDING WHERE PERSON_ID is null; |
|
SELECT * FROM PERSON FULL JOIN BUILDING ON PERSON.ID = BUILDING.PERSON_ID; |
|
|
|
-- Получить персон у которых здания построены в 1999 году |
|
SELECT * FROM PERSON LEFT JOIN BUILDING ON PERSON.ID = BUILDING.PERSON_ID WHERE DATE_BUILD >= '1999-01-01' AND DATE_BUILD <= '1999-12-31'; |
|
|
|
-- Синтетический пример с несуществующей третьей таблицей |
|
SELECT * FROM PERSON |
|
LEFT JOIN |
|
(SELECT * FROM BUILDING RIGHT JOIN CADASTR ON BUILDING.ID = CADASTR.BUILDING_ID) AS cad |
|
ON PERSON.ID = cad.PERSON_ID; |