简体   繁体   中英

Table without primary key in DB?

I have 4 tables in DB, and they are regularly connected. Now i have to make new table without new primary key, but it must have foreighn key from other table.

Is it possible?

if your relationship from your new table to other table is one to one, the foreign key can be used as your primary key. otherwise you will need a single or composite primary key to avoid redundancy of rows.

Yes. To do this you need an index on the column involved in the foreign key. But it need not be a unique index.

Note this. Primary keys, and all indexes, can have multiple columns in them. If your new table is a so-called join table showing many-to-many relationships between rows of two other tables, you can create a primary key from both columns. For example, if you have a student table with the primary key student_id , and a teacher table with the primary key teacher_id , you could create a table like this.

CREATE TABLE student_teacher (
    student_id INT NOT NULL,
    teacher_id INT NOT NULL,
    PRIMARY KEY (student_id, teacher_id),
    UNIQUE INDEX teacher_student (teacher_id, student_id)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM