what is the operator precedence in Boolean algebra? - boolean-logic

As a XOR b NOR c is not equal to a NOR b XOR c,there must be some precedence rule for all operators in Boolean algebra.So what is the precedence rule for XOR,NAND,XNOR,NOR ??

The precedence is NOT > XOR > AND > OR, some of the gates you mention are a combination of two operators in reality. For instance A NAND B can be thought of as NOT(A AND B).

Related

MySQL - Indexing for String Comparison

This is for InnoDB with MySQL 5.7.
If I have a query like:
SELECT A, B, C FROM TABLE WHERE STRCMP(D, 'somestring') > 0
Is it possible to have an index on D which can be used by the query? i.e. is MySQL smart enough to use the btree index for STRCMP function?
If not, how might I be able to redesign the query (and/or table) such that I can do string comparison on D, and there can be some form of pruning so that it does not have to hit every single row?
Amazing how many wrong answers so far. Let me see if I can not join them.
STRCMP returns -1, 0, or 1, depending on how the arguments compare.
STRCMP(D, 'somestring') > 0 is identical to D > 'somestring'. (Not >=, not =, not LIKE)
Actually, there may be collation differences, but that can be handled if necessary.
Any function, and certain operators, 'hide' columns from use with INDEXes. D > 'somestring' can benefit from an index starting with D; the STRCMP version cannot.
Why not just
WHERE D > 'somestring'
This would leverage a typical B-tree index on column D.
If you perform comparisons only by passing column values to a function such as STRCMP(), there is no value in indexing it.
Reference: https://dev.mysql.com/doc/refman/5.5/en/index-btree-hash.html
A B-tree index can be used for column comparisons in expressions that use the =, >, >=, <, <=, or BETWEEN operators.
So, you may use
SELECT A, B, C FROM TABLE WHERE D > 'somestring'
instead, while LIKE is more strict and could be what you expect.
Also, you can use the COLLATE operator to convert the column to a case-sensitive collation, to make sure it's comparing the case as well.
SELECT A, B, C FROM TABLE WHERE D > 'somestring' COLLATE utf8_bin
Mysql will not use indexes for function calls.
Use below query And Create index on D column
SELECT A, B, C FROM TABLE WHERE D ='somestring'
to create index use the below query
create index index1 on TABLE(D);

MYSQL AND & OR priority

I know its recomended to use parenthesis to separate and ,or statements.but I'm wondering how mysql engine does render statements without parenthesis.lets say we have this statement:
select * from users where A and B or C and D;
how would it be with parenthesis?
AND has higher priority than OR.
select * from users where (A and B) or (C and D);
Refer to: http://dev.mysql.com/doc/refman/5.7/en/operator-precedence.html
It is like in math expressions:
AND is like a PRODUCT
OR is like a SUM
so AND are executed before OR, unless differently indicated through parenthesis, like in math again.
AND has higher precedence than OR.
Priority of AND and OR operator in Mysql select query
Operator Precedence

What is meaning of <> in MS-SQL?

I don't know what the is meaning of <> is in MS-SQL
For example:
IF(#userID <> '')
Return 0;
ELSE
Return -1;
In Transact SQL, <> means NOT EQUAL TO
Check msdn here
As it would logically imply:
x <> y
if x is greater then or less then y.
Even simpler, not equal.
As you ask for MYSQL, Its one of the comparison operator.
MYSQL Documentation for Comparison Operator
<> Means "Not equal operator"
<> means not equal to in Transact-SQL.
Compares two expressions (a comparison operator). When you compare nonnull expressions, the result is TRUE if the left operand is not equal to the right operand; otherwise, the result is FALSE.
<> in MS SQL means "NOT EQUAL TO".
In your case IF(#userID <> '') means IF variable #userID IS NOT EQUAL to '' THEN
As you can see here it is not equal
<> is a comparison operator meaning "not equal to"
This is used to compare two expressions and will result to a boolean value.
Thank you! :)

Given 3 possible values for X, is it faster to do WHERE (X = B OR X = C), or to do WHERE X != A?

Given 3 possible values for X (A, B, C), is it faster to do:
WHERE (X = 'B' OR X = 'C'), or
WHERE X != 'A'
Or does it depend? If so, then what does it depend on?
Option 1:
WHERE (X = 'B' OR X = 'C')
and
WHERE X IN ('B', 'C')
are equivalent and may use an index on (X).
Option 2:
WHERE X != 'A'
will not use an index on (X). See a comment by Henrik Grubbström at the MySQL docs, How MySQL Optimizes WHERE Clauses page:
Indexes are ignored for the <> operator:
So, if the use of index makes the query faster (for example, if 99% of the table has X = 'A'), use the first option.
Note: The != operator is a synonym (in MySQL) of the SQL-standard <> inequality operator.
Your second operation should be faster because it requires one less logical check. If it's scanning a value, it only has to check to make sure it's not A, where your first operation would need to match B and then if there is no match, C. Regarding the use of an index, it depends on what your index looks like and how it's being called. If you have an index on columns W, X and you only filter X, the index will not be used as indexes work left-to-right.
Direct equality (=) and inquality (!=) takes the same time. Best case your queries will run the same time, but worst case, case 1. could be slower as you're adding another case to check by the OR.
Of course not knowing if there are indexes or the distribution of values for X can affect the performance...
In my opinion the second item is better because it is always only one comparison; in the first item if the value to be tested is 'C' or 'A' you have to 2 comparison, the fisrt (X = 'B') will fail and then the second comparison gives the final result.
If case 1 uses an index, which in my view it should if there is an index on X, it will be faster than case 2 if case 2 doesn't use an index, which in my view it won't. In general. It also depends on the actual distribution of values: if significantly skewed, results will vary accordingly.

SQL Query: boolean processing

I have no idea if this is the right forum or not. Lets say I have the following:
SELECT *
FROM MyTable m
WHERE ((A OR B) AND (C OR D))
Assume that A, B, C, D are proper boolean clauses that each need to be evaluated on a row-level basis. Lets also assume no indexes.
This is logically equivalent to:
SELECT *
FROM MyTable m
WHERE (A AND C)
OR (A AND D)
OR (B AND C)
OR (B AND D)
Is there a performance advantage to either one? We're on MSSql-2008.
My understanding is that your first case is more efficient, because:
in this clause:
WHERE ((A OR B) AND (C OR D))
the entire statement fails if neither A or B are true; the Second part of the statement, (C OR D) is not evaluated. Even if A OR B are true, there is only one more pair to check - C OR D. Worst case is that four criteria are checked before the statement as a whole can be evaluated (if A = False, B = False, C= False, but D = True). Best case is, the statement becomes false after checking only A and B. If neither are true, then the entire statement is false.
In your second case, each of the four cases must ALL be evaluated before the statement as a whole can be evaluated.
Nesting the OR conditionals inside the AND means if the first case fails, more on along, nothing more of interest here. You improve things even more if you place the case most likely to be false as the first pair.
I will be interested to hear from others on this . . .