MySQL best approach for db normalising, relationships and foreign keys

前端 未结 3 1260
感情败类
感情败类 2021-01-26 04:04

There is an username/password verification step first then the database has following structure

^ is primary key

* uses foreign key


1.StudentDetails table
===         


        
相关标签:
3条回答
  • 2021-01-26 04:23

    It's better to use foreign keys in number format So the Username should be the id of the StudentDetails table.

    Can you put the SQL Query that you had tried to run?

    0 讨论(0)
  • 2021-01-26 04:25

    option 1:
    enter the adress data first, and use that id when createing the row in StudentDetails

    option 2:
    change the field StudentDetails.Address so its allow NULL valus, enter the StudentDetails, then the adress, and then update the StudentDetails.Address

    0 讨论(0)
  • 2021-01-26 04:37

    Ok let me explain you how it would be. I made an example with two tables that you can see below.

    simple model

    Then you can create your query.

    mysql> show tables;
    +----------------+
    | Tables_in_test |
    +----------------+
    | addresses      |
    | students       |
    +----------------+
    2 rows in set (0.00 sec)
    
    mysql> select * from students;
    +----+----------+-----------+
    | id | name     | last_name |
    +----+----------+-----------+
    |  1 | jhon     | smith     |
    |  2 | anderson | neo       |
    |  3 | trinity  | jackson   |
    +----+----------+-----------+
    3 rows in set (0.00 sec)
    
    mysql> select * from addresses;
    +----+-----------------+---------+
    | id | address         | student |
    +----+-----------------+---------+
    |  1 | Av 1 2nd Street |       1 |
    |  2 | Av 3 4 Street   |       2 |
    |  3 | St 23 7 Av      |       3 |
    +----+-----------------+---------+
    3 rows in set (0.00 sec)
    
    mysql> select s.name,s.last_name,a.address from students s join addresses a on a.student=s.id;
    +----------+-----------+-----------------+
    | name     | last_name | address         |
    +----------+-----------+-----------------+
    | jhon     | smith     | Av 1 2nd Street |
    | anderson | neo       | Av 3 4 Street   |
    | trinity  | jackson   | St 23 7 Av      |
    +----------+-----------+-----------------+
    3 rows in set (0.00 sec)
    
    0 讨论(0)
提交回复
热议问题