Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is mysql compatible with sql-92?

The other day I was trying to create a table in mysql usign a standard syntax:

create table client(
    rc character varying(11)
    constraint client_pk primary key 
);

However, mysql doesn't support setting a primary key this way. (I'm 99% sure it's compliant with the sql-92 norm) So my question: Does MySQL fail to support at even these basic things from a 20 year old standard, or am I missing something?

EDIT: my goal is NOT to create this table using using some mysql dialect, but to create this table using a standardized syntax. And the question is whether it is possible.

like image 490
Novellizator Avatar asked Feb 15 '13 13:02

Novellizator


People also ask

Is SQL compatible with MySQL?

You can use SQL to manage multiple relational database management systems, such as MySQL, SQL Server, Oracle, and MS Access.

What is SQL 92 standard?

SQL-92 was developed by the INCITS Technical Committee H2 on Database. This committee develops standards for the syntax and semantics of database languages. SQL-92 was designed to be a standard for relational database management systems (RDBMSs). It is based SQL-89, which in turn was based on SQL-86.

What standard MySQL follows?

We try to make MySQL server follow the ANSI SQL standard and the ODBC SQL standard, but in some cases MySQL server does things differently: For VARCHAR columns, trailing spaces are removed when the value is stored.


3 Answers

To answer your question, no, MySQL is not fully compliant with the SQL-92 specification, in that MySQL supports only a subset of the specification, and MySQL has some significant(ly useful) extensions which are not part of the SQL-92 specification.

This is not just about the SQL syntax that MySQL accepts and recognizes, but (the probably bigger issue) is about what MySQL actually does with the syntax, the actual operations that MySQL performs with the syntax that it does accept. That's really a more important issue than the more superficial issue of what syntax MySQL recognizes.

It's not that MySQL is lazy. And it's not that MySQL just doesn't care.

There is an entire section of the MySQL documentation devoted to MySQL differences from the SQL standard. And the most important differences aren't really about syntax, but rather about that fact that "MySQL performs operations differently in some cases."

For example, MySQL accepts syntax for CHECK constraints, but MySQL does not actually do any checking or make any attempt to enforce CHECK constraints. MySQL accepts syntax that meets the SQL-92 specification, but the behavior of MySQL is much different from other databases that accept that same syntax.

As another example, the behavior of MySQL with respect to FOREIGN KEY constraints differs significantly, depending on whether the storage engine for the table is MyISAM or InnoDB.


To get your CREATE TABLE statement to execute in MySQL, you would need to change it.

It looks like the easiest option for you would be to remove constraint client_pk from the sql text. You can either specify the table constraint in the column definition:

create table client(
   rc character varying(11) primary key 
);

or declare the table constraint separately"

create table client(
   rc character varying(11),
   primary key (rc)
);

Both of those forms are entirely compatible with the SQL-92 specification.

Note that MySQL does accept the syntax for constraint name definition, when the declaration of the constraint is not on the column, e.g.

create table client(
   rc character varying(11),
   constraint client_pk primary key (rc)
);

And that syntax is also entirely compatible with SQL-92 specifiction.

But note that MySQL ignores the supplied constraint name, and assigns the name PRIMARY to the primary key constraint. And this is true even if the constraint is declared in a separate ALTER TABLE statement.

Note that the syntax that MySQL accepts, and in some cases the operations it performs, is dependent on the settings of specific MySQL variables, in particular, sql_mode, and of particular concern, in the case of your statement, the setting of the default-storage-engine variable, and whether this table will be created using the MyISAM engine or the InnoDB engine, or even the corner case that default-storage-engine has been set to BLACKHOLE.

I understand that your goal is to "create this table with standardized syntax".

But I think you really DO need to be concerned with at least some minimum subset of MySQL specific syntax, in as far as the variety of possible behaviors that MySQL can exhibit when presented with "standardized syntax". Consider:

SET default-storage-engine = 'MyISAM' ;
SET default-storage-engine = 'InnoDB' ;
SET sql_mode = '' ; 
SET sql_mode = 'ANSI' ; 
like image 162
spencer7593 Avatar answered Oct 05 '22 23:10

spencer7593


This works for me in MySQL 5.5.29:

create table client(
    rc character varying(11),
    constraint client_pk primary key(rc)
);

However, as a_horse_with_no_name has pointed out, MySQL ignores the client_pk name and calls it PRIMARY.

References:

http://savage.net.au/SQL/sql-92.bnf.html

http://owen.sj.ca.us/~rk/howto/sql92.html

like image 26
Benny Hill Avatar answered Oct 05 '22 23:10

Benny Hill


create table tablename
(
    column_Name varchar(20) not null,
    column_Name varchar(20),
    primary key(column_name)
);

In this way you can create table and set primary key.

like image 27
RajputAdya Avatar answered Oct 05 '22 21:10

RajputAdya