Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Gheez listing all the best practices for MySQL would require a book.</p> <p>Let me first answer: </p> <blockquote> <p>I want the next user who registers has an age greater than all those already</p> </blockquote> <p><strong>Use a trigger:</strong></p> <pre><code>DELIMITER $$ CREATE TRIGGER bi_account_each BEFORE INSERT ON account FOR EACH ROW BEGIN declare MaxAge integer; SELECT Max(account.age) INTO MaxAge FROM account; IF (new.age &lt;= MaxAge) THEN BEGIN /* force an error by selecting from a table_that_does_not_exist.*/ SELECT * FROM ErrFromTrigger_bi_account_each_New_Member_Must_Be_Older_then_The_Last END; END IF; END$$ DELIMITER ; </code></pre> <p><strong>Or a stored function</strong></p> <pre><code>DELIMITER $$ CREATE FUNCTION CanInsertInAccount(pAge integer) RETURNS boolean BEGIN declare MaxAge integer; declare InsertAllowed boolean; SELECT Max(account.age) INTO MaxAge FROM account; SET InsertAllowed = (pAge &gt; MaxAge); RETURN InsertAllowed; END$$ DELIMITER ; </code></pre> <p>See: <a href="http://dev.mysql.com/doc/refman/5.1/en/triggers.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.1/en/triggers.html</a><br> For more info on triggers.<br> And: <a href="http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html</a><br> For more info on stored procedures and functions.</p> <p>As for <strong>best practices</strong>, my list would be:</p> <ol> <li><strong>Don't trust customer input:</strong> When inserting values from php into MySQL always use <code>mysql_real_escape_string()</code> on <strong>every</strong> value (or use PDO {prepared statements}); This will protect you from SQL-injection attacks. </li> <li><strong>Strip HTML tags before displaying customer inputted data</strong> Disallow all HTML. Use htmlspecialchars() to convert HTML characters into HTML entities. So characters like &lt; and > that mark the beginning/end of a tag are turned into &lt; and >. It is not enough to simply use strip_tags() to only allow some tags as the function does not strip out harmful attributes like the onclick or onload. </li> <li>Normalize your database </li> <li>Use foreign key checks </li> <li>Use transactions, but commit (or rollback) as often as you can. Only put stuff that belongs together in one transaction. </li> <li>Use triggers </li> <li>Use stored procedures to manipulate and test stuff that depends on the layout of your database.</li> </ol> <p>That's what I can think of right now, the list is by no means complete and these things are somewhat personal. It works for me, YMMV.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload