ORACLE Database - Why SYS User can drop tables but not columns? - oracle

I am learning PLsql with Oracle 19c. I was wondering - why sys user can drop tables but not specific columns within a table? I found this workaround, where you create a copy of the table for a specific SCHEMA, drop the column in this newly created table, drop the original table, and finally create another copy of the (copied)-table without SCHEMA/USER ROLE.
Is there another, let's say a smoother way, to accomplish this? Or is there any specific reason Oracle implemented it this way?
Thanks.
SQL> SHOW USER;
USER is "SYS"
________________________________________
SQL> ALTER TABLE employees DROP (gender);
ALTER TABLE employees DROP (gender)
*
ERROR at line 1:
ORA-12988: cannot drop column from table owned by SYS
_______________________________________
SQL> DROP TABLE employees;
Table dropped.

"To maintain the integrity of the data dictionary, tables in the SYS schema are manipulated only by the database. They should never be modified by any user or database administrator. You must not create any tables in the SYS schema."
https://docs.oracle.com/database/121/ADMQS/GUID-CF1CD853-AF15-41EC-BC80-61918C73FDB5.htm#ADMQS12003

Try to put in this order:
ALTER TABLE table_name DROP COLUMN column_name;
If the table has data it doesn't work.

Related

ORA-00942: Table or View not exist connecting with another user

in Oracle SQL developer I got error ORA-00942: Table or View not exist connecting with another user when I do the following:
CREATE USER marta IDENTIFIED BY 'marta';
GRANT SELECT, INSERT ON myTable TO marta;
so then, executing:
CONNECT marta/marta;
INSERT INTO myTable VALUES ('1', 'foo', bar');
got the ORA-00942...
Obviusly, If I use system user I can insert row with no issues.
I searched other answers but I couldnt solve this... what is wrong
Obviusly, If I use system user I can insert row with no issues.
Uh-oh. There's nothing obvious about that. The SYSTEM user should not own a table called MY_TABLE (or whatever application table that is actually named). The SYSTEM user is part of the Oracle database, its schema is governed by Oracle and using it for our own application objects is really bad practice.
But it seems you have created a table in that schema and user MARTA can't see it. That's standard. By default users can only see their own objects. They can only see objects in other schemas if the object's owner (or a power user) grants privileges on that object to the other user.
So, as SYSTEM
grant select on my_table to marta;
Then, as MARTA
select * from system.my_table;
To avoid prefixing the owning schema MARTA can create a synonym:
create or replace synonym my_table for system.my_table;
select * from my_table;
But really, you need to stop using SYSTEM for your own tables.

Difference between CREATE TABLE and CREATE ANY TABLE privileges

I don't understand the difference between these two privileges.
I found these two explanations but it's not helping me.
CREATE TABLE -> Enables a user to create a table owned by that user.
CREATE ANY TABLE -> Enables a user to create a table owned by any user in the database.
If a user creates a table it's going to be owned by the user that created it right? I don't get it.
The CREATE TABLE privilege lets you create a table in your own schema. So user scott can do:
create table scott.mytable ( id number );
The CREATE ANY TABLE privilege lets you create a table in any schema in the database. So again, user scott can do:
create table hr.employees ( id number );
That is, make a table that belongs to someone else.

Oracle 12c - drop table and all associated partitions

I created table t1 in Oracle 12c.
Table has data and it is partitioned on list partition and also has subpartitions.
Now I want to delete whole table and all associated partitions (and subpartitions).
Is this the right command to delete all?
DROP TABLE t1 PURGE;
The syntax is right but not preferable,
just drop without purge so that whenever you need you could have it back, if your flashback option is enabled. If your database's flashback option is in charge, you could issue this command (provided you don't use purge):
SQL> DROP TABLE T1;
SQL> FLASHBACK TABLE T1 TO BEFORE DROP RENAME TO T1_ver_2;
When you run DROP then the table is removed entirely from database, i.e. the table does not exist anymore.
If you just want to remove all data from that table run
truncate table T1 drop storage;
You can also truncate single (sub-)partition if required.

Can we alter the All_objects table with the Sys user?

can we alter the all_objects table with the sys user.
can i alter it using the alter command.
or is this table read only?
I was looking into the tables and tried to alter it,but for some reason
im not able to do it.Is it coz it can only be read.
ALL_OBJECTS is a view it is based primarily on internal table SYS.OBJ$.
NEVER alter in any way the internal tables or views of the database dictionnary.
What are you trying to achieve by altering this view?

Reference a table in other schema omiting schema name

If I have a table sch1.tab1 is it possible to call it from schema/user sch2 just with
select * from tab1 (assume that we have all the privilegies)?
I am aware that in postgresql you can set the search path where db would look for tables which enables you to omit the schema when you are referencing a table but I do not know if this exists in oracle.
Thank you.
You can create a synonym, but you'd have to make one for each table you wanted to access; from sch2:
create synonym tab1 for sch1.tab1;
A more general method is to switch your current schema:
alter session set current_schema = 'SCH1';
You're still connected with your original user account and only have those privileges still, but you don't have to qualify objects in that schema any more. But now you would have to qualify any of your own tables (back in sch2), if you have objects in both schemas.

Resources