Postgres: Inserting rows into table with foreign key - sql

I have a table with a foreign key type which links to table wp_types. Whenever I try and insert a row into that table I get the error:
ERROR: permission denied for schema base
LINE 1: SELECT 1 FROM ONLY "base"."wp_types" x WHERE "id" OPERATOR(p...
^
QUERY: SELECT 1 FROM ONLY "base"."wp_types" x WHERE "id" OPERATOR(pg_catalog.=) $1 FOR SHARE OF x
The query is being executed as postgres.
Also I do not understand what this query is trying to do, it is clearly linked with checking the foreign key constraints but I don't understand how.

From postgresql docs:
If ONLY is specified, only that table is scanned. If ONLY is not specified, the table and all its descendant tables (if any) are scanned.
As you mentioned error is connected with checking foreign keys. When ONLY is specified postgres can not check them and it may cause an error.

Two points:
Is it possible you dropped the superuser permissions from the postgres user?
What are the permissions for the base schema?
Now one thing to consider is that you aren't sure what the query is doing. A simple EXPLAIN can show you what it is doing, but the error suggests the problem is with schema permissions, not with what the query is doing.
Try:
GRANT USAGE ON SCHEMA base TO postgres;

Related

How to validate all constraints in a MariaDB database for existing data after import?

I imported data into a MariaDB database while foreign key constraint checks were disabled. Those checks were enabled afterwards. However, some data in the database seems to be corrupt in the sense that the foreign key constraints are not satisfied.
I tried to use the mysqlcheck tool to verify all tables, but this does not produce any error output. Also running CHECK TABLE someTable does not show any errors or warnings.
Still I can manually show data not being correct, for example by performing a query:
select count(*) from some_table where id not in (select id from related_table)
where some_table has a foreign key constraint to related_table by id.
How do I fix this?

postgreSQL returns blank

I'm trying to make a foreign key. When I execute the query below, it returns blank with neither an explanation nor an error.
alter table MySQL."GrossHomeSales"
add constraint fk_zip_code
foreign key (nhs_prev_zip) references MySQL."Location" (zip_code);
You have not mentioned what your sql client is. If you are using psql, it does say ALTER TABLE when you make an alteration to a table. If instead you were using pgadmin 3, it would say something like
Query returned successfully with no result in 345 msec.
The reason that you are not seeing any output could be because this is really large table with several million rows, Then it could take a few minutes to create the index so don't expect instant output.
The other likely reason for not receiving any immediate response is because the table has been locked by another long running query.
Finally your question title says postgresql but your table has mysql in it's name?? (this is not related, just curious)

How to validate data in sql server?

I have an issue related to data in sql server. In my database some of the constraint were not enabled i.e. they were not checked , After some time working on it we found this issue that a parent rows can be deleted without deleting child, which was an issue. I enabled all the constraint in the database using query
ALTER TABLE tbl_name CHECK CONSTRAINT ALL
above query was executed on all the tables of that database without any error . But my concern is whether it will work or not , if it will work on the existing data then what will happen to that data whose parent table data has been deleted.
I want to know is there any way such that I can validate such data data whose parent record doesn't exist in the entire database. There are about 270 constraint containing FOREIGN KEY AND UNIQUE KEY . I don't want to go for manual option.
Please help me out.
ALTER TABLE tbl_name CHECK CONSTRAINT ALL
only re-enables the constraints. Importantly, the constraints are not checked against the existing data in the database (nor are they trusted by the optimizer). If you want that to occur, you need to specify WITH CHECK as well:
ALTER TABLE tbl_name WITH CHECK CHECK CONSTRAINT ALL
(And yes, the word CHECK appears twice)
If you execute this, and there are orphaned child rows (or other invalid constraints), then the ALTER TABLE will fail with an error message. There's nothing SQL Server can do to fix this issue - it's for you to decide whether to a) remove the orphaned rows, or b) to re-create, in some manner, a suitable parent row for them.
You can also add the 'ON DELETE CASCADE' code to the end of foreign keys to prevent orphaned child rows from persisting.
This is more of a 'better practice' going forward than a solution, but I believe Damien_The_Unbeliever has answered your main question.

How to create primary key for a view in sql server?

Suppose I have two database DB1, and DB2 both under the same instance.
There is a table tab2 in DB2.
I created a view in DB1 to get tab2 from DB2:
CREATE VIEW [dbo].[Tab2]
AS
SELECT *
FROM DB2.dbo.Tab2
Then I tried to create a key for tab2 in DB1:
CREATE UNIQUE CLUSTERED INDEX tab2_Key
ON dbo. tab2 (id2)
This throws the following error:
Msg 1939, Level 16, State 1, Line 1 Cannot create index on view
'Tab2' because the view is not schema bound.
How can I resolve this problem?
Well, there are several rules for a view having an index (some cascade from rules required for schemabinding).
One of the rules is that the view can't contain SELECT *. Another is that it has to exist in the same database as the object(s) it references.
I could list out the rules for you, but they are listed in the docs here and here. And I don't think telling you the rules will accomplish much anyway.
Can you explain exactly what benefit you think a clustered index on this view would provide? Did someone tell you that an indexed view is "faster"? In this case I don't see what it will do for queries against DB2.dbo.Tab2 especially if that table already has an index on id2. This just smells wrong in several ways...

RedBean: How to delete all rows from all tables

I am using RedBean ORM. In order to create the schema I used the standard redbean approach of inserting data so that Redbean would auto-fit the schema to suit my needs. I put this in a script which would basically be used to build the schema when i need to initialize my database.
The problem is that RedBean keeps a row or 2 in each table (the ones that I initially inserted to get redbean to build the schema).
If it were a normal database, to erase all rows i would just drop the schema and rebuild it, but in this case that's not possible since the initial rows would still exist.
Unfortunately there isn't too much Redbean Q/A out there. Anyone know how I would do this using the Redbean interface?
I have tried
$listOfTables = R::$writer->getTables();
foreach($listOfTables as $table)
{
R::wipe($table);
}
Of course this doesn't work though. (It doesn't TRUNCATE the tables in the correct order so I get an error about another table using this key as a foreign link. It simply iterates in ABC order)
Fatal error: Uncaught [42000] - SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint (`redbeandb`.`research`, CONSTRAINT `research_ibfk_1` FOREIGN KEY (`ownEducationHistory_id`) REFERENCES `redbeandb`.`educationhistory` (`id`)) thrown in C:\Users\Rod\nginx-1.0.12\html\rb.php on line 105
If someone has a (redbean api) solution, it would be much appreciated. And hopefully this question can be beneficial to building up more RedBean Q/A here on Stackoverflow.
Use
R::nuke();
Yes, it will drop all tables but since RedBeanPHP creates all tables on the fly this is not a problem.
I know this is an old post but I figured I'd help out someone finding this today. You can tell mysql to ignore foreign key checks if you don't care about data integrity (plan on wiping all related tables).
R::exec('SET FOREIGN_KEY_CHECKS = 0;');
$listOfTables = R::$writer->getTables();
foreach($listOfTables as $table)
{
R::wipe($table);
}
R::exec('SET FOREIGN_KEY_CHECKS = 1;');