Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
лабы_отчет.docx
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
277.52 Кб
Скачать

Начальник

Работники склада

Работники склада

Работники склада (Store Worker2)

ID_worker (PK): INTEGER

ID_boss (FK): INTEGER

ID_store (PK): INTEGER

Имя (Name): VARCHAR2 (20)

Должность (Position): VARCHAR2 (20)

Отдел (Department): VARCHAR2 (20)

23

456

1

Иванов

бухгалтер

бухгалтерия

456

124

1

Петрова

Глав. Бух.

бухгалтерия

124

124

1

Сидоров

директор

Управление

Полученный в ERWin код.

CREATE TABLE Store (

ID_store INTEGER NOT NULL,

Name VARCHAR2(20) NULL,

Area INTEGER NULL

);

ALTER TABLE Store

ADD ( PRIMARY KEY (ID_store) ) ;

CREATE TABLE Store_Worker2 (

ID_worker INTEGER NOT NULL,

ID_Boss INTEGER NULL,

Name VARCHAR2(20) NOT NULL,

Position VARCHAR2(20) NOT NULL,

Department VARCHAR2(20) NOT NULL,

ID_store INTEGER NOT NULL

);

ALTER TABLE Store_Worker2

ADD ( PRIMARY KEY (ID_worker) ) ;

ALTER TABLE Store_Worker2

ADD ( FOREIGN KEY (ID_store)

REFERENCES Store ) ;

ALTER TABLE Store_Worker2

ADD ( FOREIGN KEY (ID_Boss)

REFERENCES Store_Worker2

ON DELETE SET NULL ) ;

Лабораторная работа №7. Сетевая рекурсия.

Хранит\ хранится

Материал

Склад

Склад (Store)

ID_store (PK): INTEGER

Название (Name): VARCHAR2(20)

ID_material (PK): INTEGER

1

Домодедовский

74387

2

Северный

23004

3

Склад №3

23004

Материал (Material)

ID_material (PK): INTEGER

Название (Name) : VARCHAR2(20)

ID_store (PK): INTEGER

01002

Вагонка

1

23004

Стекловата

3

74387

Блок хаус

2

Полученный в ERWin код.

CREATE TABLE Material7 (

ID_material INTEGER NOT NULL,

Name VARCHAR2(20) NULL,

ID_store INTEGER NULL

);

ALTER TABLE Material7

ADD ( PRIMARY KEY (ID_material) ) ;

CREATE TABLE Store7 (

ID_store INTEGER NOT NULL,

Name VARCHAR2(20) NULL,

ID_material INTEGER NOT NULL

);

ALTER TABLE Store7

ADD ( PRIMARY KEY (ID_store) ) ;

ALTER TABLE Material7

ADD ( FOREIGN KEY (ID_store)

REFERENCES Store7

ON DELETE SET NULL ) ;

ALTER TABLE Store7

ADD ( FOREIGN KEY (ID_material)

REFERENCES Material7 ) ;

Лабораторная работа №8. Шаблонные отношения.

Полученный в ERWin код.

trigger /* ERwin Builtin %Datetime */

/* %Parent %VerbPhrase %Child ON CHILD INSERT SET NULL */

/* %ErwinRelationInfo */

update %Child

set

/* %%SetFK(%Child,NULL) */

%SetFK(%Child,NULL)

where

not exists (

select * from %Parent

where

/* %%JoinFKPK(:%%New,%Parent," = "," and") */

%JoinFKPK(:%New,%Parent," = "," and")

)

/* %%JoinPKPK(%Child,:%%New," = "," and") */

%JoinPKPK(%Child,:%New," = "," and");

Лабораторная работа №9. Отношения тип-подтип.

Материал

Пиломатериал

Утеплитель

Материал (Material)

ID_material (PK): INTEGER

Название (Name) : VARCHAR2(20)

Размер (Size) : VARCHAR2(20)

01002

Вагонка

20*200см

23004

Стекловата

80*4000см

74387

Блок хаус

14*300см

64735

Черновая доска

15*300см

Пиломатериал (Pilomaterial)

ID_material (FK): INTEGER

Категория (Category) : VARCHAR2(20)

01002

Высшая

64735

Третья

Утеплитель (Uteplitel)

ID_material (FK): INTEGER

Категория (Category) : VARCHAR2(20)

23004

1

Полученный в ERWin код.

CREATE TABLE Material (

ID_material INTEGER NOT NULL,

Name VARCHAR2(20) NOT NULL,

Size VARCHAR2(20) NULL

);

ALTER TABLE Material

ADD ( PRIMARY KEY (ID_material) ) ;

CREATE TABLE Pilomaterial (

ID_material INTEGER NOT NULL,

Category VARCHAR2(20) NULL

);

ALTER TABLE Pilomaterial

ADD ( PRIMARY KEY (ID_material) ) ;

CREATE TABLE Uteplitel (

ID_material INTEGER NOT NULL,

Category VARCHAR2(20) NULL

);

ALTER TABLE Uteplitel

ADD ( PRIMARY KEY (ID_material) ) ;

ALTER TABLE Pilomaterial

ADD ( FOREIGN KEY (ID_material)

REFERENCES Material

ON DELETE CASCADE ) ;

ALTER TABLE Uteplitel

ADD ( FOREIGN KEY (ID_material)

REFERENCES Material

ON DELETE CASCADE ) ;

Реализация в NeatBeans.

package derbyproject;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

/**

*

* @author Alexandra

*/

public class Main {

public String framework = "embedded";

public String driver = "org.apache.derby.jdbc.EmbeddedDriver";

// public String driver = "org.apache.derby.jdbc.ClientDriver";

public String protocol = "jdbc:derby:";

public static void main(String[] args)

{

new Main().go(args);

}

void go(String[] args)

{

/* parse the arguments to determine which framework is desired*/

parseArguments(args);

System.out.println("SimpleApp starting in " + framework + " mode.");

try

{

/*

The driver is installed by loading its class.

In an embedded environment, this will start up Derby, since it is not already running.

*/

System.out.println("step 1");

Class.forName(driver).newInstance();

System.out.println("step 2");

System.out.println("Loaded the appropriate driver.");

Connection conn = null;

Properties props = new Properties();

props.put("user", "Sun002");

props.put("password", "Sun4");

conn = DriverManager.getConnection(protocol +

"Sun;create=true", props);

System.out.println("Connected to and created database Sun");

conn.setAutoCommit(false);

/*

Creating a statement lets us issue commands against

the connection.

*/

Statement s = conn.createStatement();

System.out.println("LAB №1 - IDENTIFYING RELATIONSHIP. \n");

System.out.println("Creating table STORE...");

s.execute("create table STORE(ID_STORE int not null, NAME varchar(20), AREA integer)");

s.execute("alter table STORE add primary key(ID_STORE)");

System.out.println("Table STORE created.");

System.out.println("Adding records to STORE...");

s.execute("insert into STORE values (1, 'Домодедовский',3000)");

s.execute("insert into STORE values (2, 'Северный',8000)");

s.execute("insert into STORE values (3, 'Склад №3',4000)");

System.out.println("Records added.");

System.out.println("Creating table MATERIAL...");

s.execute("create table MATERIAL(ID_MATERIAL int not null, NAME varchar(20), SIZE varchar(20))");

s.execute("alter table MATERIAL add primary key(ID_MATERIAL)");

System.out.println("Table MATERIAL created.");

System.out.println("Adding records to MATERIAL...");

s.execute("insert into MATERIAL values (01002, 'Вагонка', '20*200см')");

s.execute("insert into MATERIAL values (23004, 'Стекловата','80*4000см')");

s.execute("insert into MATERIAL values (74387, 'Блок хаус','14*300см')");

s.execute("insert into MATERIAL values (64735, 'Черновая доска','15*300см')");

System.out.println("Records added.");

System.out.println("Creating table AVAILABILITY...");

s.execute("create table AVAILABILITY(ID_STORE int not null, ID_MATERIAL int not null, AVAILABILITY varchar(20))");

s.execute("alter table AVAILABILITY add primary key(ID_STORE,ID_MATERIAL)");

System.out.println("Table AVAILABILITY created.");

System.out.println("Creating identifying relationship between STORE and MATERIAL...");

s.execute("alter table AVAILABILITY add foreign key (ID_STORE) references STORE") ;

s.execute("alter table AVAILABILITY add foreign key (ID_MATERIAL) references MATERIAL") ;

System.out.println("Identifying relationship AVAILABILITY established. \n");

System.out.println("Adding records to AVAILABILITY...");

s.execute("insert into AVAILABILITY values (3, 74387, 'Нет')");

s.execute("insert into AVAILABILITY values (1, 74387,'Да')");

s.execute("insert into AVAILABILITY values (2, 23004,'Да')");

s.execute("insert into AVAILABILITY values (2, 01002, 'Нет')");

System.out.println("Records added.");

System.out.println("IDENTIFYING RELATIONSHIP view");

ResultSet rs1=s.executeQuery("SELECT MATERIAL.NAME, MATERIAL.SIZE, STORE.ID_STORE, AVAILABILITY.AVAILABILITY FROM MATERIAL, STORE, AVAILABILITY WHERE AVAILABILITY.ID_MATERIAL=MATERIAL.ID_MATERIAL AND AVAILABILITY.ID_STORE=STORE.ID_STORE");

if(!rs1.next())

{

System.out.println("No data selected!");

}

do

{

System.out.print(' ');

System.out.print(rs1.getString("NAME"));

System.out.print(' ');

System.out.print(rs1.getString("SIZE"));

System.out.print(' ');

System.out.print(rs1.getString("ID_STORE"));

System.out.print(' ');

System.out.print(rs1.getString("AVAILABILITY"));

System.out.println(' ');

}

while(rs1.next());

System.out.println("IDENTIFYING RELATIONSHIP view created. \n");

System.out.println("LAB №2 - NON-IDENTIFYING RELATIONSHIP. \n");

System.out.println("Creating table LOCATION...");

s.execute("create table LOCATION(ID_LOCATION int not null, ID_STORE int not null, ID_MATERIAL int not null, SHELF integer, LINE integer)");

s.execute("alter table LOCATION add primary key(ID_LOCATION)");

System.out.println("Table LOCATION created.");

System.out.println("Creating non-identifying relationship between STORE and MATERIAL...");

s.execute("alter table LOCATION add foreign key (ID_STORE) references STORE") ;

s.execute("alter table LOCATION add foreign key (ID_MATERIAL) references MATERIAL") ;

System.out.println("Non-identifying relationship LOCATION established. \n");

System.out.println("Adding records to LOCATION...");

s.execute("insert into LOCATION values (74643, 1, 01002, 10, 84)");

s.execute("insert into LOCATION values (63748, 3, 01002, 45, 10)");

s.execute("insert into LOCATION values (00034, 1, 74387, 12, 84)");

s.execute("insert into LOCATION values (87387, 1, 74387, 1, 86)");

s.execute("insert into LOCATION values (49847, 2, 01002, 35, 76)");

System.out.println("Records added.");

System.out.println("NON-IDENTIFYING RELATIONSHIP view");

ResultSet rs2=s.executeQuery("SELECT STORE.NAME, LOCATION.ID_LOCATION, LOCATION.SHELF, LOCATION.LINE FROM STORE, LOCATION WHERE LOCATION.ID_STORE=STORE.ID_STORE AND LOCATION.ID_MATERIAL=01002");

if(!rs2.next())

{

System.out.println("No data selected!");

}

do

{

System.out.print(' ');

System.out.print(rs2.getString("NAME"));

System.out.print(' ');

System.out.print(rs2.getString("ID_LOCATION"));

System.out.print(' ');

System.out.print(rs2.getString("SHELF"));

System.out.print(' ');

System.out.print(rs2.getString("LINE"));

System.out.println(' ');

}

while(rs2.next());

System.out.println("NON-IDENTIFYING RELATIONSHIP view created. \n");

System.out.println("LAB №3 - ONE-TO-ONE RELATIONSHIP. \n");

System.out.println("Creating table ADDRESS...");

s.execute("create table ADDRESS (ID_ADDRESS int not null, ID_STORE int not null, CITY varchar(20), STREET varchar(20), BUILDING varchar(20))");

s.execute("alter table ADDRESS add primary key(ID_ADDRESS,ID_STORE)");

s.execute("alter table ADDRESS add foreign key (ID_STORE) references STORE");

System.out.println("Table ADDRESS created.");

//between Store and Address - one Store has only one Address

System.out.println("Adding records to ADDRESS...");

s.execute("insert into ADDRESS values (1,3, 'Москва', 'Киевское ш.', '37')");

s.execute("insert into ADDRESS values (2,1, 'Чехов', 'ул. Профсоюзная', '9')");

s.execute("insert into ADDRESS values (3,2, 'Истра', 'ул. Ленина', '35')");

System.out.println("Records added.");

System.out.println("ONE-TO-ONE relationship Situated established. \n");

//creating view

System.out.println("Relationship view");

ResultSet rs=s.executeQuery("SELECT STORE.ID_STORE, STORE.NAME, ADDRESS.CITY, ADDRESS.STREET, ADDRESS.BUILDING FROM STORE, ADDRESS WHERE STORE.ID_STORE=ADDRESS.ID_STORE");

if(!rs.next())

{

System.out.println("No data selected!");

}

do

{

System.out.print(rs.getString("ID_STORE"));

System.out.print(' ');

System.out.print(rs.getString("NAME"));

System.out.print(' ');

System.out.print(rs.getString("CITY"));

System.out.print(' ');

System.out.print(rs.getString("STREET"));

System.out.print(' ');

System.out.print(rs.getString("BUILDING"));

System.out.println(' ');

}

while(rs.next());

System.out.println("One-to-one view created. \n");

System.out.println("LAB №4 - MANY-TO-MANY RELATIONSHIP. \n");

System.out.println("Creating table STORE_KEEP_MATERIAL...");

s.execute("create table STORE_KEEP_MATERIAL (ID_STORE int not null, ID_MATERIAL int not null)");

s.execute("alter table STORE_KEEP_MATERIAL add primary key(ID_STORE,ID_MATERIAL)");

s.execute("alter table STORE_KEEP_MATERIAL add foreign key (ID_STORE) references STORE");

s.execute("alter table STORE_KEEP_MATERIAL add foreign key (ID_MATERIAL) references MATERIAL");

System.out.println("Table STORE_KEEP_MATERIAL created.");

System.out.println("MANY-TO-MANYE relationship STORE_KEEP_MATERIAL established. \n");

System.out.println("Adding records to STORE_KEEP_MATERIAL...");

s.execute("insert into STORE_KEEP_MATERIAL values (3, 74387)");

s.execute("insert into STORE_KEEP_MATERIAL values (1, 74387)");

s.execute("insert into STORE_KEEP_MATERIAL values (2, 23004)");

s.execute("insert into STORE_KEEP_MATERIAL values (2, 01002)");

System.out.println("Records added.");

System.out.println("LAB №5 - ONE-TO-MANY RELATIONSHIP. \n");

System.out.println("Creating table STORE_WORKER...");

s.execute("create table STORE_WORKER (ID_WORKER int not null, ID_STORE int not null, NAME varchar(20), POSITION varchar(20), DEPARTMENT varchar(20))");

s.execute("alter table STORE_WORKER add primary key(ID_WORKER)");

s.execute("alter table STORE_WORKER add foreign key (ID_WORKER) references STORE_WORKER");

System.out.println("ONE-TO-MANYE relationship STORE_WORKER established. \n");

System.out.println("Adding records to STORE_WORKER...");

s.execute("insert into STORE_WORKER values (23,1,'Иванов И.И.','грузчик','доставка')");

s.execute("insert into STORE_WORKER values (456,1,'Петрова Л.П.','бухгалтер','бухгалтерия')");

s.execute("insert into STORE_WORKER values (124,2,'Сидоров А.Н.','директор','управление')");

s.execute("insert into STORE_WORKER values (76,3,'Васечкин А.Е.','логист','доставка')");

System.out.println("Records added.");

System.out.println("LAB №6 - RECURSIVE RELATIONSHIP. \n");

System.out.println("Creating table STORE_WORKER2...");

s.execute("create table STORE_WORKER2 (ID_WORKER int not null, ID_STORE int not null, NAME varchar(20), POSITION varchar(20), DEPARTMENT varchar(20))");

s.execute("alter table STORE_WORKER2 add primary key(ID_WORKER)");

s.execute("alter table STORE_WORKER2 add foreign key (ID_STORE) references STORE");

s.execute("alter table STORE_WORKER2 add ID_BOSS int");

s.execute("alter table STORE_WORKER2 add foreign key (ID_BOSS) references STORE_WORKER2");

System.out.println("Table STORE_WORKER2 created.");

System.out.println("RECURSIVE relationship BOSS established. \n");

System.out.println("Adding records to STORE_WORKER2...");

s.execute("insert into STORE_WORKER2 values (124,1,'Сидоров','директор','управление',124)");

s.execute("insert into STORE_WORKER2 values (456,1,'Петрова','Глав. Бух.','бухгалтерия',124)");

s.execute("insert into STORE_WORKER2 values (23,1,'Иванов','бухгалтер','бухгалтерия',456)");

System.out.println("Records added.");

System.out.println("LAB №7 - NETWORK RECURSIVE RELATIONSHIP. \n");

System.out.println("Creating table STORE7...");

s.execute("create table STORE7(ID_STORE int not null, NAME varchar(20))");

s.execute("alter table STORE7 add primary key(ID_STORE)");

System.out.println("Table STORE7 created.");

System.out.println("Creating table MATERIAL7...");

s.execute("create table MATERIAL7(ID_MATERIAL int not null, NAME varchar(20))");

s.execute("alter table MATERIAL7 add primary key(ID_MATERIAL)");

System.out.println("Table MATERIAL7 created.");

System.out.println("Adding records to STORE7...");

s.execute("insert into STORE7 values (1, 'Домодедовский')");

s.execute("insert into STORE7 values (2, 'Северный')");

s.execute("insert into STORE7 values (3, 'Склад №3')");

System.out.println("Records added.");

System.out.println("Adding records to MATERIAL7...");

s.execute("insert into MATERIAL7 values (01002, 'Вагонка')");

s.execute("insert into MATERIAL7 values (23004, 'Стекловата')");

s.execute("insert into MATERIAL7 values (74387, 'Блок хаус')");

System.out.println("Records added.");

System.out.println("Creating network recursive relationship...");

s.execute("alter table STORE7 add ID_MATERIAL int");

s.execute("alter table STORE7 add foreign key (ID_MATERIAL) REFERENCES MATERIAL7(ID_MATERIAL)");

s.execute("alter table MATERIAL7 add ID_STORE int");

s.execute("alter table MATERIAL7 add foreign key (ID_STORE) REFERENCES STORE7(ID_STORE)");

System.out.println("Network recursive relationship created.");

System.out.println("Adding records to STORE7...");

s.execute("update MATERIAL7 set ID_STORE = 1 where ID_MATERIAL = 01002");

s.execute("update MATERIAL7 set ID_STORE = 3 where ID_MATERIAL = 23004");

s.execute("update MATERIAL7 set ID_STORE = 2 where ID_MATERIAL = 74387");

s.execute("update STORE7 set ID_MATERIAL = 74387 where ID_STORE = 1");

s.execute("update STORE7 set ID_MATERIAL = 23004 where ID_STORE = 2");

s.execute("update STORE7 set ID_MATERIAL = 23004 where ID_STORE = 3");

System.out.println("LAB №9 - TYPE-SUBTYPE RELATIONSHIP. \n");

System.out.println("Creating table PILOMATERIAL...");

s.execute("create table PILOMATERIAL (ID_MATERIAL int not null, CATEGORY varchar(20))");

s.execute("alter table PILOMATERIAL add primary key(ID_MATERIAL)");

s.execute("alter table PILOMATERIAL add foreign key (ID_MATERIAL) references MATERIAL on delete cascade");

System.out.println("Table PILOMATERIAL created.");

System.out.println("Creating table UTEPLITEL...");

s.execute("create table UTEPLITEL (ID_MATERIAL int not null, CATEGORY varchar(20))");

s.execute("alter table UTEPLITEL add primary key(ID_MATERIAL)");

s.execute("alter table UTEPLITEL add foreign key (ID_MATERIAL) references MATERIAL on delete cascade");

System.out.println("Table UTEPLITEL created.");

System.out.println("TYPE-SUBTYPE relationship established. \n");

System.out.println("Adding records to PILOMATERIAL...");

s.execute("insert into PILOMATERIAL values (01002, 'Высшая')");

s.execute("insert into PILOMATERIAL values (64735, 'Третья')");

System.out.println("Records added.");

System.out.println("Adding records to UTEPLITEL...");

s.execute("insert into UTEPLITEL values (23004, '1')");

System.out.println("Records added.");

s.close();

System.out.println("Closed result set and statement");

/*

We end the transaction and the connection.

*/

conn.commit();

conn.close();

System.out.println("Committed transaction and closed connection");

/*

In embedded mode, an application should shut down Derby.

If the application fails to shut down Derby explicitly,

the Derby does not perform a checkpoint when the JVM shuts down, which means

that the next connection will be slower.

Explicitly shutting down Derby with the URL is preferred.

This style of shutdown will always throw an "exception".

*/

boolean gotSQLExc = false;

if (framework.equals("embedded"))

{

try

{

DriverManager.getConnection("jdbc:derby:;shutdown=true");

}

catch (SQLException se)

{

gotSQLExc = true;

}

if (!gotSQLExc)

{

System.out.println("Database did not shut down normally");

}

else

{

System.out.println("Database shut down normally");

}

}

}

catch (Throwable e)

{

System.out.println("exception thrown:");

if (e instanceof SQLException)

{

printSQLError((SQLException) e);

}

else

{

e.printStackTrace();

}

}

System.out.println("SimpleApp finished");

}

static void printSQLError(SQLException e)

{

while (e != null)

{

System.out.println(e.toString());

e = e.getNextException();

}

}

private void parseArguments(String[] args)

{

int length = args.length;

for (int index = 0; index < length; index++)

{

if (args[index].equalsIgnoreCase("jccjdbcclient"))

{

framework = "jccjdbc";

driver = "com.ibm.db2.jcc.DB2Driver";

protocol = "jdbc:derby:net://localhost:1527/";

}

if (args[index].equalsIgnoreCase("derbyclient"))

{

framework = "derbyclient";

driver = "org.apache.derby.jdbc.ClientDriver";

protocol = "jdbc:derby://localhost:1527/";

}

}

}

/**

* @param args the command line arguments

*/

//public static void main(String[] args) {

// TODO code application logic here

// }

}