Monday, August 13, 2012

How to get a form name from the twig-template in Symfony2?


It's pretty easy.
{{ form.vars.name }}

Friday, August 3, 2012

Relational Algebra Operations

How important is it to understand the relational algebra?

In the first place, the relational algebra is a part of computer science and so it should be useful thing ;)

Secondly, although the relational algebra is a formal model, it's the basis for database query languages. Widely used SQL is based on it. The study of the relational algebra is supposed to make the comprehension of SQL easier.

Foundations

First of all the relational algebra is a procedural query language. The word "procedural" means that the language consists of operations. The operations are performed on relations (tables or parts of them).

Relational Algebra Operations:

Wednesday, August 1, 2012

Using CASE in UPDATE statements

I believe you know the general way to write update statements. But let's revise and take a look at Figure 1.

If we want to update salary of the persons with salary over $50000, the query will look like this:

UPDATE Persons
SET salary = salary*2
WHERE salary > 50000;

We have just doubled salary for these persons!

Let's see a more interesting example. Suppose we want to increase salary for persons with salary over $60000 by 3 percent, whereas all others receive a 5% raise. We want to do that by executing only one(!) query.

Try to write the query by yourself before you look at the solution.

We can do it using case statement:

Right use of GROUP BY in SQL-queries

This article is about the right use of grouping in SQL-queries. I wrote the article, because this subject is very important. More than that it is not too much complicated and suits to the format of short useful articles, that my blog is for.

Job interviews like questions concerning grouping (using GROUP BY in SQL-queries). If you don't use SQL-queries every day or don't know them well, refresh your memory before your interview.

Let's take an example. Figure 1 shows a table Persons, which stores information about persons. The table has four column headers: ID, name, dept_name, salary.

First of all we use GROUP BY with aggregation.

Aggregate functions are functions that take a collection of values as input and return a single value. SQL offers five built-in aggregate functions:
  • Average: avg
  • Minimum: min
  • Maximum: max
  • Total: sum
  • Count: count

Consider the query "Find the average salary of persons." Try to write the query by yourself before you look at the solution. We write query as follows: