As far as I can tell I have done nothing wrong here, especially when compared with the syntax of other .sql files I have run on my local MySQL server prior to this.
This screenshot shows the .sql file and my attempt to run the file in MySQL.
Is there something obvious that I'm missing?
Number is not a valid data type in MySQL. Use one of the Integer types. VARCHAR2() is also invalid - use VARCHAR()
Although it is syntactically correct, don't use FLOAT for money values - it loses precision and you can get errors in basic arithmetic. Use DECIMAL for cash values.
You can find the complete reference here
Your data types are incorrect - NUMBER and VARCHAR2 do not exist. Have a look at the MySQL manual chapter on Data Types.
On a side note, it's best to keep case-sensitivity with table names. It's doesn't matter on Windows, but should you ever move your code to a UNIX environment it would be a pain to fix!
Use INT(3) instead of NUMBER(3) and VARCHAR(20) instead of VARCHAR2(20)
Related
My server is using a MySQL DB, connecting to it via the C++ connector. I'm nearing production and I've been spending some time trying to break things as part of hardening the server.
One action item I had was to see what would happen if I execute a statement with a string that is longer than VARCHAR. For example, if I have a column defined as VARCHAR(4) and then set it to the string "hello".
This of course throws an exception with the error code 1406 (Data too long for column).
What I was wondering was if there was a good or standard way to defend against this? Obviously one thing is to check against the string length and truncate manually. I can do this, however there are many tables and several columns with VARCHAR. So my worry is updating server code if one of the columns using VARCHAR has its length increased (i.e. code maintainability)
Note that the server does do some validation up front. I'm just trying to defend against a subtle bug or corner case that lets something slip through.
A couple of other options on the table are to disable strict so it will give a warning and truncate or to convert VARCHAR to TEXT.
I was wondering a few things.
Is there a recommended method to handle this situation?
What are the disadvantages of disabling strict?
Is it worth (and is it possible) to query the DB at runtime the VARCHAR lengths? Note that I'm using the C++ connector. I suppose I could also write a tool that is run before compiling which would extract out VARCHAR lengths from the SQL code used to generate tables. But that then makes me wonder is I'm over engineering this.
I'm just sorting through the possible approaches now and thought I'd seek advice from those with more experience with MySQL.
As an experience database engineer I would recommend a combination of the follow two strategies:
1) If you that know that a there is a chance, however small, that data for your varchar(4) could go higher than 4 then make the varchar field larger than 4. For example, if you expect that the field can go as high as 8 then set the field to varchar(10). The beauty of using a varchar field instead of a char is that a varchar will only use whatever storage it needs.
2) If there is a real issue with data constantly being larger than the varchar field length then you should right your own exception handler to trap for the 1406 error. For the exception to work properly you will need to come up with some type of strategy on exactly how you want to handle the exception. For example, you could send an error to the user and ask them to fix the problem, you could accept the data but truncated it so it fits into the field, or you could send the error to a log file to get fixed at a later time.
I came to know that MySQL Workbench allows a BIN flag on a column for storing data as binary strings here.
If that's the case, what's the difference between BIN flag and the datatype BINARY(n)? Besides, it even allows me to set this flag on columns with datatypes VARCHAR(n), CHAR(n), etc. which seems to be conflicting.
When should I exactly use this flag?
You can see the manual entry here. The most obvious use for this is where you have an ID rather than text where you may want an ID of, say, "A107652B" to be distinct from "a107652b". Most textual types would treat these as the same.
I am trying to practice SQL and proceed to PL/SQL with the set of exercises given to me (the set of exercises is based on Oracle SQL).
I wanted to do the SQL query exercises on my laptop by starting on basics like creating tables and querying them, and I tried it before in academia, but the exercises given to me seem to be in another form and I need to learn that type of SQL query.
I used PHPMyAdmin SQL, but it seems it won't accept data type varchar2([some num]) and number([some num]) and always indicates there is an error in my syntax.
I know that this error just stems from syntax incompatibility (since I tried it in another program once, but I forgot what it was, and I am trying to learn again and master it).
So how do I get it to work? Or what other programs can I use to start to practice this form of SQL query which accepts varchar2 and number data types?
varchar2 and number are Oracle data types. MySQL's equivalents are varchar and numeric, respectively.
I've googled this but can't get a straight answer. I have a mysql database that I want to import in to oracle. Can I just use the mysql dump?
Nope. You need to use some ETL (Export, Transform, Load) tool.
Oracle SQL Developer has inbuilt feature for migrating MySQL DB to Oracle.
Try this link - http://forums.oracle.com/forums/thread.jspa?threadID=875987&tstart=0 This is for migrating MySQL to Oracle.
If the dump is a SQL script, you will need to do a lot of copy & replace to make that script work on Oracle.
Things that come to my mind
remove the dreaded backticks
remove all ENGINE=.... options
remove all DEFAULT CHARSET=xxx options
remove all UNSIGNED options
convert all DATETIME types to DATE
replace BOOLEAN columns with e.g. integer or a CHAR(1) (Oracle does not support boolean)
convert all int(x), smallint, tinyint data types to simply integer
convert all mediumtext, longtext data types to CLOB
convert all VARCHAR columns that are defined with more than 4000 bytes to CLOB
remove all SET ... commands
remove all USE commands
remove all ON UPDATE options for columns
rewrite all triggers
rewrite all procedures
The answer depends on which MySQL features you use. If you don't use stored procedures, triggers, views etc, chances are you will be able to use the MySQL export without major problems.
Take a look at:
mysqldump --compatible=oracle
If you do use these features, you might want to try an automatic converter (Google offers some).
In every case, some knowledge of both syntaxes is required to be able to debug problems (there almost certainly will be some). Also remember to test everything thoroughly.
I am new to all this so looking for some help. Sorry if the question is really novice. I am learning all of the char, varchar, etc.
Try BOOL. (It's an alias for TINYINT(1).)
If the MySQL server is at least version 5.0.3, you can also use BIT (or BIT(1), same thing).
You'd probably benefit from checking out some of the docs.