Truncating all tables in a Postgres database

FrustratedWithFormsDesigner is correct, PL/pgSQL can do this. Here’s the script: CREATE OR REPLACE FUNCTION truncate_tables(username IN VARCHAR) RETURNS void AS $$ DECLARE statements CURSOR FOR SELECT tablename FROM pg_tables WHERE tableowner = username AND schemaname=”public”; BEGIN FOR stmt IN statements LOOP EXECUTE ‘TRUNCATE TABLE ‘ || quote_ident(stmt.tablename) || ‘ CASCADE;’; END LOOP; END; $$ LANGUAGE … Read more

Create table variable in MySQL

They don’t exist in MySQL do they? Just use a temp table: CREATE PROCEDURE my_proc () BEGIN CREATE TEMPORARY TABLE TempTable (myid int, myfield varchar(100)); INSERT INTO TempTable SELECT tblid, tblfield FROM Table1; /* Do some more stuff …. */ From MySQL here “You can use the TEMPORARY keyword when creating a table. A TEMPORARY … Read more

How do I get list of all tables in a database using TSQL?

SQL Server 2000, 2005, 2008, 2012, 2014, 2016, 2017 or 2019: SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE=’BASE TABLE’ To show only tables from a particular database SELECT TABLE_NAME FROM [<DATABASE_NAME>].INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ‘BASE TABLE’ Or, SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ‘BASE TABLE’ AND TABLE_CATALOG=’dbName’ –(for MySql, use: TABLE_SCHEMA=’dbName’ ) PS: For SQL … Read more

How do I specify unique constraint for multiple columns in MySQL?

To add a unique constraint, you need to use two components: ALTER TABLE – to change the table schema and, ADD UNIQUE – to add the unique constraint. You then can define your new unique key with the format ‘name'(‘column1’, ‘column2’…) So for your particular issue, you could use this command: ALTER TABLE `votes` ADD … Read more

How does the search_path influence identifier resolution and the “current schema”

What is the schema search path search_path? The manual: […] tables are often referred to by unqualified names, which consist of just the table name. The system determines which table is meant by following a search path, which is a list of schemas to look in. Bold emphasis mine. This explains identifier resolution. The “current … Read more