Kamran Agayev's Oracle Blog

Oracle Certified Master

Archive for December 6th, 2008

How delete duplicate rows

Posted by Kamran Agayev A. on 6th December 2008

Today I want to give you an example of how delete duplicate rows from a table
Let’s create new table with name students:
CREATE TABLE students (ID NUMBER, NAME VARCHAR2(10), surname VARCHAR2(10))
Now let’s add some duplicate rows to it:
INSERT INTO students VALUES (1, ‘Kamran’, ‘Agayev’);
INSERT INTO students VALUES (2, ‘Elcin’, ‘Mammedov’);
INSERT INTO students VALUES (1, ‘Kamran’, ‘Agayev’);
INSERT INTO students VALUES (2, ‘Elcin’, ‘Mammedov’);
INSERT INTO students VALUES (1, ‘Kamran’, ‘Agayev’);
INSERT INTO students VALUES (2, ‘Elcin’, ‘Mammedov’);
And now let’s look to the data of our table:
SELECT * FROM students
1  Kamran  Agayev
2  Elcin   Mammedov
1  Kamran  Agayev
2  Elcin   Mammedov
1  Kamran  Agayev
2  Elcin   Mammedov
Now let’s delete duplicate rows using this script:
 
DELETE FROM students   
WHERE ROWID NOT IN (SELECT MAX (ROWID) FROM students GROUP BY ID) 
And now let’s look to students table
SELECT * FROM students
1  Kamran  Agayev
2  Elcin   Mammedov

Posted in DBA scripts | 8 Comments »

Run SQL statement from one Linux command

Posted by Kamran Agayev A. on 6th December 2008

These two examples shows you how to run SQL command directly from Linux in a single command

[oracle@localhost~] echo “select count(*) from tab;” | sqlplus -s username/pass
With another method, you can run from one Linux command a SQL file

 [oracle@localhost~] sqlplus -s username/pass <filename.sql

 COUNT(*)

————-

                20

 [oracle@localhost~]

Posted in DBA scripts | No Comments »

What's View and Synonym?

Posted by Kamran Agayev A. on 6th December 2008

What is a view?

A view is a virtual table. Every view has a Query attached to it. (The Query is a SELECT statement that identifies the columns and rows of the table(s) the view uses.)

A view is based on a table or another view and acts as a window through which data on tables can be viewed or changed. A view does not contain data. The definition of the view is stored in the data dictionary. You can see definition of view in user_view data dictionary table.
A view is a logical representation of another table or combination of tables. A view derives its data from the tables on which it is based. These tables are called base tables.

What is a synonym and what types it has?

A synonym is an alias for a schema object. Synonyms can provide a level of security by masking the name and owner of an object and by providing location transparency for remote objects of a distributed database. Also, they are convenient to use and reduce the complexity of SQL statements for database users.

Synonyms allow underlying objects to be renamed or moved, where only the synonym needs to be redefined and applications based on the synonym continue to function without modification.

You can create both public and private synonyms. A public synonym is owned by the special user group named PUBLIC and is accessible to every user in a database. A private synonym is contained in the schema of a specific user and available only to the user and the user’s grantees.

Posted in Interview questions | No Comments »