Професионална програма
Loading...
+ Нов въпрос
mincho.spasov91 avatar mincho.spasov91 9 Точки

Basics - MySQL - 14.Create SoftUni Database - 72/100 !!!

Здравейте, моля за малко помощ. Judge ми дава 72/100 точки. Гърмят ми първите 3 теста. Моля за малко помощ не мога да разбера къде греша. Благодаря предварително.

 

Условие : 

14.Create SoftUni Database

Now create bigger database called soft_uni. You will use database in the future tasks. It should hold information about

  • towns (id, name)
  • addresses (id, address_text, town_id)
  • departments (id, name)
  • employees (id, first_name, middle_name, last_name, job_title, department_id, hire_date, salary, address_id)

Id columns are auto incremented starting from 1 and increased by 1 (1, 2, 3, 4…). Make sure you use appropriate data types for each column. Add primary and foreign keys as constraints for each table. Use only SQL queries. Consider which fields are always required and which are optional. Submit your CREATE TABLE  statements as Run queries & check DB.

 

Решение: 

 

    CREATE TABLE towns(
id INT(20) PRIMARY KEY AUTO_INCREMENT NOT NULL,
name VARCHAR(100) NOT NULL
);

CREATE TABLE addresses(
id INT(20) PRIMARY KEY AUTO_INCREMENT NOT NULL,
addres_text VARCHAR(100) NOT NULL,
town_id INT(20) NOT NULL
);

CREATE TABLE departments(
id INT(10) PRIMARY KEY AUTO_INCREMENT NOT NULL,
name VARCHAR(100) NOT NULL
);

CREATE TABLE employees(
id INT(10) PRIMARY KEY AUTO_INCREMENT NOT NULL,
first_name VARCHAR(100) NOT NULL, 
middle_name VARCHAR(100) NOT NULL, 
last_name VARCHAR(100) NOT NULL, 
job_title VARCHAR(100) NOT NULL, 
department_id INT(20) NOT NULL, 
hire_date DATE NOT NULL, 
salary DOUBLE(10,2) NOT NULL, 
address_id INT(20) NOT NULL

);

ALTER TABLE addresses ADD CONSTRAINT fk_addresses_towns FOREIGN KEY (town_id) REFERENCES towns(id);
ALTER TABLE employees ADD CONSTRAINT FK_address_id FOREIGN KEY (address_id) REFERENCES addresses(id);
ALTER TABLE employees ADD CONSTRAINT FK_department_id FOREIGN KEY (department_id) REFERENCES departments (id);

Тагове:
0
Module: Java DB 02/06/2020 07:27:21
Chakal avatar Chakal 1 Точки

Здравей, колега.

Това, което виждам е,че:

1. PRIMARY KEY го пишеш директно, без CONSTRAINT, а изрично е указано в условието да е.

 Add primary and foreign keys as constraints for each table.

2. На всички си задал NOT NULL. Consider which fields are always required and which are optional .

3. Аз AUTO_INCREMENT си го пиша с IDENTITY(1,1). Ако искам да започва с 1 и се увеличава с 1. Ако напиша IDENTITY(20,1), ще започне от 20, 21, 22 и т.н

Id columns are auto incremented starting from 1 and increased by 1 (1, 2, 3, 4…).

Пробвай с CONSTRAINT на PRIMARY KEY, мисля че ще мине.

0