Oracle SQL Tutorial 13 - How to Add Column Constraints (Attributes)

submitted by mkenny400 on 03/23/18 1

So far, we have this table declaration: CREATE TABLE users( user_id NUMBER, username VARCHAR(50 CHAR) first_name VARCHAR2(50 CHAR), last_name VARCHAR2(50 CHAR) ) We can run this command see that it works. As we are learning though, we are going to want to be able to recreate our table with different settings and such, but if you try to run this command, it will complain that the table already exists. So we first need to delete this table before we start editing settings. When we get more experienced, we will learn about ways to edit the structure of a table that already exists. To fix this, we can get rid of the table using the DROP TABLE command: DROP TABLE users You can run this every time if you need to practice by adding a semicolon after it. This is how you can tell Oracle that you are putting in another command after it. This is known as a delimiter. When you run the script, it is going to run both commands. Now we can go through and reconsider our table structure. This is fine for starting out because we don't have any important data in our database, but once your database is in production you are not going to want to just drop tables. In the last video we discussed different constraints that you can apply to columns in a database table, but how do you actually apply these when you are creating a table? The first way you define constraints is to put them right after the column in your CREATE TABLE statement. CREATE TABLE users( username VARCHAR2(50 CHAR) NOT NULL ) When we define constraints this way, we usually say we are adding column attributes. If you have two constraints you want to add, you just put one after the other with spaces in between. There is no particular order that is required. CREATE TABLE users( username VARCHAR2(50 CHAR) NOT NULL UNIQUE ) In this situation we have already given the column the NOT NULL and UNIQUE attributes, so we should consider making this a primary key: CREATE TABLE users( username VARCHAR2(50 CHAR) PRIMARY KEY ) As you can see, adding column attributes is super easy. We can add a default like this: CREATE TABLE users( username VARCHAR2(50 CHAR) PRIMARY KEY, account_balance NUMBER DEFAULT 0 ) Note that now we need the comma after the first row. There are a few constraints we did not talk go through an example, specifically foreign keys and check constraints. We will be adding these constraints to our database in future videos. In the mean time, I have a thought for you… Many people prefer to name their column constraints. That way, we can refer to certain constraints by name. For example, we might have a primary key constraint that could be named users_pk. The way we are creating these constraints does not allow for this feature, so in the next video we are going to be discussing different ways to create constraints. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Support me on Patreon! www.patreon.com/calebcurry Subscribe to my newsletter: eepurl.com/-8qtH Donate!: bit.ly/DonateCTVM2. ~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~ More content: CalebCurry.com Facebook: www.facebook.com/CalebTheVideoMaker Google+: plus.google.com/+CalebTheVideoMaker2 Twitter: twitter.com/calebCurry Amazing Web Hosting - www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

Leave a comment

Be the first to comment

Collections with this video
Email
Message
×
Embed video on a website or blog
Width
px
Height
px
×
Join Huzzaz
Start collecting all your favorite videos
×
Log in
Join Huzzaz

facebook login
×
Retrieve username and password
Name
Enter your email address to retrieve your username and password
(Check your spam folder if you don't find it in your inbox)

×