Creating a table in Postgre SQL

To Create table in postgresql :
Create table company(
 emp_name char(20) not null;

 emp_id int constraints any_name_for_constraint primary key,

 emp_age int check (emp_age > 18 and emp_age<50),
 --or emp_age int check (emp_age between 18 and 50),

 emp_present_salary int,
 emp_expectation_salary int check(emp_expectation_salary-   emp_present_salary < 1000),
)
>>select * from company where emp_age between 30 and 40;
>>select * from company where emp_age in (30,40);
>>select * from company where emp_age in (30,40);
>>select * from company where emp_age not in (30,40);
>>select * from company where emp_age in (30,40);
>>select * from company where emp_age%20 = 0;
>>select * from company where emp_name like 'a%'
>>select * from company where emp_name like '%a%'
>>select * from company where emp_name not like '%a'
>>>>select * from company order by emp_id;
----edit
>>select distinct name from company order by name limit 10;
>>select surname from cd.members union select name from cd.facilities;
>>select max(joindate) as latest from cd.members;
>>select firstname,surname,joindate from cd.members where joindate =(select max(joindate) from cd.members);
>>select starttime from cd.bookings where memid = (select memid from cd.members where firstname= 'David' and surname='Farrell');
>>select jointab.starttime from cd.bookings jointab inner join cd.members mems on mems.memid = jointab.memid where mems.firstname = 'David' and mems.surname = 'Farrell';
>>select bks.starttime as start,fac.name as name
from
cd.facilities fac inner join cd.bookings bks on fac.facid = bks.facid
where
fac.facid in(0,1) and
bks.starttime >='2012-09-21' and
bks.starttime < '2012-09-22'
>>



To create a dummy table to test foreign key :
Create table emp_add(
 emp_id int constraint rf references company(emp_id);
 --this will make sure onlu id present in company is entertained.
 emp_add text;
)

--inserting any entry with id not matching the id in foreign table company will give error.

Comments

Popular Posts