A
A
Arti-Jack2017-05-03 21:36:17
MySQL
Arti-Jack, 2017-05-03 21:36:17

How can I parse this SQL query?

Good day.
I want to say right away that in SQL I don’t know more than basic things (such as SELECT, MAKE TABLE, WHERE, joining tables, etc.).
But still, there is the following task:

The FAKULTY attribute is a foreign key to the FACULTIES table, with ID as the primary key. Restore the foreign key setup order.
CREATE TABLE STUDENTS
( ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
NAME TEXT NOT NULL,
 COURSE INTEGER NOT NULL, 
FAKULTY INTEGER NOT NULL, 
//установка внешнего ключа)

KEY, (ID), REFERENCE, FACULTIES, FOREIGN, (FACULTY)

Well, you need to add a request.
I know the answer is: FOREIGN KEY (FAKULTY) REFERENCES FACULTIES (ID), but the problem is that I don't understand what this piece of code does. I would be very grateful if you could explain to me.
I apologize in advance for such a noob question.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Black Tulip, 2017-05-03
@Black_Tulip

This is done very simply:

CREATE TABLE STUDENTS
( ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
NAME TEXT NOT NULL,
 COURSE INTEGER NOT NULL, 
FAKULTY INTEGER NOT NULL, 
CONSTRAINT FK_FacultyStudent FOREIGN KEY (FAKULTY)
    REFERENCES FACULTIES (ID)

Now let's look at this piece of code:
CONSTRAINT FK_FacultyStudent FOREIGN KEY (FAKULTY)
    REFERENCES FACULTIES (ID)

CONTRAINT FK_FacultyStudent - create a constraint on a specific field
FOREIGN KEY (FAKULTY) - the name of the constraint itself, in this case the secondary key, the field in brackets that will be used in the current table to connect to the second table. It is best to call such a field like this FakultyID, for better understanding and clarification that this is a secondary key
REFERENCES FACULTIES (ID) - we say that the FAKULTY field will be associated with the ID field in the FACULTIES table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question