Skip to main content

Posts

Showing posts with the label rownum

SQL insert query validation constraints to limit number of records

This is sample of query for  SQL insert query validation constraints to limit number of records lets you want to insert only 1000 records in table . We can restrict number of rows, but need to add a column on which we can add some constraint ... check below example.! CREATE TABLE TAB_1000_ROWS AS SELECT ROWNUM COL1 FROM DUAL CONNECT BY ROWNUM <1001; ALTER TABLE TAB_1000_ROWS ADD CONSTRAINT CHECK_CONST CHECK (COL1 BETWEEN 1 AND 1000); ALTER TABLE TAB_1000_ROWS MODIFY COL1 NUMBER NOT NULL; ALTER TABLE TAB_1000_ROWS ADD CONSTRAINT UNIQUE_COL1 UNIQUE (COL1); -- or we can also use sequence with max as 100 and no-cycle! after insertion of 100 values it wont allow any new values to get inserted! will throw check or unique constraint! 

Difference in length between the first biggest and the second biggest name in sql table

to get difference in length between the first biggest and the second biggest name in SQL table like Employees table. You can try following query to get the same. 1. SELECT MAX(DECODE(DR, 1, L, NULL)) - MAX(DECODE(DR, 2, L, NULL)) AS DIFF FROM ( SELECT LENGTH(FIRST_NAME||LAST_NAME) AS L, DENSE_RANK() OVER(ORDER BY LENGTH(FIRST_NAME||LAST_NAME) DESC) AS DR FROM EMPLOYEES ) WHERE DR IN (1, 2) 2.select max(a) - min(a)from ( select distinct(length(FIRST_NAME)) a from EMPLOYEES order by length(FIRST_NAME) desc) where rownum < 3

Difference between alias and synonym in sql

Difference between  alias and synonym in sql Both are different in all manner not only syntactically Synonym is 1. It is a database object 2. It may be Public or private 3. It is permanent until dropped 4. create [public] synonyms syn_name for [Existed database object]; like create synonym emp1  for hr.employees; Alias is 1. It is not a database object. 2. It will alway private to query 3. It temporary to that query.It to used to provide a name It is generally used to provide a meaning-full heading or column name or table name that is long or inconvenient to type in Query 4. take a example select rownum as rank,e.* from emp1 e; here rank and e is alias where as emp1 is synonym