problems with the check in a sql table [duplicate] - mysql

This question already has answers here:
CHECK constraint in MySQL is not working
(8 answers)
Closed 2 years ago.
i've created this table in SQL
CREATE TABLE product (
code CHAR(7) NOT NULL,
name VARCHAR(30) NOT NULL,
Description VARCHAR(500) NOT NULL,
cost DOUBLE UNSIGNED NOT NULL,
PRIMARY KEY (code),
check(substring(code,1,3) like '%[a-z]%'
and substring(code,4,4) like '%[0-9]%'),
);
the value 'code' must consist of 3 characters and 4 numbers, but it doesn't work. what's wrong in the check?

the value 'code' must consist of 3 characters and 4 numbers, but it doesn't work. what's wrong in the check?
Use regular expressions:
check (code regexp '^[A-Z]{3}[0-9]{4}$')
MySQL does not extend the definition of LIKE to include character classes. It has real regular expression support.

Related

MySQL namespace in create table [duplicate]

This question already has answers here:
Grouping tables within a MySQL database
(4 answers)
Closed 3 years ago.
Can I give the namespace on the table create syntax ? Like below in com.sumeet.QRTZ_JOB_DETAILS
It is not working with below syntax
CREATE TABLE "com"."sumeet"."QRTZ_JOB_DETAILS"(
SCHED_NAME VARCHAR(120) NOT NULL,
JOB_NAME VARCHAR(200) NOT NULL,
JOB_GROUP VARCHAR(200) NOT NULL,
DESCRIPTION VARCHAR(250) NULL,
JOB_CLASS_NAME VARCHAR(250) NOT NULL,
IS_DURABLE VARCHAR(1) NOT NULL,
IS_NONCONCURRENT VARCHAR(1) NOT NULL,
IS_UPDATE_DATA VARCHAR(1) NOT NULL,
REQUESTS_RECOVERY VARCHAR(1) NOT NULL,
JOB_DATA BLOB NULL,
PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP));
There is no concept of a hierarchy in MySQL. ie. No folders, containers or namespaces for your databases.
MySQL has 2 concepts: A database (Often referred to as a Schema), and tables. You'd need to name your database "com.sumeet" (Which is allowed, but can be considered messy*), and your table: QRTZ_JOB_DETAILS. Various software (like PHPMyAdmin) will look at the similarities and will group databases together into folders based on the name itself. For example, if you use underscores "com_sumeet" phpmyadmin will actually group those together:
Note: Naming your database with periods is likely more trouble than it's worth, due to the fact that your database calls will now require quotes in order to function correctly. (ie. "com.test".table vs com_test.table) Period is the universal separator between databases and tables, so the quotes are required to tell MySQL specifically what is a database, and what is the table in your queries.

Unable to modify column in MySQL table [duplicate]

This question already has answers here:
NULL value in multi-column primary key
(5 answers)
Closed 4 years ago.
I have a requirement wherein I need to change the table structure as per the production environment on a lower environment. The table has a multi-column PRIMARY KEY as (md_biobjectid,projectid,md_mapid), I want to modify column 'md_mapid' to varchar(50) DEFAULT NULL from 'md_mapid' varchar(50) NOT NULL.
When I am running the query :
alter table table_name
modify column md_mapid varchar(50) DEFAULT NULL; it doesn't run and I am getting following error :
Error Code: 1171. All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead.
Other columns structure on both the environment is :
'md_biobjectid' varchar(50) NOT NULL DEFAULT ''
'projectid' varchar(50) NOT NULL DEFAULT ''
MySQL version : 5.7.21-log.
you need to isse without DEFAULT NULL postfix. md_mapid column is part of composite primary key and cannot be set to null.
alter table table_name modify column md_mapid varchar(50)

unknown column in 'P001' where clause [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 4 years ago.
I trying to delete an entry from a table in mysql the table is for publications This is the table.
CREATE TABLE publication(pub_id VARCHAR(4) NOT NUll,
price DECIMAL(3, 2) NOT NULL,
name VARCHAR(20) NOT NULL,
frequency INTEGER(10) NOT NULL,
PRIMARY KEY(pub_id) );
This is the table with added entries
The query i'm using is
Delete from publication where pub_id = P001;
I get an error "Unknown column 'P001' in where clause"
What am I doing wrong?
If you don't use quotes, it will try to find a column|other object named P001. Try
delete from publication where pub_id = 'P001';
please check manual Mysql
correct:
Delete from publication where pub_id = 'P001';
check the manual and sintaxis here

Why does this script fail because it doesn't like the column name? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 5 years ago.
It doesn't like the word release as a column name.
CREATE TABLE external_db (
external_db_id INT not null,
db_name VARCHAR(100) NOT NULL,
release VARCHAR(40) NOT NULL,
status ENUM ('KNOWNXREF','KNOWN','XREF','PRED','ORTH', 'PSEUDO') not null,
PRIMARY KEY( external_db_id )
);
I changed the field name to releaseX and the error went away.
This script came from https://github.com/Ensembl/ensembl/blob/release/91/sql/table.sql, which is supposed to be mySQL. Is this a versioning issue in MySQL or can I decorate the word release in the script so it can be used as a column name?
I am using MySQL 5.7 and MySQLWorkbench 6.3.
I know release is a bad name for a column but I didn't write the script.
Release is a reserved keyword
To use it as a column name, you can escape it with backticks like this:
`release`

What does the number in mediumint(10) denote? [duplicate]

This question already has answers here:
What is the size of column of int(11) in mysql in bytes?
(11 answers)
Closed 8 years ago.
I'm creating a table in MySql and I came across columns with datatype as mediumint followed by a number in parenthesis. What does this number denote?
https://dev.mysql.com/doc/refman/5.7/en/integer-types.html says that unsigned mediumint can take a max value of 16777215, so how does it differ if have columns with different sizes mediumint(6) or mediumint(10) ?
I suppose here in an answer:
http://dev.mysql.com/doc/refman/5.6/en/numeric-type-attributes.html
so difference is only in formatting output, nothing else...