WebSQL create view - sql

Is it possible to use views with WebSQL? I assume it is because it is basically sqlite which definitely allows it, but I might as well be wrong. I am trying to create a view in my local sqlite database but it fails no matter what I do. Here are some of the things I've tried so far;
create view my_view as select id, name from my_table where active = 1;
--fails: could not prepare statement (1 no such table: main.my_table)
Why does it search for table 'my_table' in the 'main' database? I was not even aware of the 'main' database. I assume it is some system database that is there by default. The database that I open is labeled 'my_database'. All the other sql statements seem to respect that. The 'create view' is the only one that looks like it operates outside of the opened database.
create view my_view as select id, name from my_database.my_table where active = 1;
--fails: could not prepare statement (1 view my_view cannot reference objects in database my_database)
This is what specifically makes me believe that the 'create view' statement operates outside of the opened database.
create view my_database.my_view as select id, name from my_database.my_table where active = 1;
--fails: could not prepare statement (1 unknown database my_database)
This one confuses me the most. How can there be that the 'my_database' database is unknown? I can assure you that I am using this database for quite a bunch of other statements such as select, insert, update, delete and all of them work perfectly fine. It is only for the 'create view' that this database is unknown.
create view my_database.my_view as select id, name from my_table where active = 1;
--fails: could not prepare statement (1 unknown database my_database)
I even tried to run these one at a time before the 'create view':
attach 'my_database.db' as my_database;
--fails: could not prepare statement (23 not authorized)
attach 'my_database.sqlite' as my_database;
--fails: could not prepare statement (23 not authorized)
Any help is greatly appreciated. Thanks.

The default database (the database you opened) is always called main.
You get the error "no such table: main.my_table" because your database does not contain a table called my_table.

Related

If not exist clause SQL statement

so I found this sql query in a project I am succeeding. This is the first time I encountering this clause/statement. I understand that this is to look if the table exist before creating one and that Object_ID is the table name that is to be created.
My questions are:
Does sysobject mean the database?
What is the Object property?
I know that it is not the columns inside the table to be created.
The columns are : dtb_color_id and description.
can someone explain this to me. please?
IF NOT EXISTS(SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('DTB_COLOR') AND OBJECTPROPERTY(ID,'ISUserTable') = 1)
BEGIN
.......some query I understand
END
sysobjects, OBJECTPROPERTY and OBJECT_ID are used in Microsoft SQL Server. They are part of the SQL Server DMVs and system functions/procedures used to query and manipulate the metadata.
sys.sysobjects is simply the list of all objects (tables, views, SPs, functions, etc) on the server in the active database. Please note, that sys.sysobjects is deprecated and is only available for backward compatibility. Use sys.objects instead
https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/system-dynamic-management-views?view=sql-server-ver16
It has (as far as I know) no meaning in MySQL, unless somebody specifically created them.
You can also use INFORMATION_SCHEMA which is available in MySQL too (however slightly different in different RDBMS).
MSSQL INFORMATION_SCHEMA: https://learn.microsoft.com/en-us/sql/relational-databases/system-information-schema-views/system-information-schema-views-transact-sql?view=sql-server-ver16
MySQL INFORMATION_SCHEMA: https://dev.mysql.com/doc/refman/8.0/en/information-schema.html
SQL Server has no CREATE TABLE IF NOT EXISTS construct, a variation of the mentioned condition is commonly used to imitate that.
This is a way in SQL Server to check if a table exists in the active database and to perform actions according to the result, like creating the table.
OBJECTPROPERTY simply checks (in this case) if the table is a user created one.
https://learn.microsoft.com/en-us/sql/t-sql/functions/objectproperty-transact-sql?view=sql-server-ver16
I would remove the OBJECTPROPERTY condition in case the part you understand is a CREATE TABLE statement. You don't want to create a table which has a similar name to any system table/view, also you don't want to execute the CREATE TABLE if there is a VIEW with the same name (table creation will fail)
Yes sysobject means database.
The OBJECTPROPERTY() function returns information about schema-
scoped objects in the current database. Use this to check if an
object is a table, view, stored procedure, etc. You can also use
it to check if a table has a primary key, foreign key, foreign
key reference, etc.
For more details : https://learn.microsoft.com/en-us/sql/t-sql/functions/objectpropertyex-transact-sql?view=sql-server-ver16
In this scenario it is used to check whether it is user table or
not. The result of the ISUserTable property is 1 when it is user
table otherwise returns 0.
Here the following steps are followed:
First, it executes the select statement inside the IF Exists
If the select statement returns a value that condition is TRUE for IF Exists
It starts the code inside a begin statement
DTB_COLOR - May be a stored procedure

What does double dots .. mean in SQL Server?

I am using SQL Server. I found the following way to backup a database table:
-- Taking a backup
SELECT * INTO MY_BACKUP_DATABASE..CustomersTemporaryTable FROM Customers
I am trying to understand the .. in the syntax. From what I understand, the sentence means that Customers is the table that is going to be backed-up by placing it all of its content into the database called MY_BACKUP_DATABASE using CustomersTemporaryTable as the destination table. I assume when executing the sentence, CustomersTemporaryTable must already exist. Is my understanding of the sentence to take a backup correct?
Each MS SQL Table identifiers can have a name compound of three parts separates with a dot :
the database name
the SQL schema name (by default dbo)
the table, view or Table UDF name
Syntax :
db_name.schema_name.table_name
But it is not always necessary to specify the three parts.
Inside the current database, no need to specify the db_name. It's implicit...
By default every SQL user is associate with a specific default schema (frequently dbo too...).
So you can specify a table name with :
schema_name.table_name
...SQL Server will try to find the table into the current DB
db_name..table_name
...SQL Server will try to find the table into the specified DB and the default user schema
table_name
...SQL Server will try to find the table into the current DB and the
default user schema
To know with SQL schema is associated with your SQL user, use :
SELECT SCHEMA_NAME() AS DEFAULT_CURRENT_USER_SCHEMA
To know all the associations between SQL users and SQL schemas, do :
SELECT name AS USER_NAME, default_schema_name
FROM sys.database_principals
WHERE type_desc LIKE '%?_USER' ESCAPE '?'
First of all, understand that what you are doing is not "taking a backup", it is inserting data into a table from another table. If you have not created the destination table the syntax is like this:
Select *
INTO Destination_Table
FROM Source_Table
The destination table will be created automatically. This doesn't necessarily work so well if you will be inserting additional data that might be different lengths or data types, but for a one of select should work fine.

How to list ONLY the tables I've created in Oracle SQL?

I am a beginner using SQL Plus, so this might sound like a stupid question but I really don't know how to do it. I cannot find a way to view all the tables I've created. I try a large number of commands like
SELECT owner, table_name
FROM dba_tables`
or
SELECT table_name
FROM user_tables
But i always end with a few thousand results.
Pic after executing the first command
SELECT owner, table_name
FROM dba_tables
Basically i created about 15 tables since i started using SQL Plus, and i forgot some of the names. I would like to have a way to check just the tables I created, without having a few thousand unnecessary other names in there.
Does anybody know how to do that? I will add that i'm using the SYSTEM user and oracle express.
First of all, you should stop using the SYSTEM account. That is for the Oracle database only. Create and use a new database user and you will save yourself many headaches worse than your current one.
To solve your current problem, you want to filter out the "thousands of tables" that are owned by SYSTEM out of the box. (I'm taking your word it's thousands -- I have never counted them).
One idea would be this:
SELECT object_name
FROM dba_objects
WHERE object_type = 'TABLE'
AND owner = 'SYSTEM'
AND created >= to_date('01-JAN-2018','DD-MON-YYYY')
... replacing 01-JAN-2018 with the date when you started using SQL*Plus, which hopefully wasn't the same day that the database was installed (or else this might not work well)!
What #matthew-mcpeak said above - QUIT USING SYSTEM.
After you've created a proper schema to contain your objects, you can query USER_TABLES. No WHERE clause is necessary: this is a VIEW of the TABLES in the schema belonging to your SESSION user account.
So login as PETE, query
SELECT * FROM USER_TABLES
Will show every table in the PETE schema.
or
select *
from ALL_OBJECTS
where OBJECT_TYPE = 'TABLE'
and OWNER = 'PETE';
or query
SELECT * FROM ALL_TABLES where owner = 'PETE'
We also have DBA_ views, such as DBA_TABLES and DBA_OBJECTS. They are faster than the ALL_ views - because there is no security model in place. ALL_ views only show you what you are able to see. So if you have access to DBA_ views, use them.
But if you just want to see what's in your schema, the USER_ views are just dandy.
However.
This won't show you every table you've created. For example, what about all those tables you polluted the SYSTEM schema with?
The Oracle Data Dictionary doesn't keep track of who created what. You would need a AFTER CREATE TRIGGER to capture the session owner any time a CREATE TABLE was fired and log that to a new table for reporting. Or enable auditing on your system and let that take care of it. ORACLE-BASE (Tim) demonstrates that here.
If you do know any of the tables you have created, make a query like this:
select owner,table_name from user_tables where table_name='NAME OF YOUR TABLE';
Then, you can change the query to:
select owner, table_name from user_tables where ower='THE OWNER OF THE TABLE YOU SELECTED BEFORE'
This should return only the tables created by you.
owner might be your username, but, I'm not sure since I have no experience in Oracle, nor ever tried anything of the sort, but, seems logical to me that this should work.
Since Oracle 12g, tables maintained by Oracle can be ignored by the following query:
SELECT *
FROM sys.user_tables
LEFT JOIN sys.user_objects ON user_objects.object_type = 'TABLE'
AND user_objects.object_name = user_tables.table_name
WHERE user_objects.oracle_maintained != 'Y'

Deleting objects from SQL Server

In SQL Server Database Engine I have a table named Table A.
I deleted the table using graphical interface, but when I wanted to create a table with same name, the error shows
The object already exists
What is the remedy of this situation?
The following steps should help you track down what is going on and help you create your table:
Right-click on your database and select refresh
Verify that your table does not exist under this database.
If you table is
not shown here, then very likely your table is displayed under the
master database.
To create a table in your selected database,
first select the database and then run your query.
A better
option for number 4, just to be sure you are specifying the correct
database is to run the command use dbname; (where dbname is
the name of your database). Do this on the line above your create table code.

Copy tables from one database to another in SQL Server

I have a database called foo and a database called bar. I have a table in foo called tblFoobar that I want to move (data and all) to database bar from database foo. What is the SQL statement to do this?
SQL Server Management Studio's "Import Data" task (right-click on the DB name, then tasks) will do most of this for you. Run it from the database you want to copy the data into.
If the tables don't exist it will create them for you, but you'll probably have to recreate any indexes and such. If the tables do exist, it will append the new data by default but you can adjust that (edit mappings) so it will delete all existing data.
I use this all the time and it works fairly well.
On SQL Server? and on the same database server? Use three part naming.
INSERT INTO bar..tblFoobar( *fieldlist* )
SELECT *fieldlist* FROM foo..tblFoobar
This just moves the data. If you want to move the table definition (and other attributes such as permissions and indexes), you'll have to do something else.
This should work:
SELECT *
INTO DestinationDB..MyDestinationTable
FROM SourceDB..MySourceTable
It will not copy constraints, defaults or indexes. The table created will not have a clustered index.
Alternatively you could:
INSERT INTO DestinationDB..MyDestinationTable
SELECT * FROM SourceDB..MySourceTable
If your destination table exists and is empty.
If it’s one table only then all you need to do is
Script table definition
Create new table in another database
Update rules, indexes, permissions and such
Import data (several insert into examples are already shown above)
One thing you’ll have to consider is other updates such as migrating other objects in the future. Note that your source and destination tables do not have the same name. This means that you’ll also have to make changes if you dependent objects such as views, stored procedures and other.
Whit one or several objects you can go manually w/o any issues. However, when there are more than just a few updates 3rd party comparison tools come in very handy. Right now I’m using ApexSQL Diff for schema migrations but you can’t go wrong with any other tool out there.
Script the create table in management studio, run that script in bar to create the table. (Right click table in object explorer, script table as, create to...)
INSERT bar.[schema].table SELECT * FROM foo.[schema].table
You can also use the Generate SQL Server Scripts Wizard to help guide the creation of SQL script's that can do the following:
copy the table schema
any constraints (identity, default values, etc)
data within the table
and many other options if needed
Good example workflow for SQL Server 2008 with screen shots shown here.
You may go with this way: ( a general example )
insert into QualityAssuranceDB.dbo.Customers (columnA, ColumnB)
Select columnA, columnB from DeveloperDB.dbo.Customers
Also if you need to generate the column names as well to put in insert clause, use:
select (name + ',') as TableColumns from sys.columns
where object_id = object_id('YourTableName')
Copy the result and paste into query window to represent your table column names and even this will exclude the identity column as well:
select (name + ',') as TableColumns from sys.columns
where object_id = object_id('YourTableName') and is_identity = 0
Remember the script to copy rows will work if the databases belongs to the same location.
You can Try This.
select * into <Destination_table> from <Servername>.<DatabaseName>.dbo.<sourceTable>
Server name is optional if both DB is in same server.
I give you three options:
If they are two databases on the same instance do:
SELECT * INTO My_New_Table FROM [HumanResources].[Department];
If they are two databases on different servers and you have linked servers do:
SELECT * INTO My_New_Table FROM [ServerName].[AdventureWorks2012].[HumanResources].[Department];
If they are two databases on different servers and you don't have linked servers do:
SELECT * INTO My_New_Table
FROM OPENROWSET('SQLNCLI', 'Server=My_Remote_Server;Trusted_Connection=yes;',
'SELECT * FROM AdventureWorks2012.HumanResources.Department');