HomeНаука и техникаRelated VideosMore From: Caleb Curry

Oracle SQL Tutorial 15 - How to Add Primary Key Constraints

202 ratings | 13709 views
The goal of this video is to take the CREATE TABLE statement we have and walk through the different ways to create primary keys. This and foreign keys are the most common constraints, so we need to make sure that you have this one down. Once we have a more complex database design with multiple tables, we will learn the proper way to create foreign keys. For now, I am going to keep all of our constraints at the column level, unnamed. The only exception is the primary key, because that is what we are focusing on in this video. The first way to create the primary key is at the column level, unnamed. The primary key is very important because it what we use to distinguish rows from one another. Every table you create is going to need a primary key, and I suggest putting a lot of effort into making sure your keys are set up correctly and organized. --Delete the table if needed: DROP TABLE users; CREATE TABLE users( user_id NUMBER PRIMARY KEY, username VARCHAR2(50 CHAR) NOT NULL UNIQUE ) The next way is at the column level, but named: --Delete the table: DROP TABLE users; CREATE TABLE users( user_id NUMBER CONSTRAINT users_pk PRIMARY KEY, username VARCHAR2(50 CHAR)NOT NULL UNIQUE ) The general naming convention here is the table name followed by an underscore, followed by pk for primary key. Finally, the third way, which is at the table level, is the way we are going to create our primary key: --Delete the table: DROP TABLE users; CREATE TABLE users( user_id NUMBER, username VARCHAR2(50 CHAR) NOT NULL UNIQUE, CONSTRAINT users_pk PRIMARY KEY (user_id) ) Now, once you've created these constraints, you can use Oracle SQL Developer to find these constraints. Open your databases in the connections tab and find the table in the Tables folder. Double click your table and travel to the Constraints tab. You can tell here that the UNIQUE constraint still exists in this table, but it has a pretty disgusting name. It kind of wants to make me puke. Referencing that constraint in the future with that wacky name would be a burden. Engrave these three options in your head so that you can use any of them whenever you need and can read other peoples' code. Thank you for sticking with the series thus far. In the next video, we will be…doing something. See you then! :) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Support me on Patreon! http://www.patreon.com/calebcurry Subscribe to my newsletter: http://bit.ly/JoinCCNewsletter Donate!: http://bit.ly/DonateCTVM2. ~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~ More content: http://CalebCurry.com Facebook: http://www.facebook.com/CalebTheVideoMaker Google+: https://plus.google.com/+CalebTheVideoMaker2 Twitter: http://twitter.com/calebCurry Amazing Web Hosting - http://bit.ly/ccbluehost (The best web hosting for a cheap price!)
Html code for embedding videos on your blog
Text Comments (9)
why do you drop table if its an "empty" table?
mohsin khan (1 year ago)
Miguel Eduardo San Martín Suárez to recreate other
Daan Djurak (1 year ago)
make us videos for select oreders in sql,u will have many job there:)
Varun K (1 year ago)
Hello Caleb, How to put CONSTRAINT on multiple columns e.g. If I needed to have one column as the primary key and some other column which can not have null? And this I want to apply on Table level Constraints.**
Vesna Stojanovic (1 year ago)
Love the silly comments, makes this even more interesting. You're a lifesaver. Thank you.
Ankita Sharma (1 year ago)
Your videos are awesome Caleb!!
Kaynaat Khan (1 year ago)
it's giving me error missing right parentheses at the end where PRIMARY KEY(user_id)...
Leo Cabrera (2 years ago)
Hey man, as someone learning SQL, I really appreciate the time and effort you are placing into your videos! They're really great! Keep up the good work!
Caleb Curry (2 years ago)
Thank you Leo. I will keep the content coming!

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.