Sunday, 27 January 2013

Regular Expressions PHP/Javascript

MetaCharacter: It is special chacter(s) which have special meaning in Regular Expression.

For Example:
*, ., /, \, ? and ^.

Sr
Meta character
Desctiption
1
[ ]
Match any character within [phc]
Means find “p” OR “h” OR “c”
2
-
Range
a-z means a to z(a,b,c, ... z)
0-0 means 0 to 9 (0,1,2,3,4,5,6,7,8,9)
A-Z means A to Z(A,B,C,D ..... Z)
3
^
Caret It means start with a character
For example: ^ab (start with “a”)

Inside the bracket if have opposite meaning.
For example: [^a](must not start with “a”)

4
$
End with character
For Example
Abc$ means end with “c”
5
.
The . (period) means any character(s) in this position,
For example, ph. will find php, php-tutorial and php-tutorial-php but not aphp because it has no following character
6
?
Matches the preceding character 0 or 1 times only.
For example:
colou?r will find both color (0 times) and colour (1 time).
7
*
Matches the preceding character 0 or more times.
For example:
tre* will find tree (2 times) and tread (1 time) and trough (0 times).
8
+
Matches the previous character 1 or more times.
For example:
tre+ will find tree (2 times) and tread (1 time) but NOT trough (0 times).
9
{n}
Preceding character, or character range, n times exactly.
For example:
find a local phone number we could use [0-9]{3}-[0-9]{4} which would find any number of the form 723-4567 OR 132-3234 OR 214-3433.
10
()
To group a character.
For Example:
(php), will find the “php” only.
11
|
Vertical bar used for OR
(a|c) find either “a” OR “c”
12
\d
any character in the range 0 – 9
13
\D
Any character not in between 0-9
14
\s
Whitespace or tab
15
\S
Not whitespace or tab
16
\w
Alphanumeric characters (a-z, 0-9, A-Z)
17
\W
Not alphanumeric character (a-z, 0-9, A-Z)


Wednesday, 23 January 2013

MySQL Constraint Example

Q1: Table restrict for delete, if child table have records.

Q2: Mysql constraint restrict.

Q3. How to prevent from delete a parent record, if child exists.

Above all Questions have only one answer that is below with fine example.

Create a table products
CREATE TABLE products (
    id int(11) unsigned not null primary key AUTO_INCREMENT,
    name varchar(255) default null
)

Create a table reviews, as each products have one or more reviews

CREATE TABLE reviews (
    id int unsigned not null primary key AUTO_INCREMENT,
    review_by int(11) unsigned not null,
    product_id int(11) unsigned not null,
    FOREIGN KEY (product_id) REFERENCES products (id) ON DELETE RESTRICT  ON UPDATE RESTRICT
  )


Now, if you try to delete a product having one or more review(s), you will get an mysql error similar to following.

Error
SQL query:
DELETE FROM `products` WHERE `products`.`id` =1
MySQL said:

#1451 - Cannot delete or update a parent row: a foreign key constraint fails (`abc`.`reviews`, CONSTRAINT `reviews_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`))

You can only delete the product, after deleted its reviews.



You can also add constraints after table creation, Use following query as example.
alter table `reviews` add constraint `FK_reviews_check` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE RESTRICT ON DELETE RESTRICT;
MySQL constraints Requirement
1. Both FOREIGN KEY table and REFERENCE table must have same Storage Engine.
2. Both FOREIGN KEY and Reference Id must have same datatype.
3. Both FOREIGN KEY and Reference Id must have same attributes means either both un-signed OR signed.