SQL




SQL definition:

SQL (Structured Query Language) is a set of commands used to perform operations on databases.

SQL example:

Interpretation of SQL commands depends on the database system where they are performed.

Let’s take a example shop database:




One to many relation

Basic SQL commands:




  • getting data from a database entity
    • select (list of data that we need) from (list of databases from which we need data)

Code:

select PRODUCT_NAME, ID_SELLER from PRODUCTS;

will return following data:

SQL

  • getting data from a database entity with conditions
    •  select (list of data that we need) from (list of databases from which we need data) where (list of conditions)

Code:

select PRODUCT_NAME, ID_SELLER from PRODUCTS where ID_SELLER = ‘S15’;

will return following data:

SQL

  • getting and joining data from 2 database entities with conditions
    •  select (list of data that we need) from (list of databases from which we need data) where (list of conditions including condition which tells what is the common join data for both tables)

Code:

select pr.PRODUCT_NAME, se.SELLER_NAME from PRODUCTS pr, SELLERS se where pr.ID_SELLER=pr.ID_SELLER and pr.ID_SELLER =  ‘S15’;

will return following data:

SQL

  • inserting data in a database entity
    • insert into (list of places that we need to insert data) values (list of values that we want to insert)

Code:

insert into  SELLERS (ID_SELLER, SELLER_NAME) values ( ‘S16’, ‘Kate Jones’);

will insert new data in the Sellers database entity:

pic33

  • deleting data in a database entity
    • delete (name of a database entity where we want to delete data) where (list of conditions)

Code:

delete  SELLERS  where ID_SELLER= ‘S16’;

will remove row from Sellers database entity with ID_SELLER equal to S16:

One to many relation

  • updating data in a database entity
    • update (name of a database entity where we want to update data) set (list of data that we want to update) where (list of conditions)

Code:

update  SELLERS  set SELLER_NAME = ‘Smith’ where ID_SELLER= ‘S14’;

will return following data:

SQL

Copyright © 2015-2016 • All Rights Reserved • database-definition.com

Leave a Reply

twenty − nineteen =