|
26 | 26 | }, |
27 | 27 | { |
28 | 28 | "cell_type": "code", |
29 | | - "execution_count": 3, |
| 29 | + "execution_count": 9, |
30 | 30 | "metadata": {}, |
31 | 31 | "outputs": [ |
32 | 32 | { |
|
139 | 139 | "The parent table `Title` is above and the child table `Employee` is below." |
140 | 140 | ] |
141 | 141 | }, |
| 142 | + { |
| 143 | + "cell_type": "markdown", |
| 144 | + "metadata": {}, |
| 145 | + "source": [ |
| 146 | + "## Defining Foreign Keys in SQL\n", |
| 147 | + "In standard SQL, the same relationship is defined with more verbose syntax. The child table must explicitly redefine the columns of the parent's primary key and then declare the foreign key constraint.\n", |
| 148 | + "\n", |
| 149 | + "Notice that the data type (`char(8)`) of the primary key in `Title` must be exactly repeated for the `title_code` column in `Employee`.\n", |
| 150 | + "\n", |
| 151 | + "```sql\n", |
| 152 | + "-- Parent Table\n", |
| 153 | + "CREATE TABLE title (\n", |
| 154 | + " title_code CHAR(8) NOT NULL,\n", |
| 155 | + " full_title VARCHAR(120) NOT NULL,\n", |
| 156 | + " PRIMARY KEY (title_code)\n", |
| 157 | + ");\n", |
| 158 | + "\n", |
| 159 | + "-- Child Table\n", |
| 160 | + "CREATE TABLE employee (\n", |
| 161 | + "person_id INT NOT NULL,\n", |
| 162 | + "first_name VARCHAR(30) NOT NULL,\n", |
| 163 | + "last_name VARCHAR(30) NOT NULL,\n", |
| 164 | + "title_code CHAR(8) NOT NULL,\n", |
| 165 | + "PRIMARY KEY (person_id),\n", |
| 166 | + "FOREIGN KEY (title_code) REFERENCES title(title_code)\n", |
| 167 | + ");\n", |
| 168 | + "```\n", |
| 169 | + "\n", |
| 170 | + "The concise `-> Title` syntax in DataJoint handles this automatically, reducing redundancy and preventing potential errors if the parent's primary key definition changes." |
| 171 | + ] |
| 172 | + }, |
142 | 173 | { |
143 | 174 | "cell_type": "markdown", |
144 | 175 | "metadata": {}, |
|
0 commit comments