SQLite delete with table alias [duplicate] - sql

This question already has answers here:
Sqlite delete query error
(3 answers)
Closed 9 years ago.
I am trying to alias a table in SQLite, for example by the following command: (it is from the book i am reading"Database Management Systems by Ramakrishnan")
DELETE FROM Students S WHERE S.sid=12546
This code gives a syntax error. Without aliasing, the following code works:
DELETE FROM Students WHERE sid=12546
But, if i want to alias the table, what should i do? Can anyone help?
Thanks

The DELETE statement operates on a single table and does not use a table alias. so you will have to use your query as :
DELETE FROM Students WHERE sid=12546
Update:
SQLite apparently doesn't support joins with the delete statement, as you can see on the Syntax diagrams. In short in SQLite, one DELETE command deletes from only one table. So aliasing is of no use

Related

How to retrieve table attributes in PostgreSQL? [duplicate]

This question already has answers here:
PostgreSQL "DESCRIBE TABLE"
(24 answers)
Closed 2 years ago.
So I'm very new to PostgreSQL (and SQL as a whole) and I'm trying to find a way to view all attributes of a table. For example, say you have a table called "movies", I would like to see what attributes it has (e.g. movie_name, release_year).
I'm sure there is a simple command for it, but I couldn't find it.
Thanks in advance for any help!
Assuming that by attributes you mean columns, a standard approach uses system view information_schema.columns:
select column_name, datatype
from information_schema.columns
where table_name = 'mytable'
From a PSQL command line, you can also use \d (which stands for describe):
# \d mytable

What is the Postgresql SQL query analogous to 'USE database_name' from MySQL? [duplicate]

This question already has answers here:
How to switch databases in psql?
(15 answers)
Closed 3 years ago.
I have to execute a script from Java code which should create a database and then a table in that database. How do I do it in PostgreSQL?
CREATE DATABASE mydb
USE mydb // doesn't work for PostgreSQL
CREATE TABLE mytable (.....)
What SQL do we give for Step 2 in PosgreSQL?
Version : 9.5
Almost this question has been asked before (Difference Between Schema / Database in MySQL)
This has a reference to chapter 5.8 'Schemas': https://www.postgresql.org/docs/current/ddl-schemas.html
If your answer was 'how are schemas implemented under PostgreSQL', than this might be the correct answer. I think you cannot use use .., and you have to use mydb.mytable. (or set a search pattern SET search_path TO mydb,public;)

Copy all columns without data but with dependencies in SQL [duplicate]

This question already has answers here:
In SQL Server, how do I generate a CREATE TABLE statement for a given table?
(16 answers)
Closed 5 years ago.
So i'm trying to copy all data of table CarOrders into a new table, NewCarOrders. But i only want to copy columns and dependencies and not data. I don't know if it truly matters, but i'm using SQL Server Management Studio.
Here are two things that i tried, but none worked.
SELECT *
INTO NewCarOrders
FROM CarOrders
WHERE 0 = 1
The issue with above is, it is copying all the columns but it is not copying the dependencies/relationships.
I saw some posts on some of the forums regarding this, but the suggested solution was using SSMS Wizard. I want to do it using SQL (or T-SQL).
I'm new to SQL world, so i'm really sorry if it is a stupid or no brainer question. Let me know if i am missing some important information, i'll update it.
Try below query and check if this works for you
SELECT TOP 0 *
INTO NewCarOrders
FROM CarOrders
This will create NewCarOrders table with same structure as CarOrders table and no rows in NewCarOrders.
SELECT * FROM NewCarOrders -- Returns zero rows
Note : This will not copy constraints , only structure is copied.
For constraints do as below -
In SSMS right click on the table CarOrders, Script Table As > Create To > New Query Window.
Change the name in the generated script to NewCarOrders and execute.
Also change the constraints name in the generated script else it will throw error like There is already an object named 'xyz' in the database
You can use the like command:
CREATE TABLE NewCarOrders LIKE CarOrders;

SQLite: How to add a column to all the tables in the database?

I am quite new to SQLite (SQL overall). I created a database and created multiple tables in it and suddenly realized I had forgot to add a column to them. Now I have 280 tables having distinct names without a column. I have tried adding * and combining with SELECT but it gives an error. How can I accomplish it?
my code:
ALTER TABLE "MYBUS.*" ADD COLUMN STOPID int null;
After 3 hours issue finally solved.
After searching for sometime, I stumbled upon this answer
SQlite alter table with result from select statement
I ran the code:select 'alter table '||tbl_name||' add STOPID int null' from sqlite_master
It returned all the 280 SQL queries needed but with quotation marks, so I took the clue from this.lau and created a python regex script and ran it. Worked like a charm and within 10 lines of code I was able to add a column to all my 280 tables.
See this link https://dba.stackexchange.com/questions/160145/add-columns-to-all-tables-in-a-database-if-the-columns-dont-exist
Even though it applies to SQL Server, this should help you in getting started.
Ensure that you perform a check on the column name before adding it to a table.

Ignore errors in mysql by reading from some MySQL dump [duplicate]

This question already has answers here:
Ignore mysql error messages when executing an sql file
(2 answers)
Closed 8 years ago.
I got an MySQL dump with ONLY insert statements. I tried to import it like this:
mysql> \. racktablesTabellen.sql
which always worked for me, till now because I got some errors I have to ignore. Can anybody tell me how to Ignore errors and just keep on with the entries when I got an sql dump with only insert statements ?
A possible solution is to remove the PRIMARY KEY from your table, import and add the PRIAMRY KEY again with ALTER IGNORE syntax.