MariaDB is showing data only when I select all fields - mariadb

I have strange error (?) while using MariaDB command line.
When I use SELECT * FROM users; where I have current_role column (which is enum type) it shows it's value, but when I use SELECT current_role FROM users; I only get NULL. How is that possible and what I may be doing wrong? I don't have that issue with other fields.

current_role is a keywaord und you must escape to get the info from a field with the same name.
see: https://mariadb.com/kb/en/library/current_role/
Use backticks to escape the fieldname like:
SELECT `id`,`current_role` FROM YourTable;
MariaDB [(none)]> SELECT CURRENT_ROLE;
+--------------+
| CURRENT_ROLE |
+--------------+
| NULL |
+--------------+
1 row in set (0.00 sec)

Related

How to fix "UNIQUE constraint failed" from VACUUM (also INTEGRITY_CHECK fails)

I use an app which creates this SQLite DB with this table:
CREATE TABLE expense_report (_id INTEGER PRIMARY KEY, ...)
And for some reason that _id (which is the ROWID) became invalid in that DB.
When I scan the table I see that the last rows got an _id which was already being used long ago:
1,2,3...,1137,1138,...,1147,1149,...,12263,12264,1138,...,1148
I highlighted above the ranges in which I see that I have the same _id for completely different rows (the rest of the values do not match at all).
And querying this DB usually gets me inaccurate results due to that. For instance:
SELECT
(SELECT MAX(_ID) FROM expense_report) DirectMax
, (SELECT MAX(_ID) FROM (SELECT _ID FROM expense_report ORDER BY _ID DESC)) RealMax;
| DirectMax | RealMax |
| 1148 | 12264 |
And inserting a new row into this table via DB Browser for SQLite also generates an _id of 1149 (instead of 12265), so the problem becomes worse if I keep using this DB.
Running PRAGMA QUICK_CHECK or PRAGMA INTEGRITY_CHECK show this error response:
*** in database main ***
On page 1598 at right child: Rowid 12268 out of order
And running VACUUM also detects the problem but doesn't seem to be able to fix it:
Execution finished with errors.
Result: UNIQUE constraint failed: expense_report._id
Anyone knows a way to fix these duplicate ROWID values?

How could you get if a table is autoincrement or not from the metadata of an sqlite table?

In case you have several tables inside any sqlite database how could the get the information that they have an auto increment primary key or not?
For instance I am already aware that you could get some info concerning the columns of a table by simply querying this: pragma table_info(tablename_in_here)
It would be much better to get the auto increment column dynamically rather than setting up each corresponding model inside the source code with a boolean value.
Edit:
Let me use this table as an example:
CREATE TABLE "test" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"name" TEXT NOT NULL
)
and this is the result table after executing pragma table_info("test")
cid | name | type | notnull | dflt_value | pk
0 | id | INTEGER | 1 | null | 1
1 | name | TEXT | 1 | null | 0
As you can see there is no information whether the id column is autoincrement or not
Edit2:
I looking for a solution that involves sqlite directly through a statement.
Special situations where the sqlite3 command in the terminal can be used to somehow parse the required information from inside are not acceptable. They do not work in situations where you are not allowed to execute commands in a terminal programmatically. Like in an Android app.
Autoincrementing primary keys must be declared as INTEGER PRIMARY KEY or some equivalent, so you can use the table_info date to detect them.
A column is an INTEGER PRIMARY KEY column if, in the PRAGMA table_info output,
the type is integer or INTEGER or any other case-insensitive variant; and
pk is set; and
pk is not set for any other column.
To check whether the column definition includes the AUTOINCREMENT keyword, you have to look directly into the sqlite_master table; SQLite has no other mechanism to access this information.
If this query returns a record, you have the AUTOINCREMENT keyword somewhere in the table definition (which might return a wrong result if this word is commented out):
SELECT 1
FROM sqlite_master
WHERE type = 'table'
AND name = 'tablename_in_here'
AND sql LIKE '%AUTOINCREMENT%'
You can parse the output of .schema. That will give you the sql commands as you used them to create your tables. If autoincrement was declared, you will see it in the output. This has the advantage that it will list all your tables too.

Oracle: Custome auto-increment: C0001 C0002

i am trying to create a table in Oracle sql.
I have no trouble creating the table and the sequence i created works fine.
Right now i can use form to dial in name and email and the table will create the PK for me.
The PK ID would looked like this:
10001 | xxx | xxxx
10002 | xxx | xxxx
10003 | xxx | xxxx
My question is, how can i use function or trigger to auto update my ID, make it into:
QWER10001 | xxx | xxxx
QWER10002 | xxx | xxxx
QWER10003 | xxx | xxxx
I know how to update it manually, but i want it can put that custom text in front of my ID when i put in information.
the text can be set, no need to change it when the number reach max.
I spend hours looking for solution but can't find the right one, please help!
I also did some more research online, right now i have this:
CREATE OR REPLACE TRIGGER "PROFILE_T1"
BEFORE INSERT OR UPDATE
ON "PROFILE"
FOR EACH ROW
BEGIN
:NEW.PROFILEID := 'WCCU' sequence.PROFILEIDUSE
END;
but this did not work. PROFILEID is the PK and is auto incremented. PROFILEIDUSE is the sequence to make it auto increment. WCCU is the text i want to add to the PK when every data is entered.
create or replace trigger "PROFILE_T1"
BEFORE
insert on "PROFILE"
for each row
begin
:new.PROFILEID := 'WCCU' || PROFILEIDUSE.nextval;
end;
Since you have already created the sequence, you simply have to use it in your INSERT statement where you should explicitly specify the column value. If you don't want to explicitly specify the value in the insert statement, you can use a before inert trigger to set the value for the column.
In INSERT statement
insert into your_table values('QWER'||your_sequence.nextval,....);
Using TRIGGER
CREATE OR REPLACE TRIGGER your_trigger
BEFORE INSERT ON your_table
FOR EACH ROW
DECLARE
BEGIN
:new.your_primary_key_column := 'QWER' || your_sequence.nextval;
END your_trigger;
Now, you can exclude this primary column from your insert statement. Trigger will add it for you.

Insert only if id doesn't exist

Alright, so we have a phonegap app with a table setup like
tblTest (actualid INTEGER PRIMARY KEY, id INTEGER, name TEXT)
The actualid is a unique id for the device and the id is maintained in a server side database. We've had issues with webservices returning duplicate records and while we're fixing that I wanted to add something to our sql that would prevent duplicates from being added (fixing bad data is a pain).
Our current insert statement is setup like
INSERT INTO tblTest (id, name) VALUES (101, 'Bob')
If you run that twice the database will end up looking like
actualid | id| name
1 | 101| Bob
2 | 101| Bob
And what I'd like for it to look like is
actualid | id| name
1 | 101| Bob
Insert or Replace would give me an actualid 2 in the example and any example I've found using a where clause was setup like
INSERT INTO tblTest SELECT ..... WHERE.....
Which doesn't work because none of the data is in a table yet (unless I'm making a newbie mistake, I'm not very good at sqlite or general sql).
Use INSERT OR IGNORE:
INSERT OR IGNORE INTO tblTest (id, name) VALUES (101, 'Bob')
(This requires a unique index on the id column, which you already have.)
You might want to try this:
INSERT INTO tblTest
(id, name)
SELECT 101 as id, 'Bob' as name
FROM tblTest
WHERE NOT EXISTS(SELECT * FROM tblTest WHERE id = 101 and name = 'Bob')

.schema for postgres

I'm migrating a database from sqlite3 to postgres and am wondering if there are any short tutorials that can teach me the new syntax.
Also, as a short term question, how do I see the schema of a postgres table which is equivalent to .schema in sqlite?
You could use pg_dump command line utility, i.e.:
pg_dump --table <table_name> --schema-only <database_name>
Depending on your environment you probably need to specify connection options (-h, -p, -U switches).
You could use \d from within psql:
=> \?
...
Informational
(options: S = show system objects, + = additional detail)
\d[S+] list tables, views, and sequences
\d[S+] NAME describe table, view, sequence, or index
...
=> \d people
Table "public.people"
Column | Type | Modifiers
------------------------+-----------------------------+-----------------------------------------------------
id | integer | not null default nextval('people_id_seq'::regclass)
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
...
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
...
Check constraints:
"chk_people_latlng" CHECK ((lat IS NULL) = (lng IS NULL))
....
You can also root around in the information_schema if you're not inside psql.
If you are using psql (and \d... ) then you can
\set ECHO_HIDDEN
to see the sql for the queries that psql is executing to put together the \d... output-- this is useful not only as sql syntax examples but it also shows you where find, and how to connect, the database metadata.
To get the schema name for a table you can:
SELECT n.nspname AS schema_name,
c.relname AS table_name
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname = '<table_name>'
;
(don't know how that compares to .schema)
Maybe you can use a PostgreSQL Cheat Sheet:
http://www.postgresonline.com/special_feature.php?sf_name=postgresql83_cheatsheet&outputformat=html

Resources