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.
58 lines
3.3 KiB
58 lines
3.3 KiB
-- ///////////////////////////////////////Создание таблиц/////////////////////////////////// |
|
|
|
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; |