How can I add regular expressions to MySQL datafields? - mysql

I have the following table:
Table Account{
[...]
email varchar(100),
[...]
}
and a corresponding regular expression:
/^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i
How can I use the MySQL model, to link the regular expression to the the data-field "email", so that the regex is accessible to read out through php as well as a trigger or even better, in a kind of constraint?
I want to define this directly using MySQL workbench, so a separate table won't work for me.

In my opinion, input validation like this is not really the task of the database. Those validation rules may change. You may even make them configurable by the user some day. This becomes awfully difficult if the rules are buried in some trigger somewhere.
Also, you will need to handle a validation error on application level anyway - showing a message of some sort.
The best place to make them accessible to PHP inside mySQL may be a column comment, that you can fetch using SHOW COLUMNS in conjunction with the FULL keyword.

Related

How Can I Check A String Whether It Does Exists In A The Given String Or Does Not?

I am currently working on a database project that uses platform MySQL. Project includes tables such as course, instructor, researchArea etc. What I really need is a trigger that checks how many words from "researchArea keywords" are in the given text. Text is going to be pulled from another table which named course. With the result I am planing to get a integer matching percentage value and with the value, I planned to insert the calculated value into "matchingvalue" which is a attribute in instructor table, a attribute to describe the relation of the course content between the instructor.
I have a big lack of scripting experience and syntax information in sql already and I couldn't even think a solution to solve this thing. Any ideas to create a trigger for this query or function whatever it is?
Image of the EER

LUIS to MySQL query - Azure Chatbot

How to generate MySQL Querys with LUIS and fetch data from the DB hosted in Azure?
Should generate a natural language query to an MySQL Query.
e.g.
How much beer was drunken on the oktoberfest 2018?
--> GET amountOfBeer FROM Oktoberfest WHERE Year ==2018;
Does anyone has an idea how to get this to work?
Already generated small Intents in LUIS e.g. GetAmountOfBeer
Dont know how to generate the MySQL Statements and how to get the data from the DB.
Thanks.
You should be able to achieve this, or something similar, using intents and entities. How successful this can be depends on how many and how diverse your queries need to be. First lets start with the phrase you mentioned: "How much beer was drunken on the oktoberfest 2018". You can easily (as you've done) add this as an utterance for an intent, GetAmountOfBeer. Though I'm a fan of intent names that you can read as "I want to GetAmountOfBeer", here you may want to name the intent amountOfBeer so you can use it in your query directly.
Next you need to set up you entities. For year (or datetime rather) that should be easy, as I believe there are some predefined entities for this. I think you need to use a datetime recognizer to parse out the right attribute (like year), but I haven't tried to do this before. Next, Oktoberfest seems to be a specific holiday or event in your DB, so you could create a list entity of all the events you have.
What you are left with is something like (pseudocode) GET topIntent FROM eventEntity WHERE Year ==datetime.Year, or something like that.
If your query set is more complex, you might have to have multiple GET statements, but you could put those in a switch statement by topIntent so that, no matter what the intent is, you can parse out the correct values. You also might want to build this into a dialog where you can check if the entities exist, and if not, you can prompt the user for the missing data.

creating my own save command in cakephp

As we all know that cakephp has default save command for inserting record into the database.
But i want to know can i create my own save command in cakephp or modify the existing one.
Can I do this?
You must be asking why i'm asking why i'm saying that? let me give you an live example for this----
Suppose i have an textbox which contains the username entered by the user.I'm not taking this as unique, hence more than one user will insert same username(possible..).
Ex-My name is prakash Gupta and i'm taking the username 'prakash'. There will be other users also whose name can be prakash gupta and they will provide the same username.Now inorder to solve this i'm using random function and attaching some digits behind the username, so that
it will be different for every one.
Now 'save' command will take the username which i entered in the textbox and insert into the database but i want to insert the modified username into the database which i generated
by random function. can this be possible???
if yes let me know....
Normally you don't change the standard one but either add another one or use the beforeSave etc. hooks.
The save functions are part of the Models. Normally you modify them with: Behaviors. See: http://book.cakephp.org/2.0/en/models/behaviors.html They let you modify the models, saving etc. without having to code all directly into your models.
Based on your edit:
Unique constraint
As I see you want a unique username. There are multiple solutions for that. First make sure to set a constraint. Even your random() trick will possibly generate a duplicate since random can create also the same. So username.random(4) could generate multiple times: username1234. You cannot be sure.
Constraints should be set at for example your model level. Start this unique check with validations of CakePHP.
When do you know the username is already used
You will know when the validation failed. So first just validate the record. So before save call model::validate() to check whether your unique constraint is ok. If yes just save.
http://book.cakephp.org/2.0/en/models/data-validation.html
If not you can add your random string and check again. Call model::validate to see if it is now ok. This part could be implemented for example in a behaviour in the beforeSave() method.
Now you have a unique one save your record. Make sure to check whether it succeeded because it is possible that in the meantime another record with that username was added. In that case re-run the process.
Lots of work
It is hard because multiple clients can add data same time in a database before you even know. Most simple is to catch the error, so just call save with your username. If it fails because of your unique constraint add the random() string and try again and again. It should be possible to save. Trick here with your random string because if it is short and a username is used a lot you can run into issues. If your random string is 3 numbers you can have the same username 1000 times, from 000 to 999.
Other option
What you sometimes see is suggestions, the interface suggests some free names based on your username input. You still need the check though.
Another option
Just tell the user it is in use. Maybe add an ajax check so you can instantly show the result by validating the field.

mysql don't return results if not from statement but from INDEX table or something

I think my question was a little confusing.....It confused me :)
Working on a media site as a take-over project and it has a custom CMS. The client wants the ability to activate/deactivate media....sort of like Wordpress's publish/unpublish feature.
Instead of digging through all the code looking for mysql queries (which I'm not opposed to), I was wondering if you can add a sort of INDEX to a table that won't let it return result rows if that rows "active" column = let's say 0.
Just trying to be lazy and learn something at the same time, heh.
I don't need examples of queries to make it happen, btw.
What you describe is called a "view". Here is a page describing how to create them in MySQL: http://dev.mysql.com/doc/refman/5.0/en/create-view.html. However, in most cases you will still have to alter your code to use the view instead of the table.
You can consider create a view (which contains active record only)
AND swap the view name to actual table name instead, so you can achieve the negative filtering without changing any of your source code.

Case Sensitivity and Indices in MYSQL

I am creating a system where is use e-mail address as unique identifier. There are times I need to look for a user by the e-mail address. If the user enters the e-mail address in all lower case, but the database has it stored as mixed case, is it a full scan, or will the database still use the index?
This really is a problem because I attempt to validate that the e-mail address is valid when adding the user to the system.
I am using grails with MYSQL on the back end for the database.
I am currently doing this to find the user
def c = User.createCriteria()
def currentUser = c.get() { ilike('emailAddress',message.sender.address) }
I know I can force the case at the UI, but I would also like to force it at the model level
Thanks, and sorry for the long question
MySQL specifies collation for every character column, which may be case-sensitive or case-insensitive.
Index is built using whatever collation is specified on the column, so:
Alter your table to specify case-insensitive collation on email column (like ascii-general-ci, for example).
Rebuild your index.
Enjoy.
Keep in mind that all queries against email will now be case-insensitive.
Unfortunately MySQL does not support function based indexes like Postgres and Oracle. (Source)
A possible workaround in MySQL is to add another column for lower case e-mail addresses, and a trigger that populates it with lower case e-mails on all updates and inserts. Then simply index that column, and use that for your lookups.
With a function based index, you would have been able to do the following:
CREATE INDEX
ix_users
ON
table_users
USING
lower(email_address);
With Grails you have a few options to validate the model:
You can write a setter for the emailAddress that converts it to a consistent case:
public void setEmailAddress(email){
emailAddress = email
}
A more involved but correct answer would be to create a custom editor (PropertySupportEditor) that will handle the normalization for you automatically.
You will also would want to write a custom validator to ensure that Grails' validation fails if the emailAddress is not correctly normalized. If you wanted to make it really elegant you could make the validator into a reusable constrtaint using the constraints plugin which could result in something like this:
static constraints = {
emailAddress(normalizedEmail:true)
}