How to alter table to add a Range Partition - partitioning

OK Guys, I understand your points... so let me put it this way,
Here is my table
CREATE TABLE TEST1
(
USERID integer,
ENTRYCREATEDDATE TIMESTAMP
) ;
And here is my alter query (I tried adding and removing ADD word below)
ALTER TABLE TEST1 PARTITION BY RANGE (USERID)
(
PARTITION P0 VALUES LESS THAN (10)
);
RESULT:
1. Error report:
SQL Error: ORA-01735: invalid ALTER TABLE option
01735. 00000 - "invalid ALTER TABLE option"
*Cause:
*Action:
Error report:
SQL Error: ORA-00902: invalid datatype
00000 - "invalid datatype"
*Cause:
*Action:
Now Please help me. Actually I want to partition on ENTRYCREATEDDATE, but will come to that later

Here's how to do it in Teradata:
http://www.info.teradata.com/HTMLPubs/DB_TTU_14_00/index.html#page/SQL_Reference/B035_1144_111A/Alter_Function-Syntax.020.106.html
MySQL:
https://dev.mysql.com/doc/refman/5.1/en/alter-table-partition-operations.html
Oracle:
https://docs.oracle.com/cd/E17952_01/refman-5.5-en/alter-table-partition-operations.html
Your google search skills need work sir!
Per your question: Make sure USERID is an INT

Related

mysql error 1062 during alter table modify column

I have a table that looks like this:
CREATE TABLE t1 (
id BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
col1 VARCHAR(256),
UNIQUE INDEX t1_col1_index (col1)
)
I'm trying to modify the col1 type using the following query:
ALTER TABLE t1 MODIFY COLUMN col1 varchar(191) COLLATE utf8mb4_unicode_ci;
However, I run into this duplication error:
error: ("1062", "QMYSQL3: Unable to execute statement", "Duplicate entry '+123456789' for key 't1_col1_index'")
I initially thought it could be because two or more rows might 'contain' similar value for col1 and on changing varchar length the data gets truncated but then I found out that data truncation wouldn't even allow the query to go through. Any pointers on what could be causing this?
EDIT (Resolved): Truncation does happen when ##sql_mode is not set with STRICT_TRANS_TABLES. This was causing the error.
You are reducing the length of a varchar column that is controlled by a UNIQUE constraint.
This is risky business. Oversize data will be silently trimed (unless you have the ##sql_mode set to STRICT_TRANS_TABLES in which case an error will be raised). This probably generates duplicates, which cause the error to be raised by your UNIQUE constraint.
You can check the max length of the values in your column with :
SELECT MAX(CHAR_LENGTH(col1)) FROM t1:
I am not sure if this is work.
Try to check the table t1.
select count(1) from t1 where col1 = 123456789
Now if count is greater than one then try to remove the other one and leave only one record.
Then try to run your statement again.
Reminder:
Do back up first before removing.

MySQL - Concatenate Existing Fields in New Generated Field

In my table, I have two fields: book and reference. Neither are required to be unique on their own. However, the concatenated value of these two values must be unique.
I'm trying to create a generated column that concatenates the two, but I'm receiving the following error message when running the SQL:
Executing:
ALTER TABLE `bibleverses`.`myverses`
ADD COLUMN `fullref` VARCHAR(20) GENERATED ALWAYS AS (CONCAT(book, reference)) STORED AFTER `mp3`;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GENERATED ALWAYS AS (CONCAT(book, reference)) STORED AFTER `mp3`' at line 2
SQL Statement:
ALTER TABLE `bibleverses`.`myverses`
ADD COLUMN `fullref` VARCHAR(20) GENERATED ALWAYS AS (CONCAT(book, reference)) STORED AFTER `mp3`
You can achieve this thing by just applying UNIQUE key constraint to both these columns and it will become a composite key so that you can store the unique values in a pair of these two columns. You can try following SQL statement :
ALTER TABLE bibleverses.myverses ADD UNIQUE(book, reference);
First execute an alter table to add your new column, then you run an update to fill out the fild, like:
ALTER TABLE <table_name> ADD COLUMN <column name, type, definition, etc>;
UPDATE TABLE <table_name> SET <field> = <value>;
To create a virtual generated column (which updates if the constituents changes)
ALTER TABLE producerADD COLUMNFullName varchar(32) as (CONCAT(FirstName,' ',Surname));

SQL Syntax Sum and Count

I have a 1064 error:
You have an error in your SQL syntax; check the manual that correspond to your MySQL server version for the right syntax to use neear '*) as NB_FR, sum (*)) as MT_FR
FROM gc_mouvements where COOPX="477" or COOPX="4' at line 4
Here is the code I used, I don't understand the origin of the error:
ALTER TABLE gc_modele_retrait ADD COLUMN
(Nb_frais_477 int(5),Nb_frais_481 int(5),Mt_frais_477 int(5),Mt_frais_481 int(5));
UPDATE gc_modele_retrait
SET Nb_frais_477=0, Nb_frais_481=0, Mt_frais_477=0, Mt_frais_481=0;
DROP TABLE IF EXISTS gc_modele_retrait_frais;
CREATE TABLE gc_modele_retrait_frais
(PRIMARY KEY (COCO))
ENGINE=myisam
SELECT COCO, COOPX, COUNT(*) AS NB_FR, SUM(*) AS MT_OPE
FROM gc_mouvements WHERE COOPX="477" OR COOPX="481" GROUP BY COCO, COOPX;
The issue is with using SUM (*), you have to pass numerical value or column containing numerical value to SUM. In your case you need:
SUM (Nb_frais_477 + Nb_frais_481)

Oracle 12c JSON Query Issue with Dot Notation and Double Quotes

I have a table "EvMetadata" with column "Metadata" that has a check constraint of "IS JSON". Note that the table and its columns are created with DOUBLE QUOTES by design.
Following SQL works where I'm not specifying any JSON work to be done by Oracle.
select
m."Metadata"
from "EvMetadata" m
As you can see below, the Metadata column simply displays its content which happens to be JSON data.
However, I get error if I were to issue a json query as follows.
select
m."Metadata"."FileName"
from "EvMetadata" m
I just added "FileName" using dot notation. As you can see above, "FileName" is a valid json field. So why the error?
Error is
ORA-00904: "M"."Metadata"."FileName": invalid identifier 00904. 00000 - "%s: invalid identifier" *Cause: *Action: Error at Line: 2 Column: 3
Could this be a bug with Oracle's JSON query support using the dot notation under a specific scenario where database objects are declared with double quotes? The reason I suspect that may be true is that the following equivalent query, not using the dot notation, works.
select
JSON_VALUE(m."Metadata", '$.FileName')
from "EvMetadata" m
You need to have an "IS JSON" check constraint on the column for dot notation to work:
Here's an excerpt from the documentation:
Each json_key must be a valid SQL identifier, and the column must have an is json check constraint, which ensures that it contains well-formed JSON data. If either of these rules is not respected then an error is raised at query compile time. (The check constraint must be present to avoid raising an error; however, it need not be active. If you deactivate the constraint then this error is not raised.)
Here's a test example I did to verify this is how it's working:
--create a table to put stuff in
create table foo (
json varchar2(4000)
);
--------------------------------
Table FOO created.
--insert test value
insert into foo(json) values('{"attr1":5,"attr2":"yes"}');
commit;
--------------------------------
1 row inserted.
Commit complete.
--try some selects
--no table alias, no constraint, borked
select json.attr1 from foo;
--------------------------------
Error starting at line : 12 in command -
select json.attr1 from foo
Error at Command Line : 12 Column : 8
Error report -
SQL Error: ORA-00904: "JSON"."ATTR1": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
--with table alias, no constraint, borked
select a.json.attr1 from foo a;
--------------------------------
Error starting at line : 15 in command -
select a.json.attr1 from foo a
Error at Command Line : 15 Column : 8
Error report -
SQL Error: ORA-00904: "A"."JSON"."ATTR1": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
--add our constraint
alter table foo add constraint json_isjson check (json is json);
--------------------------------
Table FOO altered.
--no table alias, with constraint, borked
select json.attr1 from foo;
--------------------------------
Error starting at line : 21 in command -
select json.attr1 from foo
Error at Command Line : 21 Column : 8
Error report -
SQL Error: ORA-00904: "JSON"."ATTR1": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
--table alias and constraint, works!
select a.json.attr1 from foo a;
--------------------------------
ATTR1
--------------------------------------------------------------------------------
5
In case anyone else gets this issue, its documented in Oracle Support under note 2192052.1
Basically, it's a bug whereby Dot Notation doesn't work on a column which is created with a NOT NULL constraint, i.e.
If you do:
CREATE TABLE foo.bar (id NUMBER NOT NULL, json_doc CLOB NOT NULL CHECK (json_doc IS JSON));
you'll get the error when you run:
SELECT a.json_doc.elementName FROM foo.bar a;
but if you do:
CREATE TABLE foo.bar (id NUMBER NOT NULL, json_doc CLOB CHECK (json_doc IS JSON));
ALTER TABLE bar MODIFY (json_doc NOT NULL);
the Dot notation will work.
You do not need quotes, this shall work:
select m.Metadata.FileName from EvMetadata m
Please refer to the example of official documentation:
SELECT po.po_document.PONumber FROM j_purchaseorder po;
SELECT json_value(po_document, '$.PONumber') FROM j_purchaseorder;

error 1005 (hy000) can't create table (errno 150) in mysql

As in screenshot, both the fields: cust_id in customer table and Store_ID in ShelfLocation table have the same signature, still I am not able to reference cust_id from Store_ID.
Any help?
This solved it for me:
Add before first line:
SET FOREIGN_KEY_CHECKS = 0;
Add after last line:
SET FOREIGN_KEY_CHECKS = 1;
The solution is checking to make sure Primary_Key and Foreign_Key exactly match their data types.
If one is signed and the other is unsigned, it will fail.
Good practice is to make sure both are unsigned INT.