I have 2 tables:
table1 with synonyms schema: userA.table1, userB.table1
table2
I have used:
ALTER TABLE table1 RENAME TO table1_old;
ALTER TABLE table2 RENAME TO table1;
It is necessary to recreate synonyms with drop, create, replace SQL syntax or is safe to use as is synonyms and don't recreate it?
No, you don't need to recreate synonyms if the underlying object changes.
They will be temporarily marked as "invalid" until Oracle re-validates them, which will eventually be done automatically - although it's often a good idea to revalidate them manually (in order to check they are still ok).
ALTER SYNONYM usera.table1 COMPILE;
ALTER SYNONYM userb.table1 COMPILE;
Related
I want to alter column within the table to be NULL from NOT NULL.
The problem is that table has one constraint, one trigger and 4 indexes.
Can I somehow alter that column without dropping and recreating everything?
exec sys.sp_depends 'object_name'
List all dependencies and then drop them an recreate everything.
SQL Managements studio offer option with SCRIPT AS CREATE and you can save all views, triggers, tables as backup...
Basically I want to do this:
begin;
lock table a;
alter table a rename to b;
alter table a1 rename to a;
drop table b;
commit;
i.e. gain control and replace my old table while no one has access to it.
Simpler:
BEGIN;
DROP TABLE a;
ALTER TABLE a1 RENAME TO a;
COMMIT;
DROP TABLE acquires an ACCESS EXCLUSIVE lock on the table anyway. An explicit LOCK command is no better. And renaming a dead guy is just a waste of time.
You may want to write-lock the old table while preparing the new, to prevent writes in between. Then you'd issue a lock like this earlier in the process:
LOCK TABLE a IN SHARE MODE;
What happens to concurrent transactions trying to access the table? It's not that simple, read this:
Best way to populate a new column in a large table?
Explains why you may have seen error messages like this:
ERROR: could not open relation with OID 123456
Create SQL-backup, make changes you need directly at the backup.sql file and restore database. I used this trick when have added INHERIT for group of tables (Postgres dbms) to remove inherited fields from subtable.
I would use answer#13, but I agree, it will not inherit the constraints, and drop table might fail
line up the relevant constraints first (like from pg_dump --schema-only,
drop the constraints
do the swap per answer#13
apply the constraints (sql snippets from the schema dump)
I have a Sql Server 2008 database I inherited. A number of apps and SSIS packages work off that database. Not too long ago the scope of the database changed and a lot of new tables were added. As a result of this a lot of the table names (and even the database name itself) no longer make sense, resulting in a very confusing schema.
I could rename the tables straight away and change the apps and processes to use the new names but the chaos and downtime it would cause in the meantime would not be acceptable.
Is there a way I can add an alternate name for a table (like a permanent alias) that I could use to refer to either the new or old table name until all of my refactoring is complete?
Create a synonym first.
CREATE SYNONYM dbo.SensibleName FOR dbo.CrazyName;
Now find all the references to CrazyName in your codebase, and update them to reference SensibleName instead. Once you believe you have found them all, you can eventually run:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION;
DROP SYNONYM dbo.SensibleName;
EXEC sp_rename N'dbo.CrazyName', N'SensibleName', N'OBJECT';
COMMIT TRANSACTION;
If you need to make column names more sensible, you'll have to do so using a view, as synonyms only cover a subset of database-level objects.
Some other info here.
You can rename it with sp_rename and then add synonym:
CREATE SYNONYM OldTableName FOR NewTableName
I've got a question about script in Oracle!
I have a materialized view on a prebuilt table, in my BBDD, (this MV is of the user DAT_OWN, other 2 user (APP & BO) have synonym on this MV)
I have to change the MV and add a column. I know that I need to drop this MV and create a another one, but what's append with the synonym?
I have a previous script like:
DROP SYNONYM APP.STAT_VOZ;
CREATE SYNONYM APP.STAT_VOZ FOR DAT_OWN.STAT_VOZ;
DROP SYNONYM BO.STAT_VOZ;
CREATE SYNONYM BO.STAT_VOZ FOR DAT_OWN.STAT_VOZ;
DROP MATERIALIZED VIEW DAT_OWN.STAT_VOZ;
CREATE MATERIALIZED VIEW DAT_OWN.STAT_VOZ
ON PREBUILT TABLE WITH REDUCED PRECISION
REFRESH COMPLETE
START WITH TO_DATE('21-ene-2013 19:20:00','dd-mon-yyyy hh24:mi:ss')
NEXT (trunc(SYSDATE,'HH')+19/72)
WITH PRIMARY KEY
AS
SELECT TO_CHAR (SUM (COUNT)) AS sum_count,
start_date AS date_hour,
input_type AS input_type
FROM DAT_OWN.another_table
WHERE start_date > TO_CHAR (SYSDATE - 60, 'yyyymmdd')
GROUP BY start_date
Why would somebody do a drop synonym APP and create synonym APP drop synonym BO and create synonym BO BEFORE dropping of the materialized View? In my opinion, I have to do
drop synonym1
drop synonym2
drop Mview
create Mview
create Synonym1
Create Synonym2
I'm sure that the programmer before me did a good job, but I can't understand why they did it that way! Can somebody explain me this, please?
Regards
Your original script was probably just written by an "old school" developer. Most people are accustomed to dropping objects immediately before recreating them. As you've noticed, the sequence is not important.
Most people these days use CREATE OR REPLACE syntax instead of first dropping the object. Here is a good explanation of that concept.
There is probably no need to touch either synonym in the script.
A synonym is just a pointer. There is no requirement that the object pointed to by the synonym exists at all. If you simply drop and re-create the materialized view, the synonyms will automatically point at the newly created materialized view. Of course, after the materialized view is dropped and before it is recreated, if a session tries to use the synonym to query the materialized view, that session will get an error that the object doesn't exist. But, presumably, you're waiting to drop and recreate the materialized view until no one will be using it so that generally isn't a major concern.
I am busy studying MySQL and I understand that update is used to update a record or row in a table. So what does alter do that is so different? Seems like they are the same.
Thanks, any help will be appreciated.
ALTER is a DDL (Data Definition Language) statement. Whereas UPDATE is a DML (Data Manipulation Language) statement. ALTER is used to update the structure of the table (add/remove field/index etc). Whereas UPDATE is used to update data.
The ALTER changes the table in the database, you can add or remove columns, etc. But it does not change data (except in the dropped or added columns of course).
While the UPDATE changes the rows in the table, and leaves the table unchanged.
ALTER is used to change things like table structures or stored procs, otherwise known as DDL statements.
ALTER table MyTable
ADD MyNewColumn VARCHAR(100)
OR
ALTER PROC dbo.MyStoredProc
Alter command is a data definition language
Update command is a data manipulation language
Alter example- table structure, table name, sp, functions
Update example-change database in a row or column etc
Alter command make changes in table structure.
Update command make changes with inside the table
Alter command is used to add, delete modify the attributes of the table in the database
Update command is used to update existing record in a database
Let's see in simple words...
Alter command we use for the modify the structure of the database, table(add, drop, modify)and it falls under DDL.
Update command we use for the modify the rows(records) of the table using where condition and its fall under DML.