Foreign Key #6 | MySQL | Bahasa Indonesia

Guntur Budi
10 Apr 201609:22

Summary

TLDRThe video script provides a detailed tutorial on creating a 'table barang' with specific columns and attributes in a database. It explains the process of defining columns such as 'barang ID', 'nama barang', 'harga beli', 'harga jual', 'supplier ID', and 'satuan ID', and setting them with appropriate data types and constraints. The script further elaborates on establishing foreign key relationships with 'supplier' and 'satuan' tables, including the use of 'on delete' and 'on update' clauses with 'restrict' and 'cascade' options. It also covers the creation of foreign keys in phpMyAdmin and HeidiSQL, emphasizing the importance of indexing and primary keys for relational integrity.

Takeaways

  • πŸ“ The script discusses creating a new table named 'table barang' with specific columns and data types.
  • πŸ”‘ It emphasizes defining primary keys ('barang ID') and foreign keys ('supplier ID' and 'satuan ID') with their respective references to other tables.
  • πŸ› οΈ The script mentions creating indexes for foreign keys before defining them to ensure proper database relationships.
  • πŸ—‚οΈ The 'on delete' and 'on update' clauses are explained, with options like 'restrict' and 'cascade' to manage referential integrity.
  • 🚫 The use of 'restrict' is recommended over 'cascade' for 'on delete' to prevent accidental deletions that could leave orphaned records.
  • πŸ”„ For 'on update', 'cascade' is preferred to automatically update related records in child tables when changes are made in the parent table.
  • πŸ’Ύ The script outlines the process of creating tables with foreign keys in both command-line interfaces like MySQL and graphical tools like phpMyAdmin and HeidiSQL.
  • πŸ–₯️ In phpMyAdmin, tables must be created first before defining foreign keys, which is done through the 'Foreign key constraints' section under 'Structure'.
  • πŸ’» In HeidiSQL, creating a new table allows for immediate addition of foreign keys, with a straightforward interface to define relationships and constraints.
  • πŸ“‹ The script highlights the importance of naming indexes uniquely to avoid conflicts and ensure clear database structure.
  • πŸ” It provides a detailed walkthrough of the steps involved in defining table structures, including primary and foreign keys, and the implications of different 'on delete' and 'on update' options.

Q & A

  • What is the purpose of creating a 'table barang' as described in the transcript?

    -The purpose is to create a new table named 'table barang' to store information about items, including their ID, name, purchase price, selling price, supplier ID, and unit ID, with appropriate constraints and relationships to other tables.

  • What data types and constraints are assigned to the 'barang ID' and 'nama barang' columns in the 'table barang'?

    -The 'barang ID' is assigned an 'INT' data type with 'NOT NULL', 'AUTO_INCREMENT' properties, and the 'nama barang' is given a 'VARCHAR' data type with a length of 50 and also marked as 'NOT NULL'.

  • How are the 'harga beli' and 'harga jual' columns defined in the 'table barang'?

    -Both 'harga beli' and 'harga jual' columns are defined as 'INTEGER' data types with 'NOT NULL' constraints, indicating that these fields must always have a value and cannot be left empty.

  • What is the significance of 'supplier ID' and 'satuan ID' in the 'table barang'?

    -The 'supplier ID' and 'satuan ID' are foreign keys in the 'table barang' that establish relationships with the 'supplier' and 'satuan' tables, respectively. They are used to reference the supplier and unit of measurement for each item.

  • Why is it necessary to create indexes before defining foreign keys in the 'table barang'?

    -Indexes are necessary before defining foreign keys to ensure that the database can efficiently look up and match the values in the foreign key columns with the referenced primary keys in the related tables.

  • What is the difference between 'ON DELETE RESTRICT' and 'ON DELETE CASCADE' as foreign key actions in the 'table barang'?

    -With 'ON DELETE RESTRICT', the deletion of a referenced row in the parent table (supplier or satuan) is not allowed if there are dependent rows in the child table (table barang). 'ON DELETE CASCADE', on the other hand, automatically deletes the dependent rows in the child table when a referenced row in the parent table is deleted.

  • What does 'ON UPDATE CASCADE' mean in the context of the 'table barang'?

    -'ON UPDATE CASCADE' means that if the value of the referenced key in the parent table (supplier or satuan) is updated, the corresponding foreign key values in the child table (table barang) will also be updated automatically.

  • Why might someone choose 'ON DELETE RESTRICT' over 'ON DELETE CASCADE' when defining foreign keys?

    -Choosing 'ON DELETE RESTRICT' over 'ON DELETE CASCADE' is a way to prevent accidental deletions of parent records that are still referenced by child records, thus ensuring data integrity and avoiding orphaned records in the child table.

  • How can the process of creating a table with foreign keys be done in phpMyAdmin?

    -In phpMyAdmin, one must first create the table without foreign keys. After the table is created, navigate to the 'Structure' tab, then to 'Foreign key constraints' to define the foreign keys and their constraints.

  • What is the process of adding a table with foreign keys in HeidiSQL?

    -In HeidiSQL, you can create a new table and add columns directly. After defining the primary key and any necessary indexes, you can add foreign keys by specifying the foreign key name, the column, the referenced table and column, and the actions for 'ON DELETE' and 'ON UPDATE'.

Outlines

00:00

πŸ’» Database Table Creation with Foreign Keys

This paragraph describes the process of creating a database table named 'table barang' with various columns including 'barang ID', 'nama barang', 'harga beli', 'harga jual', 'supplier ID', and 'satuan ID'. The 'barang ID' is set as an auto-incrementing primary key. The 'supplier ID' and 'satuan ID' are defined as foreign keys that reference the 'supplier' and 'satuan' tables, respectively. The speaker emphasizes the need to create indexes before defining foreign keys. The foreign key constraints are set with 'on delete restrict' and 'on update cascade' to ensure referential integrity and automatic updates. The paragraph also discusses the importance of defining primary and foreign keys correctly to maintain database relationships.

05:01

πŸ› οΈ Troubleshooting and Creating Foreign Keys in Database Management Tools

The second paragraph focuses on troubleshooting common errors when creating a table with foreign keys and the steps to create such tables using database management tools like phpMyAdmin and HeidiSQL. It mentions the need to correct syntax errors, such as missing parentheses, and to set proper index names to avoid conflicts. The paragraph explains how to add foreign key constraints in phpMyAdmin by accessing the 'relation' view and filling in the properties for the foreign key. It also covers the process in HeidiSQL, where after creating a new table, you can add columns and define indexes before setting up the foreign key constraints. The speaker prefers using 'cascade' for updates to simplify the process of maintaining referential integrity across related tables.

Mindmap

Keywords

πŸ’‘Table

In the context of the video, a 'table' refers to a structured collection of data in a database, organized into rows and columns. The video script describes the creation of a new table named 'table barang', which is used to store information about items or products. This table is central to the video's theme of database management and schema design.

πŸ’‘Column

A 'column' in the video script represents an individual field or attribute within a table that holds data for a specific category. The script outlines several columns such as 'barang ID', 'nama barang', 'harga beli', 'harga jual', 'supplier ID', and 'satuan ID', each designed to store different types of information about the items in the 'table barang'.

πŸ’‘Integer

The term 'integer' is used in the script to define the data type for certain columns in the table, such as 'barang ID', 'supplier ID', and 'satuan ID'. In database terminology, an integer is a whole number, which is suitable for storing values like IDs where decimal points are not applicable. The video emphasizes the use of integers for primary keys and identifiers.

πŸ’‘VARCHAR

In the script, 'VARCHAR' is mentioned as the data type for the 'nama barang' column, which is used to store variable-length strings, such as the names of items. VARCHAR is a common data type in SQL for storing character strings, allowing for flexibility in the length of the data, which is essential for names that can vary in length.

πŸ’‘NOT NULL

The 'NOT NULL' constraint mentioned in the script is used to ensure that certain columns do not allow NULL values. This is critical for columns like 'harga beli' and 'harga jual', where every item must have a defined buying and selling price. The video uses 'NOT NULL' to enforce data integrity and completeness in the database.

πŸ’‘AUTO_INCREMENT

The 'AUTO_INCREMENT' attribute is used for the 'barang ID' column in the script to automatically generate a unique identifier for each new record. This feature is essential in database design for primary keys, ensuring that each item has a unique ID without manual intervention, as illustrated in the video's discussion on table creation.

πŸ’‘Foreign Key

A 'Foreign Key' in the video script refers to a column or set of columns that refer to the primary key of another table, establishing a link between the two tables. The script describes setting up foreign keys for 'supplier ID' and 'satuan ID', which reference the 'supplier' and 'satuan' tables, respectively. This is a key concept in relational database design for maintaining referential integrity.

πŸ’‘ON DELETE

The 'ON DELETE' clause in the script is part of the foreign key constraint and specifies the action to be taken when a referenced record in the parent table is deleted. The video discusses using 'ON DELETE RESTRICT' to prevent deletion of a parent record if it is still referenced by child records, ensuring data consistency.

πŸ’‘ON UPDATE

Similar to 'ON DELETE', 'ON UPDATE' in the script dictates the behavior when a referenced record in the parent table is updated. The video mentions a preference for 'ON UPDATE CASCADE', which automatically updates the child records to match the changes in the parent record, streamlining data maintenance across related tables.

πŸ’‘Primary Key

A 'Primary Key' is a column or a set of columns that uniquely identifies each row in a table. In the video, 'barang ID' is defined as the primary key for the 'table barang', ensuring that each item can be distinctly identified. The primary key is a fundamental concept in database design, as it serves as the basis for indexing and relationships with other tables.

πŸ’‘Index

An 'Index' in the script refers to a database object that improves the speed of data retrieval operations on a table. The video mentions creating indexes for 'supplier ID' and 'satuan ID' before defining foreign keys, which is a common practice to enhance performance when accessing and joining tables based on these columns.

Highlights

Creating a new table named 'table barang' with specific column definitions.

Defining 'barang ID' as an integer with auto-increment, not null.

Setting 'nama barang' with a varchar length of 50, not null.

Establishing 'harga beli' as an integer, not null.

Setting 'harga jual' as an integer, not null.

Adding 'supplier ID' and 'satuan ID' as integers to reference other tables.

Describing the relationship of 'supplier ID' and 'satuan ID' as foreign keys referencing 'supplier' and 'satuan' tables.

Mentioning the need to create indexes before defining foreign keys.

Defining indexes for 'supplier ID' and 'satuan ID'.

Setting up foreign key constraints for 'supplier ID' with 'on delete restrict'.

Discussing the implications of 'on delete restrict' and 'on delete cascade'.

Explaining the use of 'on update cascade' for 'supplier ID'.

Creating a foreign key for 'satuan ID' with similar constraints as 'supplier ID'.

Defining 'barang ID' as the primary key of the table.

Mentioning the importance of closing the table creation query with a semicolon.

Discussing the process of creating a table with foreign keys in phpMyAdmin.

Explaining the process of adding foreign keys in HeidiSQL by creating a new table and defining columns.

Describing the steps to add foreign keys in HeidiSQL, including naming the constraint and selecting the reference table and column.

Transcripts

play00:00

Oke selanjutnya kita akan Coba buat satu

play00:02

table lagi bernama table barang

play00:10

oke kolom-kolomnya adalah yang pertama

play00:13

barang ID saya kasih Inter integer not

play00:17

Nul auto

play00:20

increment lalu nama

play00:24

barangnya saya kasih varchar panjangnya

play00:28

50 not n

play00:31

juga lalu selanjutnya saya kasih harga

play00:35

belinya

play00:37

integer not

play00:40

Nul selanjutnya saya kasih harga

play00:44

jualnya inteer not

play00:48

N0 selanjutnya saya kasih supplier

play00:53

ID

play00:55

integer dan juga satuan id

play01:00

integer

play01:03

Oke Nah di sini dapat dilihat ya

play01:06

bahwasanya di sini saya mendefinisikan

play01:08

supplier ID dan satuan ID ya supplier ID

play01:11

nanti akan mereferensi ke tabel supplier

play01:15

ya satuan ID juga akan mereferensi ke

play01:17

tabel satuan ya Jadi nanti bareng

play01:20

menjadi Child untuk parent-nya ya

play01:22

parent-nya yaitu satuan dan supplier

play01:26

Oke Oleh karena itu ini yang dinamakan

play01:27

dengan Foreign Key ya sebelum membuat

play01:30

forign ke kita harus membuat indeksnya

play01:32

terlebih dahulu ya jadi di sini saya

play01:34

definisikan

play01:36

indek lalu di sini saya berikan indeks

play01:39

untuk supplier

play01:42

ID lalu indeks

play01:45

untuk satuan

play01:51

ID baru di sini saya eh Deskripsikan

play01:54

foreign ke-nya forign ke-nya yaitu untuk

play01:58

supplier id

play02:00

dulu ya ID dulu yaitu references ya mau

play02:06

mereference ke tabel yang mana yaitu ke

play02:09

tabel supplier di kolom yang mana ya di

play02:12

kolom yang mana tadi kalau misalkan kita

play02:14

lihat itu supplier ID ya di kolom

play02:17

supplier

play02:19

id lebih baik sih ininya sama aja ya Ini

play02:22

sama aja cuman e karena sudah terlanjur

play02:24

tidak apa-apa Lalu nanti ada perintah

play02:28

yang namanya

play02:31

deleteah on delete ini adalah perintah

play02:35

jika nanti saya melete parentnya ya jika

play02:38

saya nanti misalkan di sini Saya sudah

play02:39

punya barang dengan supplier toko toko a

play02:43

misalkan lalu di table parentnya ya toko

play02:47

A itu saya mau hapus itu apa yang akan

play02:49

dilakukan di tabel barang ini ya jika on

play02:53

deletnya restri ya restri nanti tablenya

play02:58

tidak akan bisa dihus lama masih ada

play03:00

child-nya di tabel barang ya jika on

play03:04

delate-nya restrict jika on delete-nya

play03:06

cascate ya jika nanti saya hapus di

play03:09

table parent supplier maka table Child

play03:12

yang ada di tabel barang nanti akan ikut

play03:15

terhapus ya saya lebih suka menggunakan

play03:18

restrict agar aman gitu ya jadi nanti

play03:21

tidak ee Tidak sembarangan orang mau

play03:23

menghapus dari tabel supplier ya Jika

play03:25

masih ada barangnya yang mereferensi ke

play03:28

tabel supplier tersebut maka tidak akan

play03:30

bisa di-delete ya Ada juga satu lagi on

play03:33

update ya on update sifatnya sama ya Ada

play03:36

cascade ada restrict ya kalau update

play03:38

Saya lebih suka pakai cascate ya Jadi

play03:40

tidak merepotkan nanti ketika mau update

play03:43

tinggal ee misalkan di sini saya mau

play03:45

update ID ID supplier-nya ya nanti yang

play03:48

di table barang akan ikut terupdate gitu

play03:50

ya nanti kalau misalkan restrict jika

play03:52

saya mau update di sini maka di sini

play03:54

tidak bisa ter maka eh update-nya akan

play03:57

gagal karena yang di sini masih ada

play03:59

child-nya ya jadi itu adalah restrict

play04:01

dan update eh restrict dan cascate yang

play04:04

bisa diterapkan di on delete dan on

play04:06

update ya lalu satu lagi saya buat

play04:09

foreign key-nya untuk satuan ID ya

play04:15

mereferensikan

play04:16

ke

play04:22

satuan Ya akan saya samakan on

play04:25

delete-nya restrict juga on update-nya

play04:28

juga

play04:30

skate oke lalu saya definisikan primary

play04:35

key-nya yaitu adalah barang ID

play04:42

oke enter saya tutup lalu saya kasih

play04:45

titik koma Oh ini kurang tutup ya ini

play04:48

kurang tutup Oke kita bisa ulangi nah

play04:51

ini adalah repotnya jika kita

play04:52

menggunakan comen from Ya kita harus

play04:56

satu-satu seperti ini

play05:00

ya tidak apa-apa sekaligus kita tahu

play05:02

gimana caranya kalau misalkan ada error

play05:05

ya oke di sini kita harus tambahkan

play05:10

tanda kurungnya tinggalan tadi

play05:13

oke indeksnya sudah on delete restrict

play05:18

on update case gate sepertinya

play05:21

Oke enter

play05:25

oke query oke no R affected ya Ini

play05:28

adalah cara untuk membuat table yang

play05:31

mempunyai kunci forign ke Oke hal ini

play05:34

juga kita bisa lakukan di php my admin

play05:37

dan juga di hidsql

play05:39

Oke di php my admin caranya adalah

play05:42

dengan pertama kita buat tableennya dulu

play05:45

ya kita buat tablenya dulu lalu setelah

play05:48

membuat table ya jadi tidak bisa

play05:49

langsung ketika buat table langsung buat

play05:51

foring Sepertinya kalau di php my admin

play05:53

kita harus buat table-nya dulu lalu kita

play05:55

klik relation view relation view ada di

play05:58

Tab struktur ya di forign key constraint

play06:02

ya di sini Biarkan saja lalu ke forign

play06:04

key constraint langsung ya forign key

play06:05

constraint langsung di sini ada

play06:07

properties-prerties untuk ngisi forign

play06:09

ke-nya ya di sini karena sudah saya buat

play06:12

sebelumnya ya Nah ini adalah nama

play06:14

indeksnya nama indeksnya terserah nama

play06:16

indeksnya terserah namun jangan sampai

play06:18

sama dengan nama indeks yang lain ya

play06:20

harus dibedakan kalau saya membuat

play06:22

melalui comand Prom tadi ya ini saya

play06:25

tidak perlu menuliskan nama indeksnya

play06:27

sudah akan dibuat secara otomatis oleh

play06:29

eh mycale ya lalu selanjutnya adalah

play06:33

kolom dari tabel barang kolom dari tabel

play06:36

barang yang ingin dibuat foren ke-nya

play06:37

lalu di sini adalah references-nya ya

play06:39

references-nya lalu di sini ya on delete

play06:42

sama on update-nya ya Tadi ada restrict

play06:45

dan ada casgate di sini juga ada pilihan

play06:46

lain yaitu set null dan no action ya

play06:48

cuman e yang lebih sering dipakai adalah

play06:51

casgate dan restrict saja ya di sini

play06:53

kita bisa tambahkan beberapa forignk

play06:56

sekaligus di sini lalu kita bisa Klik

play06:58

simpan ya Ini adalah cara untuk membuat

play07:02

fornk di php my admin Lalu bagaimana

play07:05

caranya untuk menambahkan table dengan

play07:07

forign di hdsl ya kita bisa buat tabel

play07:11

baru

play07:13

oke tabel baru lalu di sini saya

play07:15

ketikkan nama tabelnya misalkan saya

play07:17

untuk contoh saja barang dua lalu

play07:20

misalkan saya tambahkan tiga kolom di

play07:21

sini yang pertama barang ID lalu

play07:25

langsung saja supplier id

play07:30

lalu langsung saja juga satuan ID oke

play07:34

lalu jangan dilupakan

play07:36

indeksnya barang Id adalah primary nah

play07:40

supplier ID ya kita juga buat index key

play07:43

ya ini adalah Eh sama dengan seperti

play07:47

saya membuat indeks seperti ini ya key

play07:50

lalu juga di sini juga saya tambahkan

play07:52

key

play07:54

baru oke Setelah itu saya bisa buat

play07:57

foreign key-nya

play07:59

foreign key-nya yang pertama bisa saya

play08:01

klik tambah ya dinamakan dinamakan

play08:05

misalkan di sini supplier FK ya foren

play08:10

kolomnya supplier ID tabel

play08:14

referensinya supplier kolom

play08:17

foren-nya supplier ID oke lalu saya bisa

play08:21

tambah lagi misalkan satuan id-nya tadi

play08:24

satuan forign ke ya namaak ini ya

play08:28

seperti saya utarakan tadi ini terserah

play08:29

ya Yang penting jangan sama satuan ID

play08:33

tabel referensinya satuan kolom forign

play08:36

key-nya satuan ID Oke on update on

play08:39

delete-nya Saya cascate di sini

play08:42

cascate ya setelah itu sebenarnya bisa

play08:45

kita klik kode create-nya dulu ya hampir

play08:49

sama ya barang dua barang ID supplier ID

play08:52

Primary Key nah ini ya

play08:54

indeksnya seperti saya putarkan tadi Nah

play08:57

di sini ada constrain-nya ini adalah

play08:58

nama forign ke-nya

play09:03

Oke sudah kita bisa Klik

play09:08

simpan ya Ini adalah cara untuk membuat

play09:11

table yang yang memiliki forignk pada

play09:15

comand Prom lalu pada PHP my admin dan

play09:18

pada heid SQL

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Database DesignForeign KeyData ManagementSQL TutorialPHPMyAdminMySQLTable CreationData IntegrityDatabase SchemaProgramming