The ability to generate a query based on the condition of changing data that is located in another table.
It can query a table restricting the rows returned on the basis of another queried table.
WHERE EXISTS
and WHERE NOT EXISTS
WHERE IN
and WHERE NOT IN
select * from nombres where exists ( select empleados from asegurados ); select * from names where exists (select employees from insured); select numero_usuario,email from usuarios where exists ( select 1 from clasificados where clasificados.email= usuarios.email); select name,email from users where exists (select 1 from classifieds where classifieds.email = users.email);
Note that Postgres supports the same "aliasing/shortcut" mechanism used in Oracle, using order number from the original query.(Sql compliance with Oracle)
select miembro from parientes where nombres in ( select * from parientes_lejanos where appelido like '%R'); select member from relatives where name in (select * from foreign_relatives where lastname like '%R');
Testing if behaviour is compliant with Oracle (See how Oracle executes this )
select barcos_veloces,propietarios,costo from barcos where propietarios in (select 2 from usuarios where valor_neto > 10000000 ); select fast_boats,owners,costs from boats where owners in (select 2 from users where net_worth > 10000000 );
insert into parientes(miembro,nombre) values (12,'Alexia'); insert into relatives(member,name) values(12,'Alexia');
update parientes set apellido='Rubio' where miembro=12; update relatives set lastname='Rubio' where member=12;
delete parientes where miembro=12;
INDEX, SEQUENCE, TABLE ,VIEW...
create table alumnos ( nombre varchar2(25), apellido varchar2(25), matricula integer primary key, salon number ); create tables pupils ( name varchar2(25), lastname varchar2(25), id integers primary key, classroom number);
There are various Datatypes which you can use in table definitions, however you are very likely to use a few extensively, these may be:
char(n)
& varchar(n)
(Where (n)
is an upper limit) decimal(n)
& int4
(Where (n)
is a precision of up to ~8000 digits; and int4
a number between -2147483648 to +2147483647)timestamp,date,time
& interval
( Where timestamp
is year-to-microsecond precision, date
is year-to-date precision, time
is only time of day, and interval
is used for time intervals.A constraint is used to implement certain rules on columns in the table that will be created, these are normally business or operational rule.
A constraint is used to implement certain rules on columns in the table that will be created, these are normally business or operational rule.
not null
: Requires a value for this column. unique
: Two rows cant have the same value in this column ( side effect, Oracle creates an index on this column ) primary key
:Same as unique
except that no row can have a null value, also it is possible to refer from other tables to this key. check
: Limit the range of the column. Example: rango integer check ( rango > 6 and rango <= 9)
references
: This column can only contain values present in another tables primary key . Example:usuario not null references empleado
Constraints can also apply to multiple columns:
create table los_mejores ( identificacion CHAR(5) CONSTRAINT thisid PRIMARY KEY, nombre_titulo varchar(45) NOT NULL, edad date, unique(edad,identificacion) ); create table los_mejores ( identificacion PRIMARY KEY DEFAULT('user_id'), nombre_titulo varchar(45), edad integer, check (nombre_titulo <> '') );
When a certain query is used, it is often beneficial that instead of typing the long query every time, a view be created, the other reason a View might be created is Privacy, perhaps the developer should only have access to certain parts of the table, thus he is only granted permissions to the view. The actual query takes place in the same way as would a normal SQL query.
Sequences are used to provide an order number list, they are normally used for primary keys in tables.
To create a sequence the create sequence
command is used, its functionality is very similar to Oracle's, see following examples:
CREATE SEQUENCE user_id START 1000; INSERT INTO users VALUES(NEXTVAL('user_id'),'Gabriel Lopez');
The first command creates a sequence called user_id
which starts at 1000
, the second line inserts data using the sequence; Postgres uses a similar way of calling sequence values, however the notation is inverted, in Oracle user_id.NEXTVAL
in Postgres the NEXTVAL function is decalred and then the sequence_name
.
Other options to the create sequence
are: CYCLE, MINVALUE,MAXVALUE, INCREMENT
which indicate respectively : In case the MAXVALUE is reached reCYCLE the sequence starting at MINVALUE; the minimum value a sequence can have defaults to 1 (limit:-2147483647); the maximum value a sequnece can have, defaults to 2147483647; and INCREMENT which defaults to 1.
Another option is CACHE
this option defaults to 1, what this function does is preallocate a certain number of sequence values into memory, allowing faster access. While this may allow for faster sequence access, it is possible to lose sequence numbers if a session is terminated, a fact you may be familiar with if you use sequences in Oracle,since this value also defaults to 5.
Every user that has access to a database is defined in a table called pg_shadow
, to add a new user to the pg_shadow table the command createuser is used, this command can be issued directly from the shell if the user has the appropriate privileges; another possbilitiy to add a user to the pg_shadow
table is with the create user in psql
, this allows for finer settings for the user like: password expiration, specific userid and group membership.Examples:
CREATE USER daniel WITH PASSWORD 'd43SfgStAda' CREATEDB; CREATE USER developer WITH PASSWORD 'eey245VCrwr' VALID UNTIL 'Sep 19 2002';
The first user is created with database creation permission, while the second user is assigned a limited time access to the database.
Postgres defines groups
to ease administration privileges on a database, the table name pg_group
contains the relevant information, in order to add a new group the CREATE GROUP
command is used, some examples of this command:
CREATE GROUP developers; CREATE GROUP dbas WITH USER daniel, julio, roberto;
The first line simply creates a group, while the second line includes specific users defined in the database (those defined in pg_user).
Postgres like Oracle allows privileges to be assigned on an object|privilege|(user|group)
basis.
See the following examples