Altering structure of table and dropping table

14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.35
List Price:  €96.21
You save:  €28.86
£55.83
List Price:  £79.77
You save:  £23.93
CA$100.76
List Price:  CA$143.95
You save:  CA$43.19
A$112.23
List Price:  A$160.33
You save:  A$48.10
S$95.14
List Price:  S$135.93
You save:  S$40.78
HK$543.62
List Price:  HK$776.63
You save:  HK$233.01
CHF 63.04
List Price:  CHF 90.06
You save:  CHF 27.02
NOK kr797.48
List Price:  NOK kr1,139.30
You save:  NOK kr341.82
DKK kr502.49
List Price:  DKK kr717.87
You save:  DKK kr215.38
NZ$124.10
List Price:  NZ$177.29
You save:  NZ$53.19
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,361.55
List Price:  ৳11,945.58
You save:  ৳3,584.03
₹5,962.79
List Price:  ₹8,518.63
You save:  ₹2,555.84
RM314.04
List Price:  RM448.65
You save:  RM134.61
₦108,335.42
List Price:  ₦154,771.52
You save:  ₦46,436.10
₨19,495.71
List Price:  ₨27,852.21
You save:  ₨8,356.49
฿2,390.81
List Price:  ฿3,415.60
You save:  ฿1,024.78
₺2,467.44
List Price:  ₺3,525.07
You save:  ₺1,057.62
B$434.14
List Price:  B$620.23
You save:  B$186.09
R1,296.68
List Price:  R1,852.48
You save:  R555.80
Лв131.68
List Price:  Лв188.12
You save:  Лв56.44
₩102,198.93
List Price:  ₩146,004.73
You save:  ₩43,805.79
₪256.56
List Price:  ₪366.53
You save:  ₪109.97
₱4,090.81
List Price:  ₱5,844.26
You save:  ₱1,753.45
¥10,994.99
List Price:  ¥15,707.80
You save:  ¥4,712.81
MX$1,408.98
List Price:  MX$2,012.91
You save:  MX$603.93
QR255.09
List Price:  QR364.43
You save:  QR109.34
P967.82
List Price:  P1,382.66
You save:  P414.84
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,562.49
List Price:  E£5,089.49
You save:  E£1,527
ብር8,907.19
List Price:  ብር12,725.10
You save:  ብር3,817.91
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$69,323.69
List Price:  CLP$99,038.09
You save:  CLP$29,714.40
CN¥510.80
List Price:  CN¥729.75
You save:  CN¥218.94
RD$4,245.36
List Price:  RD$6,065.06
You save:  RD$1,819.70
DA9,465.07
List Price:  DA13,522.10
You save:  DA4,057.03
FJ$162.39
List Price:  FJ$232
You save:  FJ$69.60
Q539.11
List Price:  Q770.19
You save:  Q231.08
GY$14,638.77
List Price:  GY$20,913.42
You save:  GY$6,274.65
ISK kr9,772.70
List Price:  ISK kr13,961.60
You save:  ISK kr4,188.90
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,291.33
List Price:  L1,844.84
You save:  L553.50
ден4,143.97
List Price:  ден5,920.21
You save:  ден1,776.24
MOP$559.97
List Price:  MOP$800
You save:  MOP$240.02
N$1,289.45
List Price:  N$1,842.15
You save:  N$552.70
C$2,574.80
List Price:  C$3,678.44
You save:  C$1,103.64
रु9,534.71
List Price:  रु13,621.60
You save:  रु4,086.89
S/260.99
List Price:  S/372.86
You save:  S/111.86
K283.85
List Price:  K405.52
You save:  K121.66
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,936.45
List Price:  ZK2,766.47
You save:  ZK830.02
L335.12
List Price:  L478.77
You save:  L143.64
Kč1,694.03
List Price:  Kč2,420.15
You save:  Kč726.12
Ft27,771.44
List Price:  Ft39,675.18
You save:  Ft11,903.74
SEK kr774.38
List Price:  SEK kr1,106.30
You save:  SEK kr331.92
ARS$71,792.58
List Price:  ARS$102,565.23
You save:  ARS$30,772.64
Bs484.55
List Price:  Bs692.24
You save:  Bs207.69
COP$307,165.41
List Price:  COP$438,826.54
You save:  COP$131,661.12
₡35,530.54
List Price:  ₡50,760.09
You save:  ₡15,229.55
L1,777.33
List Price:  L2,539.15
You save:  L761.82
₲546,250
List Price:  ₲780,390.60
You save:  ₲234,140.59
$U3,115.91
List Price:  $U4,451.49
You save:  $U1,335.58
zł287.82
List Price:  zł411.19
You save:  zł123.37
Already have an account? Log In

Transcript

Hi, welcome back. In this lesson, we understand how to alter the structure of a table. In the previous lesson, we created tables. We took all important decisions, but we live in a dynamic world where you may have to change the structure of the table because of some compelling reasons. So what if you want to add a new column to an existing table? Or what if you want to delete an existing column?

What if you want to increase the width of a column and there are many such requirements? Now, we will see how to deal with all that in this particular lesson. But before that, I want you to understand there is something called data dictionary. data dictionary is part of your database. It is a collection of tables. And in those tables or achill, stores information about the database, like what tables you have, what columns you have, and what constraints you have for your columns and tables.

And a lot of information about database is part of database itself. That means the data dictionary stores the data about database. It's also known as method data, data about the data. And the best part of it is Oracle takes care of data dictionary on its own. You never need to modify anything about the data dictionary. It's called active data dictionary.

So the DBMS is automatically updating data dictionary whenever it is needed, then can we access this Data Dictionary. Yes, you can access data dictionary by using some views. Well, don't worry if the term view is not known to you. We will get into all the details later in this course. But a view is very much like a table. And it is created so that you get access to an existing table.

And you get only what you want from the table. So data dictionary views are created by Oracle so that users can access data that is relevant to them. Any view that starts with the user is containing data related to user. Now, let's see a few things. So here we are. And I want to know what are the tables I have, so I can just give something like Select star from tab.

It's a kind of data dictionary and it is providing information about all the tables you have, don't worry about those tables. They are the tables representing the deleted tables, don't worry, for now, these are the tables I have. And you can see the students and courses which we created in previous lesson. Then we can also get more information about for example constraints. So we can say user constraints and that is another data dictionary view. It provides you access to data dictionary and these are user constraints.

So the constraints are having some meaningful names and the constraints are of different types, like some of them are check constraints, some of them are foreign keys, and all that. So these are the constraints we have. Especially if you look at these constraints. They belong to Do our tables which we created in the previous lesson, like courses table and students table. But what if you want to get constraints only for a particular table? Yes, we can.

There you can see a column called table name. So where you say table name is equal to, I'm interested in constraints related to students. So I give this let's try. Well, it is going to give me nothing. Wow. We do have some constraints and we have seen them already.

Well, the reason why we don't get anything here is there is no table with the names students. I mean, there is no table with the name, lower case students, because when the data is stored in data dictionary, the object names are always in uppercase. So don't forget that you must always Ways give it in uppercase, and then it works. So now you get all the constraints related to students table, or any other table for that matter, for example, jobs table. And we have only two constraints for jobs table, and so on. So coming back.

So that's about the user constraints. And don't forget to give it in uppercase, because object names are always stored in uppercase in data dictionary. And then this is the command we use to alter the structure of the table. ALTER TABLE is what we use to add new columns to existing columns and modify some characteristics of existing column and dropping a column. Let us see how to deal with them. So this is the command and this is the syntax, like add modified drop You can also enable and disable constraints.

So for the time being, you can disable a constraint so that it is inactive. Later, you can enable it, or you can even drop it. We'll see. So here are some of the examples. To start to wait. I would like to add a new column to courses table.

It's called description and it is of type where character to and this is the way you add a new column to courses table. So this will add third column to courses table and this is going to be the type of that column and then you can modify you felt the name of the course is to be 40 characters, not 30. So you can increase the width of the column anytime you want. But if you want to decrease the width of the column Maybe you're trying to decrease it from 30 to 20. Well, that is possible provided the data in the column is not truncated. So if all the values in that column have less than 20 characters, then you can decrease it to 20.

Otherwise, you're not allowed to do that, because that would result in loss of data, then we can drop a constraint. If I don't want a constraint anymore, I can say drop the constraint. And this is the constraint name. So give a meaningful name to constraint so that you can easily drop the constraint or refer to constraint by its name. Even when we tried to enable disable. We give the constraint name.

So remember the name. Of course, you can always get it from user constraints. The table which I'm talking about right, and then this is about dropping a column, you can drop a column all together. But this is generally very lengthy process. So you are strongly recommended to do this only when the system is relatively free. So don't drop columns when system is busy, because it could really take a lot of time.

So this is about how you can drop a column a constraint, or add a new column to the existing table, and then modify an existing column. You can do a lot of things but there are ifs and buts. So in the beginning, I don't recommend you worry too much about other possibilities. But anyway, For more information, please refer to alter table command in Oracle documentation. And it is the largest command you have Hell a lot of stuff with ALTER TABLE. But to know all those options, you need to know a lot about Oracle.

But as a beginner, I don't recommend you worry too much but do understand these commands. Well, that's the way we alter the structure of the table. And now this is about dropping a constraint, you cannot drop a primary key or a unique constraint if there is any dependent foreign key constraint. So the remedy for that is use cascade constraints, which is an option at the time of dropping the constraint. So what you need to do is, as you can see in the syntax, while you're dropping the constraint, you can give cascade constraints. So what it does, look at this diagram in this the students take Trouble is referencing courses table.

So course is the foreign key that is pointing to the course code. Now what if you're trying to remove the primary key constraint from this course code. Once you remove this primary key constraint, the column can be duplicated, it is no longer unique. But the rule for foreign key says foreign key can refer only to a column, which is unique. So either eight must be primary key or eight must be unique. Those are the options you have.

But when you're removing a primary key or unique constraint from the parent column, it cannot be any longer parent for foreign key. So in such cases, you have to tell a racquel to remove the foreign key constraint event from the dependent columns. So, cascade constraints is saying delete all dependent constraints on the column that is right now being made non unique or non primary key, I mean, if you delete primary key or unique constraint from the code, then I want Oracle to go to students remove foreign key constraint from the course column of students table. So, this is mandatory without you giving cascade constraints or racquel cannot delete the primary key or unique constraint of parent table. Well, the same you have to give even in some other commands. drop table is the next command where we can drop the table all together.

It is going to remove the whole table, the data, the structure, everything is good. Again, if you have any dependent tables with constraints referring to the columns in the table being dropped, you have to give cascade constraints only then the constraints will be gone in those dependent tables and the parent table can be dropped. This purge is saying, Do not take the table to recycle bin. So if you're not using this purge option, or racquel is going to put this table in Recycle Bin and it allows you to restart the table using a command called flash you will be able to restore the table at a later stage. Well, if you give purge, then Oracle will not take the table into the recycle bin. So you will not have an option to get the table back at a later stage.

So If you are serious about dropping the table without ever getting it back, you don't want this to occupy any space in the database do give purge, otherwise, leave the option open. So this is about drop table and alter table. Those are the measure commands discussed in this lesson. So we talked about what is data dictionary, how we can use some of the data dictionary views like user constraints, we do have lot more data dictionary views to provide details about the database. Then ALTER TABLE is used to alter the structure of the table, like adding new columns, dropping existing columns, modifying some characteristics of existing columns. cascade constraint is used to remove all the dependent foreign key constraints.

When you are deleting a constraint, like a unique or primary key, or it is also an option we use in the context of DROP TABLE command drop table is to drop the table is to get rid of everything about the table. But you may want to restore the table if you like. In that case just gave DROP TABLE without purge. But if you give purge option, the table is dropped and you can never restore it. Well, that's about how we can alter the structure of a table or drop a table. We will learn more about how to deal with the various other concepts related to data retrieval in the coming lessons.

See you in the next lesson.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.