Database is a bit interesting

Source: Internet
Author: User
Tags php regular expression

Hi

Today there is no Sai Tou, direct database to start. Looked at the course, the introduction of this is going to 7 days to fix it, after all, still working in the Department of the people ...

1. MySQL

---add constraints---

--mysql> ALTER TABLE users2 ADD PRIMARY KEY (ID);

--mysql> ALTER TABLE users2 ADD FOREIGN KEY (PID) REFERENCES Province (ID);

--Set default value mysql> alter TABLE USERS2 ALTER age set to default 15;

Delete the default value mysql> alter TABLE USERS2 alter age DROP to default;

--Delete primary key, because each table has only one primary key, so mysql> ALTER table users2 DROP PRIMARY key;

--Remove the unique constraint, because the unique constraint may have many in a table, so first look for the name mysql> SHOW INDEXES from users2\g; that Key_name

mysql> ALTER TABLE users2 DROP INDEX username;

Note that the constraint is removed here

--Delete the foreign key constraint, but also to see the first name

mysql> SHOW CREATE TABLE users2;
+--------+----------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -------------+
| Table | Create Table |
+--------+----------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -------------+
| Users2 | CREATE TABLE ' Users2 ' (
' username ' varchar () not NULL,
' PID ' smallint (5) unsigned DEFAULT NULL,
' ID ' smallint (5) unsigned not NULL DEFAULT ' 0 ',
' Age ' tinyint (3) unsigned not NULL,
UNIQUE KEY ' pid ' (' pid '),
CONSTRAINT ' Users2_ibfk_1 ' FOREIGN KEY (' pid ') REFERENCES ' province ' (' ID ')
) Engine=innodb DEFAULT charset=latin1 |
+--------+----------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -------------+
1 row in Set (0.00 sec)

Here, find the name behind constraint, and then

mysql> ALTER TABLE users2 DROP FOREIGN KEY users2_ibfk_1;

The foreign key constraint is removed, but the index is still in place, and the above command drop index is required to delete the indexes

---Modify the data table---

--Modify the column definition (data type, location, etc.)

mysql> ALTER TABLE users2 MODIFY ID SMALLINT UNSIGNED not NULL first;

Note that the definition of the ID here is not changed, but it should be entered again, if you want to modify the definition, you have to enter the corresponding, and modify the definition of the column must be careful, especially the data in the database is very miscellaneous.

--Modify the column name

mysql> ALTER TABLE users2 change pid p_id TINYINT UNSIGNED not NULL;

Change is more capable and more input

--Modify the data table name

mysql> ALTER TABLE users2 RENAME users3;

Mysql> RENAME TABLE users3 to Users2;

Two methods, where the second method can modify multiple tables

------emphasize again, try not to modify the table name or library name, dangerous, must be cautious. -------

---summary---

--constraints

-5 constraints or table-level and column-level

--Modify the data table

-To field: Add delete Modify definition and name

-Pair constraint: Add delete

-Data sheet: renaming

2. php-Regular Expression

---boundary control and Mode unit---

--^ where the matching string begins-limits what fields to match must begin with

-for example, ^d must start with D and cannot have a space before

--$ ... End

--mode unit ()

To treat () as an atom, a regular expression in ().

----Basic grammar to this end, the knowledge points scattered, to be flexible to use----

---lazy match and greedy match---

--correction mode-fix the case of ambiguous match

--Greedy match is selected long when ambiguous, match opposite

--when there is ambiguity, the default greed

<?php
/*
* description:php Regular expression function
*
* @name: Show
* @description: Output debug
* @param $var: input data
* @return void
*
*/

Create ambiguity
$pattern = '/imooc.+123/'; Match IMOOC any number of characters and end with 123
$subject = ' I love imooc__123123123123123 ';

$matches =array ();
Preg_match ($pattern, $subject, $matches);

Show ($matches);

Function Show ($var =null) {
if (empty ($var)) {
echo ' null ';
}elseif (Is_array ($var) | | Is_object ($var)) {
Array,object
Echo ' <pre> ';
Print_r ($var);
Echo ' </pre> ';
}else{
String,int,float ...
Echo $var;
}
}

The result is

Array (    [0] = imooc__123123123123123)

--Modified to lazy mode: $pattern = '/imooc.+123/u ';

Array (    [0] = imooc__123)

---common pattern of corrections---

--u, lazy match $pattern = '/imooc.+123/u ';

-----ignore case $pattern = '/imooc.+123/ui '; There are two kinds of correction mode, it is also possible

--x, ignoring blank $pattern = '/imo o c.+123/uxi ';

--s, let '. ' Match all characters except line break

--note that the correction pattern is the correction of the matching structure, the object is regular expression, must not match the object

-------------Practical Application-------------

---common situation---

--Verbal description of the behavior of the demand ~ Regular expression ~ Regular expression

--Non-empty ~ atoms occur 1 times to infinity ~.+//. Generally represents arbitrary atoms, + represents any number, application scenario is the hint of required fields, etc.

--Floating point number (example is two decimal places) ~ numbers appear continuously 1 to infinity, followed by two digits ~\d+\.\d{2}$//\d digit match, \. Escape., \d{2} Two-bit number i,{} to implement quantifiers, $ must end with two digits (and match the previous description). The scenario is a validation of payments and the like

----------------Review------------------

Screening

| Match two or more, that is, or

[] match any atom in parentheses, special [1-3], here--the meaning of "to"

[^] ditto for reciprocal, matching all atoms in parentheses except

Note: With ^ to be careful, it will match all other invisible atoms, including spaces, and then there is the ^ to be written with the other atoms close to the head

-Define the set of atoms

. equivalent to [^\n], matches any character except newline characters--basically all

\d [0-9] matches a decimal number

\d [^0-9] matches a non-decimal digit

\w [0-9a-za-z] matches a number, letter, or underscore

\w [^0-9a-za-z] matches a non-numeric, letter, or underscore

\s matches a non-visible atom

\s matches a visible atom

--quantifier

-Indicates how many times an atom appears in succession

-{n} indicates that the atom in front of it happens to be n times in a row--5{3}

-{n,} indicates that the atom in front of it appears at least n consecutive times

-{N,M} [f|5]{3,6} indicates n times, up to M times, N to M

-* any time, as long as continuous, all match, {0,}

-? {0,1}0 to 1 times

-+ {1,} at least 1 times

-------------------Review is over------------------------

--Phone number matching ~ number, 11 digits, ending with a number, starting with the number 1 ~^1[34578]\d{9}$ or ^1 (3|4|5|7|8) \d{9}$

--email address Match ~ English or number, the number of digits, there is @, there is. And with a non-numeric end ~^\[email protected]\w+ (\.\w+) +$//note () The use of custom atoms (blocks), the possible repetition of some of the atomic set of write

--url address matches ~ The beginning of the letter (not the number)://, then the number, the letter. ~^ (https?:/ /)? (\w+\.) +[a-za-z]+//Here is just a match to the general situation

Database is a bit interesting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.