{"id":1002,"date":"2024-12-03T17:31:49","date_gmt":"2024-12-03T12:01:49","guid":{"rendered":"https:\/\/csaccept.com\/?p=1002"},"modified":"2024-12-04T09:51:24","modified_gmt":"2024-12-04T04:21:24","slug":"what-is-primary-key-and-foreign-key-with-example","status":"publish","type":"post","link":"https:\/\/csaccept.com\/what-is-primary-key-and-foreign-key-with-example\/","title":{"rendered":"What is Primary Key and Foreign Key? With Example? DBMS Part-4"},"content":{"rendered":"\n

Primary Key and Foreign Key<\/strong><\/h3>\n\n\n\n

Primary Key: <\/h2>\n\n\n\n

It is a unique value that is used to identify a row in a table.<\/p>\n\n\n\n

If you are thinking about unique constant, then you must to know the difference unique contant can store null values also primary key can not store null values.<\/p>\n\n\n\n

Syntax:- <\/strong><\/p>\n\n\n\n

create table tablename(<\/p>\n\n\n\n

column1 datatype,<\/p>\n\n\n\n

column2 datatype,<\/p>\n\n\n\n

……………………………..<\/p>\n\n\n\n

primary key (column name)<\/p>\n\n\n\n

);<\/p>\n\n\n\n

A table can contain only one Primary Key.<\/strong><\/p>\n\n\n\n

<\/p>\n\n\n\n

Foreign Key: <\/h2>\n\n\n\n

It is a key which is used to link two table together. <\/p>\n\n\n\n

Foreign Key can have multiple null value.<\/p>\n\n\n\n

Syntex:-<\/strong><\/p>\n\n\n\n

create table tablename(<\/p>\n\n\n\n

column1 datatype,<\/p>\n\n\n\n

column2 datatype,<\/p>\n\n\n\n

…………………………….<\/p>\n\n\n\n

foreign key [column1, column2,……………….]<\/p>\n\n\n\n

references [primary key table name]<\/p>\n\n\n\n

);<\/p>\n\n\n\n

<\/p>\n\n\n\n

Let’s take Example:-<\/strong> (Student Table)<\/strong><\/p>\n\n\n\n

Roll No.<\/strong><\/td>Name<\/strong><\/td>Address<\/strong><\/td><\/tr>
1<\/td>Lav pratap<\/td>Delhi<\/td><\/tr>
2<\/td>Harshit<\/td>Mumbai<\/td><\/tr>
3<\/td>Aayan Krishna<\/td>Kolkata<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

Primary Key<\/strong><\/h3>\n\n\n\n

-Name and Address are not unique. <\/p>\n\n\n\n

-Here Roll No. is unique.<\/p>\n\n\n\n

-So we discussed before, primary key is unique and not null value.<\/p>\n\n\n\n

<\/p>\n\n\n\n

Result Table<\/h2>\n\n\n\n
Maths<\/strong><\/td>Science<\/strong><\/td>English<\/strong><\/td><\/tr>
10%<\/td>80%<\/td>70%<\/td><\/tr>
90%<\/td>60%<\/td>80%<\/td><\/tr>
70%<\/td>50%<\/td>60%<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

<\/p>\n\n\n\n

-You want to check Roll NO. 3 Marks in Maths, Foreign Key come into picture Student Table + Result Table.<\/p>\n\n\n\n

We have to add Primary Key in Result Table which act as Foreign Key.<\/p>\n\n\n\n

Foreign Key<\/h2>\n\n\n\n
Maths<\/strong><\/td>Science<\/strong><\/td>English<\/strong><\/td>Roll No.<\/strong><\/td><\/tr>
10%<\/td>80%<\/td>70%<\/td>1<\/td><\/tr>
90%<\/td>60%<\/td>80%<\/td>2<\/td><\/tr>
70%<\/td>50%<\/td>60%<\/td>3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

<\/p>\n\n\n\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

Primary Key and Foreign Key Primary Key: It is a unique value that is used to identify a row in a table. If you are thinking about unique constant, then…<\/p>\n","protected":false},"author":2,"featured_media":1003,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[32],"tags":[],"class_list":["post-1002","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dbms"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/csaccept.com\/wp-content\/uploads\/2024\/12\/primary.jpg?fit=1024%2C442&ssl=1","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/csaccept.com\/wp-json\/wp\/v2\/posts\/1002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csaccept.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csaccept.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csaccept.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/csaccept.com\/wp-json\/wp\/v2\/comments?post=1002"}],"version-history":[{"count":3,"href":"https:\/\/csaccept.com\/wp-json\/wp\/v2\/posts\/1002\/revisions"}],"predecessor-version":[{"id":1010,"href":"https:\/\/csaccept.com\/wp-json\/wp\/v2\/posts\/1002\/revisions\/1010"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/csaccept.com\/wp-json\/wp\/v2\/media\/1003"}],"wp:attachment":[{"href":"https:\/\/csaccept.com\/wp-json\/wp\/v2\/media?parent=1002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csaccept.com\/wp-json\/wp\/v2\/categories?post=1002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csaccept.com\/wp-json\/wp\/v2\/tags?post=1002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}