Netezza Sql query - sql

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.

Related

Create a new temporary local table from the result set data from a SELECT statement

SELECT o_id,first_name,last_name
INTO newoperator
FROM operators
WHERE o_id >5 GROUP BY first_name
syntax error at or near "INTO"
Can anyone help
The operation you are trying to accomplish is creating a table based on another table. The generic term for this in SQL is CTAS -- which stands for CREATE TABLE AS.
Not all databases support this syntax. For instance, SQL Server uses the SELECT INTO syntax.
That is because that is the syntax most commonly used for this across databases:
CREATE TABLE newoperator AS
SELECT DISTINCT o_id, first_name, last_name
FROM operators
WHERE o_id > 5;
Note that I fixed the query so it has some hope of working. In your version, last_name and o_id were not in the GROUP BY, which would generate an error (in almost any database).

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;

Database query UNIQUE constraint

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;

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.

how to convert result of an select sql query into a new table in ms access

how to convert result of an select sql query into a new table in msaccess ?
You can use sub queries
SELECT a,b,c INTO NewTable
FROM (SELECT a,b,c
FROM TheTable
WHERE a Is Null)
Like so:
SELECT *
INTO NewTable
FROM OldTable
First, create a table with the required keys, constraints, domain checking, references, etc. Then use an INSERT INTO..SELECT construct to populate it.
Do not be tempted by SELECT..INTO..FROM constructs. The resulting table will have no keys, therefore will not actually be a table at all. Better to start with a proper table then add the data e.g. it will be easier to trap bad data.
For an example of how things can go wrong with an SELECT..INTO clause: it can result in a column that includes the NULL value and while after the event you can change the column to NOT NULL the engine will not replace the NULLs, therefore you will end up with a NOT NULL column containing NULLs!
Also consider creating a 'viewed' table e.g. using CREATE VIEW SQL DDL rather than a base table.
If you want to do it through the user interface, you can also:
A) Create and test the select query. Save it.
B) Create a make table query. When asked what tables to show, select the query tab and your saved query.
C) Tell it the name of the table you want to create.
D) Go make coffee (depending on taste and size of table)
Select *
Into newtable
From somequery