esoe
2 years ago
2 changed files with 131 additions and 0 deletions
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
-- ///////////////////////////////////////Создание таблиц/////////////////////////////////// |
||||
|
||||
CREATE TABLE person ( |
||||
id int primary key generated always as identity (start with 1, increment by 1), |
||||
full_name varchar(200) not null, |
||||
birth date |
||||
); |
||||
|
||||
--DROP TABLE PERSON; |
||||
|
||||
CREATE TABLE building ( |
||||
id int primary key generated always as identity (start with 1, increment by 1), |
||||
address varchar(200) not null, |
||||
date_build date, |
||||
status varchar(50), |
||||
type_buiding varchar(50), |
||||
count_men int, |
||||
check (type_buiding in ('жилое', 'ТЦ', 'БЦ', 'больница')), |
||||
check (status='build' and date_build is null or status='done' and date_build is not null) |
||||
); |
||||
|
||||
--DROP TABLE BUILDING; |
||||
|
||||
ALTER TABLE building ADD COLUMN person_id int; |
||||
ALTER TABLE building ADD CONSTRAINT fk_building_person FOREIGN KEY (person_id) REFERENCES person(id) |
||||
|
||||
-- ////////////////////////////////////////////////////Наполнение таблиц данными////////////////////////////////////////////////// |
||||
INSERT INTO PERSON (FULL_NAME, BIRTH) VALUES ('Andrey Rozkov', '1996-12-05'); |
||||
INSERT INTO PERSON (FULL_NAME, BIRTH) VALUES ('Maksim Rozkov', '1989-10-15'); |
||||
INSERT INTO PERSON (FULL_NAME, BIRTH) VALUES ('Olga Krot', '1982-02-03'); |
||||
INSERT INTO PERSON (FULL_NAME, BIRTH) VALUES ('Natalia Ivanova', '1999-01-23'); |
||||
INSERT INTO PERSON (FULL_NAME, BIRTH) VALUES ('Vladimir Zorin', '1996-12-05'); |
||||
INSERT INTO PERSON (FULL_NAME, BIRTH) VALUES ('Ivan Karulin', '1996-08-05'); |
||||
|
||||
UPDATE PERSON SET BIRTH = '1998-06-25' WHERE FULL_NAME LIKE '%in'; |
||||
DELETE FROM PERSON WHERE FULL_NAME='Maksim Rozkov'; |
||||
|
||||
SELECT * FROM PERSON; |
||||
|
||||
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS, TYPE_BUIDING, PERSON_ID) VALUES ('Saint Petersburg', '1956-02-06', 'done', 'жилое', 1); |
||||
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS, TYPE_BUIDING, PERSON_ID) VALUES ('Moscow', '1981-05-26', 'done', 'жилое', 1); |
||||
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS, TYPE_BUIDING, PERSON_ID) VALUES ('Saint Petersburg', '1843-12-15', 'done', 'жилое', 4); |
||||
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS, TYPE_BUIDING, PERSON_ID) VALUES ('Saint Petersburg', '1799-04-09', 'done', 'жилое', 5); |
||||
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS, TYPE_BUIDING) VALUES ('Moscow', '1650-01-01', 'done', 'жилое'); |
||||
INSERT INTO BUILDING (ADDRESS, DATE_BUILD, STATUS, TYPE_BUIDING) VALUES ('Moscow', '1999-11-01', 'done', 'жилое'); |
||||
INSERT INTO BUILDING (ADDRESS, STATUS, TYPE_BUIDING) VALUES ('Moscow', 'build', 'БЦ'); |
||||
|
||||
SELECT * FROM BUILDING; |
||||
|
||||
-- ////////////////////////////////////////////Запросы данных из БД/////////////////////////////////////////// |
||||
SELECT * FROM BUILDING ORDER BY ID DESC; -- сортировка по полю id в обратном порядке |
||||
SELECT DISTINCT(STATUS) FROM BUILDING; -- выводит уникальные значения поля status |
||||
SELECT COUNT(id) FROM BUILDING; -- выводит количество записей поля id |
||||
|
||||
|
||||
SELECT * 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 p.ID, b.ADDRESS, b.PERSON_ID FROM PERSON p JOIN BUILDING b ON p.ID=b.PERSON_ID; -- тоже что и INNER JOIN; |
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
-- ///////////////////////////////////Создание таблиц/////////////////////////////////////////////// |
||||
|
||||
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; |
Loading…
Reference in new issue