Sei sulla pagina 1di 14

Basic SQL TrainingBasic HistoryDatabases were originally set up as flat files.

These flat files were very similar to


excel documents, where the same data would be repeated over and over again. In
1970, EF Cod deemed this inefficient and decided to create a relational database
system that would create relationships between data in the database.- such as peoples
names, or departments etc.
Relational-> relationships using one piece of data to link it to many tables( this saves
on space and duplication)
Charles River has over 200 tables, each one of our clients has these tables, however,
the data that is in these tables is different, ie different securities etc.
SQL is the language that accesses this data.
The Commands
There are 4 basic commands that need to be understood.
SELECT
INSERT
UPDATE
DELETE

- Retrieves data from the tables


- Puts data into the tables rows, or a new row
- Modifies one or more columns
- Deletes a row or column in the table.

The SELECT function is a safe function that will not allow you to screw up the
database (or DB)

Use Microsoft SQL Enterprise, Query analyser.

This program is often just under your programs, but may be hidden, as is above, under
another directory. Log in, selecting the server required.

If a blank screen comes up, press F8, which will bring up the below screen.

The top left hand highlighted shows the server the database is situated on. The top
highlighted box shows the database that is being looked at, and the bottom box shows
the expandable
Using v723, expand the left hand pane option- ( v723- User tables)

To execute the
statement, hit this button

The user can now see what tables are available, and by expanding the next level down
will show the columns that are in the table.
The basic form of a select function are:
SELECT
FROM
WHERE
The use of the star or *, will bring everything back in that table.
eg, select * from cs_broker, however, structure the query as above, putting the new
query in the next line.
Select *
From cs_broker

The results from this query are then given in the bottom right box, with all columns
and all rows. Also note that there are a total 30 rows.

Try another statement


Select *
From ts_order
This will return all the columns, and rows in the table ts_order.

Command box

Results window

By expanding the tables on the left box, the user can ascertain the columns in each
table.

If the user only required several of the columns that were in the table rather that all of
them, this can be done by specifying each column required. For instance, the user
only requires, order_id, status, security_id , all from the ts_order table.
The statement to retrieve only these would be:
Select order_id, sec_id, status
From ts_order

The columns are returned in no particular order.

To take the statement further, the user may only want to see the above statement with
securities that have a READY status.
Select order_id, sec_id, status
From ts_order
Where status = READY
The use of the where statement is now used to define the columns status. The status
must be within single quotes.

NB- the information within the quotes is note case sensitive, however, for oracle it is.

Joining
Joining tables is required when some columns do not show all of the data is required
for the query that is required.
Perform the following statement:
Select order_id, trans_type, sec_id, status
From ts_order
Where status = READY

Now select the following table;


Select csm_security

Notice that when the query has been run, and that there are two queries in the top box,
there two box appear below. This is sometime useful, however, to receive only one
box, highlight the required statement, and then run.

If the user requires data from two different tables, aliases are required to associate the
two tables. This basically means, using the common data between the two tables, a
relational query is then created.
Select *
From csm_security
Sec_id is a common denominator for these two tables, therefore, it is possible to link
these two tables, using this common factor.

Using the above information, it is then possible to associate the csm_security table to
the ts_order table, by using association.
Associate sec_id(in the ts_order table) to sec_name ( in the csm_security table)
By adding the csm_security to the from statement will create an alias.

Select o.order_id, o.trans_type, o_sec_id, o.status, sec.sec_name


From ts_order o, csm_security sec
Where o.status = ready
And o.sec_id = sec.sec_id
The from statement creates the associates by adding the o, at the end of each table
where the data should be taken from the ts_order table, and the sec, at the end of the
csm_security table.

This statement has now brought back all the securities that are in the csm_security
table, and ts_order table that have a status of ready, returning their name, and the
trans_type, and order_id, using the order_id as the common denominator.

Important Tables to be aware of:


Ts_order_alloc
cs_position
cs_fund
csm_security

(Contains, order_id, account_id, target_quantity)


( account, sec_id)
(fund number)
( sec_name, currency)

Try this:
Run a query to find all orders in the ready status
Pick one of the orders- find the allocations for that one order.
Now show the account name for each of those allocations
Basic Examples
select*
fromts_order
wheremanager='XBBJNW9'
and
trans_type='SELLL'
and
order_acct_cd='GEMZ'
and
trade_datebetweento_date('01/01/2009','dd/mm/yyyy')andto_date
('29/12/2009','dd/mm/yyyy')

select*
fromcsm_security
wheresec_typ_cd='CURR'

Potrebbero piacerti anche