Database query UNIQUE constraint - sql

I am new to PL SQL. I would like to fetch data on the basis of some unique attribute. May I use the UNIQUE keyword in WHERE clause? If yes then what would be the exact syntax.
Thanx in advance.

Syntax for using UNIQUE:
SELECT UNIQUE <column_name_list> FROM <table_name> WHERE <conditions>;
However, consider using the keyword DISTINCT for selecting unique values as UNIQUE is not standard across all SQL databases.
SELECT DISTINCT <column_name_list> FROM <table_name> WHERE <conditions>;
See this post for explanation.

To select only the unique values from a certain column, all you have to do is:
SELECT UNIQUE columnName FROM tableName;

Related

Select columnName,* from table

I am a newbie in Oracle SQL, though I have experience in SQL Server.
In SQL Server, to select the rows from a table with a particular column in front:
select columnName,* from tableName
In Oracle:
select columnName,* from tableName
gives error ORA-00936: missing expression, as below:
Please guide.
I can't view images, but here's what I think you need:
select t.column_name, t.*
from table_name t
i.e. you should prefix that particular column name with a table alias ("t"), and then use the same alias with the asterisk ("t.*") to retrieve all table columns.
In Oracle, if you need to view a column but also all columns, you need to define an alias for the table.
Select columnName, A.*
from tableName A;
few things we need to keep it in mind
Alias name in sql - used to derive the individual column name via select query
When you are going to use *[select all] you don't have to worry about the alias name
But when you try to pull all the columns and some specific fields you want to filter then you should go for "Alias"
Alias its object key to refer the inter column
select stu.studentName,stu.* from student stu;

oracle - Unique constraints ORA-00001 vs Case Sensitive Records

I tried to insert into a table with a unique constraints on 2 columns and encountered the unique constraints error.
Select distinct query returns the following records:
Record 1:
ColA:A001 ColB:TV set A001
Record 2:
ColA:A001 ColB:Tv set A001
Does oracle do case sensitive comparison when validating against unique constraints? For instance, in the above scenario, we can see all the values are the same except the ColB (TV verus Tv). Distinct showes they are two different records whereas unique constraints seems to think they are the same?
Can anyone help to clarify?
Thanks!
By default, Oracle is case sensitive. So, TV is not the same as Tv. If the constraint is implemented as:
create unique index table_colb on table(colb)
(or the equivalent in the create table statement), then the two will be different. If you want case insensitivity, then use a functional index:
create unique index table_colb on table(lower(colb))
Oracle is Case Sensitive You may check The DDL SQL Fiddle Demo.
Check the NLS_COMP ans NLS_SORT
select * from NLS_INSTANCE_PARAMETERS where parameter in ('NLS_SORT','NLS_COMP');

Select query to retrieve the value of primary key for a specific row in a table

I am struggling to retrieve the value of primary key for a table. We are using MS SQL Server 2005. The database was designed years back by somebody else (he didn't follow the normalization rules at all). He used Key (which is a keyword in sql server) as the column name for primary key of a table. So I cannot use query like this: select key from table_name where column2 = ?
Could anyone help to write a query to get the value of the primary key for a specific row something like this: select primary_key from tbale_name where column2 = ?
Yes you can, simply wrap column names in backticks:
select `key` from `table_name` where `column2` = ?
Alternatively, depending on your DB, you might use square brackets:
select table_name.[key] from table_name where table_name.[column2] = ?
Edit: I see you said "MS SQL". I think that one works with the square brackets. MySQL accepts the backtick syntax.

Netezza Sql query

I have a list of tables in a Netezza database and I want to get the name of primary key for each of the tables.
Can anyone provide me the query.
You can use this query.
SELECT * FROM _v_relation_keydata;
There is nothing sort of Primary Key thing in Netezza. If you want to look at the NULL or NOT NULL constraints for your required table you can enter the below commands from your nzsql command line
\d [YOURTABLENAME]
SELECT database
, schema
, constraintname
, relation as tablename
, conseq as seq
, attname as columnname, *
FROM _v_relation_keydata
where contype='p'
and schema='ADMIN'
order by relation, conseq
We do not have primary key concept in Netezza. If you are concerned about Not NULL columns following query will help you.
select * from _v_relation_column where NAME='TABLE_NAME' and ATTNOTNULL='Y';
The key(primary/foreign) concepts is not there in Netezza. But we can create primary keys in Netezza and this is created to sync the model with the outside data reporting tools like Informatica/Microstrategy.
You can look into the system view _v_relation_keydata.

Duplicate value in a postgresql table

I'm trying to modify a table inside my PostgreSQL database, but it says there is duplicate! what is the best way to find a duplicate value inside a table? kinda a select query?
Try Like This
SELECT count(column_name), column_name
from table_name
group by column_name having count(column_name) > 1;
If you try to change a value in a column that is part of the PRIMARY KEY or has a UNIQUE constraint and get this error there, then you should be able to find the conflicting row by
SELECT *
FROM your_table
WHERE conflicting_column = conflicting_value;
If conflicting_value is a character type, put it in single quotes (').
EDIT: To find out which columns are affected by the constraint, check this post.
First of all, determine which fields in your table have to be unique. This may be something marked as a Primary Key, a unique index based on one or more fields or a check constraint, again based on one or more fields.
Once you've done that, look at what you're trying to insert and work out whether it busts any of the unique rules.
And yes, SELECT statements will help you determine what's wrong here. Use those to determine whether you are able to commit the row.