How Add a Foreign Key Constraint (Make Relationships) in MySQL WorkBench

Поделиться
HTML-код
  • Опубликовано: 31 май 2024
  • @MattMacarty
    #mysql #workbench #foreignkey
    ✅ Please SUBSCRIBE:
    ruclips.net/user/subscription_...
    Add and work with foreign key constraints in MySQL
    ✅ Pure SQL Tutorial starts 7:18
    ✅ How to Create Databases:
    • How to Create a Databa...
    ✅ How to Create EER Diagrams:
    • How to Make and Use an...
    ✅ Link to GitHub:
    github.com/mjmacarty/mysql
    In this video I am going to demonstrate add a foreign key contraint in MySQL and create relationships using the workbench. See the link to download the two tables used from GitHub so you can follow along. The tables customers and orders, so we can imagine the relationships there where one customer places many orders (hopefully).
    We start off by examining and ERD of the unrelated tables. We can use the EER diagram to manually add the one-to-many relationship button here and draw the relationship in between the tables, however for this to take effect you would need to forward engineer the diagram into a new database.
    There are a number of different ways we add foreign key constraints, and the video covers the methods to do this in the workbench. First I'm going to use the GUI method, and then we will see how to do the same thing with pure SQL. The requirements are the same regardless of which method you use: you need to relate a primary key from one table to a column in a second table of the same data type and size. A couple of things to keep in mind, 1. when you import data MySQL tends to make it as generic as possible. For example any string data will be imported as text datatype; 2. MySQL does not allow text data to be defined as a primary key. MySQL allows CHAR data and INT data to be defined as primary keys. For speed, INT data is preferred, but we will be using CHAR data in this video.
    Some people prefer the GUI interaction since you can simply point click and drag. In reality, though the Workbench generates all the required SQL, which you have "apply". To me it's not easier to use the GUI interface, and I prefer the PURE SQL method. In addition, since we are dealing with SQL there's always or usually more than one way of doing things. I am going to ALTER the tables using the MODIFY keyword on the columns of interest to add both primary and foreign keys.

Комментарии • 68

  • @shinakritphanutaiwat6717
    @shinakritphanutaiwat6717 Год назад +3

    Thanks so much, this helped especially with making relationships through the code and demonstrating how to turn it into an ERD.

  • @SidAhmedHachoud
    @SidAhmedHachoud 2 года назад

    Thank you so much, this was literally life-saving!

  • @dellyemaya9184
    @dellyemaya9184 2 года назад

    Thanks again. You are the best!

  • @koraxtu5989
    @koraxtu5989 Год назад

    Thanks for this vid, it was very helpful!

  • @HarryScutt
    @HarryScutt Год назад

    You rescued my uni project thank you so much

  • @Adekorede
    @Adekorede Год назад

    Thank you so much Matt

  • @kamalkushwah2659
    @kamalkushwah2659 Месяц назад

    Thank you so much for this type of detailed video you save me...

  • @faizanwar_
    @faizanwar_ Год назад

    Nailed it ...that's the way to do it👍👍

  • @Adrianacmp
    @Adrianacmp Год назад

    You literally saved me! I've been watching videos for about 3hs and had not found any help! Thanks to you I could almost complete my first data base. The only thing left is to set some 'domains' (like a list of options that must be available when introducing new data in a field. is there any chance you could make a video about it? I'd really appreciate it!
    Anyways, thanks a lot for this video. You really know how to explain yourself and make it look so easy!

    • @MattMacarty
      @MattMacarty  Год назад +2

      Let me see what I can do, but there is a datatype in MySQL called ENUM, that does what you want. I talk about it briefly in this video: ruclips.net/video/68XMlO9fciA/видео.html starting at 14:30

    • @Adrianacmp
      @Adrianacmp Год назад

      @@MattMacarty Thanks a lot man! I'll check it

  • @user-bk6ck6ju7h
    @user-bk6ck6ju7h 4 месяца назад

    Perfect explanation, well concise

  • @ironrival9651
    @ironrival9651 2 года назад

    Wow this is a very good tutorial. Thanks a lot.😃

  • @TiiLiiXx
    @TiiLiiXx Год назад

    Thank you, it helped me

  • @miguelangelayalaleiva4939
    @miguelangelayalaleiva4939 Год назад

    Me fue de gran ayuda. tank you

  • @michaelowens2747
    @michaelowens2747 2 года назад

    Really helpful

  • @pranavarora724
    @pranavarora724 7 месяцев назад

    thanks man

  • @appajik858
    @appajik858 Год назад

    thank u so much

  • @frog706
    @frog706 2 года назад

    Brilliant video, first one that was actually useful.

  • @montrafons
    @montrafons Год назад

    Thanks for the explanation, it is very easy to understand.
    I have a question, when the SQL query has been done and the relationship between the tables has been formed in the form of an EER Diagram, it's the relationship "One to Many", is it automatic from the workbench or can we change it for this relationship? Then is there any impact on the relationship between tables in Schemas, if we change from "One to Many "to" One to One " in the EER Diagrams section?
    Thanks

    • @MattMacarty
      @MattMacarty  Год назад

      The database engine tries to determine the relationship when you add the foreign key constraint. You can edit and reimplement, but typically we don't see 1:1 relationships that often. They are usually between two primary keys.

    • @montrafons
      @montrafons Год назад

      @@MattMacarty Okay buddy, in what cases do we need 2 primary keys? Suppose that one table already has primary keys, then add primary keys from another table

  • @neux64
    @neux64 Год назад

    "you can't make primary/foreign keys using text attibutes"
    This was it, I was wondering why the latest version of MySQL/phpmyadmin was telling me I couldn't do it. I thought it was a bug since both of my datatypes were the same, but they were both TEXT type, so that's why. Thanks!

    • @MattMacarty
      @MattMacarty  Год назад

      Glad it helped. MySQL won't let you use varchar either.

  • @sweatermanny94
    @sweatermanny94 2 года назад +1

    Very useful but what about creating constraints for specific fields? For example: the field qauntity in the cart table must be more or equal to zero. Any idea on how to create those in workbench?

    • @MattMacarty
      @MattMacarty  2 года назад

      Yes you can put a UNSIGNED constraint on numerical data so : (Quantity TINYINT DEFAULT 1 UNSIGNED), this would limit input to 0 -127 and set a default value of 1

    • @anthonyfallon6070
      @anthonyfallon6070 10 месяцев назад

      I don’t know what this means, can you maybe give an example how this would be used?

  • @ate-trainingplcprogramming4837
    @ate-trainingplcprogramming4837 8 месяцев назад

    Vielen Dank . IT means in German thanks 10000x

  • @Justin-mx5ye
    @Justin-mx5ye 2 года назад

    Really insightful video. Thanks for the upload. Any chance you can upload something on complex monte carlo simulations in Python? I'd like to learn more about portfolio management from this perspective.

    • @MattMacarty
      @MattMacarty  2 года назад +1

      I have this one: ruclips.net/video/A0J0VAHzIxc/видео.html What were you thinking?

    • @Justin-mx5ye
      @Justin-mx5ye 2 года назад

      @@MattMacarty I thought this was a helpful video. I am interested in learning more about fundamental research of the stock market using python. For example, I have used monte carlo simulations to simulate varying weights of equities to maximize a sharpe ratio. Any general portfolio management/python videos are always fascinating to me. I know you're probably busy, so thank you so much in advance.

  • @miguelangelayalaleiva4939
    @miguelangelayalaleiva4939 Год назад

    Gracias

  • @user-fb7hy7gn4t
    @user-fb7hy7gn4t Год назад

    can you please describe if there are contents in the table which you have created will the contents also move to another table after foreign constrain is done and what is the use of this, kindly help

    • @MattMacarty
      @MattMacarty  Год назад +1

      No the content will not move from one table to another. You are creating a relationship with the foreign key so that you can't add or delete data from tables improperly.

    • @user-fb7hy7gn4t
      @user-fb7hy7gn4t Год назад

      @@MattMacarty thankyou for the response may I know if I need to copy the data from one table to another table is it possible

  • @iAmGratchet_
    @iAmGratchet_ 10 месяцев назад

    Is it possible to add mulitple values in a column?

    • @MattMacarty
      @MattMacarty  10 месяцев назад

      Yes but this violates first normal form. What did you have in mind?

  • @45_dzikrimufidtsaqif33
    @45_dzikrimufidtsaqif33 10 месяцев назад

    How to do if the table connection we want is in a different schema sir?

    • @MattMacarty
      @MattMacarty  10 месяцев назад

      probably the easiest thing to do is copy the table into your schema

  • @Huszan
    @Huszan Год назад

    My referenced column contains only 'Specify Column...'

    • @MattMacarty
      @MattMacarty  Год назад

      You need to enter the primary for the table that is related

    • @danilorosado8868
      @danilorosado8868 8 месяцев назад

      @@MattMacarty I still don't get it

  • @jakeevans598
    @jakeevans598 4 месяца назад

    I have error: Duplicate foreign key constraint name

    • @MattMacarty
      @MattMacarty  4 месяца назад

      sounds like you are trying to add the same foreign key constraint twice

  • @BrianGarside
    @BrianGarside Месяц назад

    Helpful, but take too long to make a point like when creating a foreign key.

  • @EricNsi
    @EricNsi Месяц назад

    You sound like the dude from ruclips.net/channel/UCmrLCXSDScliR7q8AxxjvXg

    • @MattMacarty
      @MattMacarty  28 дней назад

      Yeah sort of I guess

    • @MattMacarty
      @MattMacarty  28 дней назад

      But gosh I don't push propaganda do I?

    • @EricNsi
      @EricNsi 26 дней назад

      @@MattMacarty SQL propaganda I guess