SQL Server : creating a table - sql

When creating a table in SQL Server, it is created using dbo.<tableName> format.
I want to change dbo.tableName to source.tableName, as I want to import data into a source table and then cook that data.
Thanks

You are talking about schemas. If the schema source doesn't exist yet, you need to run create schema source. Once the schema exists it's as easy as create table source.tableName (...).

Related

How to rename a table in Athena?

probably a very trivial question but I'm not sure about this and also don't want to lose the table. How do I rename a table in Athena?
Database name - friends
table name - centralPark
desired table name -centralPerk
you can't!
see the list of unsupported DDL in Athena.
what you can do is to make a new table using select:
CREATE TABLE centralPark
AS SELECT * FROM centralPerk
WITH DATA
and drop the old table:
DROP TABLE IF EXISTS centralPerk
Using a CTAS query is effective, but I found it to be quite slow. It needs to copy all the files.
But you don't need to copy the files. You can create a new table directly in the Glue catalog and point it at the existing files. This works in seconds or less.
If you're using Python, I highly recommend the awswrangler library for this kind of work.
import awswrangler as wr
def wrangler_copy(db, old_name, new_name):
wr.catalog.create_parquet_table(
db,
new_name,
path=wr.catalog.get_table_location(db, old_name),
columns_types=wr.catalog.get_table_types(db, old_name),
# TODO: partitions, etc
)
And then drop the old table if you like.
DROP TABLE IF EXISTS <old_name>

How to rename a database in azure databricks?

I am trying to rename a database in azure databricks but I am getting the following error:
no viable alternative at input 'ALTER DATABASE inventory
Below is code:
%sql
use inventory;
ALTER DATABASE inventory MODIFY NAME = new_inventory;
Please explain what is meant by this error "no viable alternative at input 'ALTER DATABASE inventory"
and how can I solve it
It's not possible to rename database on Databricks. If you go to the documentation, then you will see that you can only set DBPROPERTIES.
If you really need to rename database, then you have 2 choices:
if you have unmanaged tables (not created via saveAsTable, etc.), then you can produce SQL using SHOW CREATE TABLE, drop your database (be careful anyway), and recreate all tables from saved SQL
if you have managed tables, then the solution would be to create new database, and either use CLONE (only for Delta tables), or CREATE TABLE ... AS SELECT for other file types, and after that drop your database
Alex Ott's answer, to use Clone, is OK if you do not need to maintain the versioning history of your database when you rename it.
However if you wish to time travel on the database of Delta tables after the renaming, this solution works:
Create your new database, specifying its location
Move the file system from the old location to the new location
For each table on the old database, create a table on the new database, based on the location (my code relies on the standard file structure of {database name}/{table name} being observed). No need to specify schema as it's just taken from the files in place
Drop old database
You will then be left with a database with your new name, that has all of the data and all of the history of your old database, i.e. a renamed database of Delta tables.
Pyspark method (on databricks, with "spark" and "dbutils" already defined by default) :
def rename_db(original_db_name, original_db_location, new_db_name, new_db_location):
spark.sql(f"create database if not exists {new_db_name} location '{new_db_location}'")
dbutils.fs.mv(original_db_location,new_db_location,True)
for table in list(map(lambda x: x.tableName, spark.sql(f"SHOW TABLES FROM {original_db_name}").select("tableName").collect())):
spark.sql(f"create table {new_db_name}.{table} location '{new_db_location}/{table}'")
spark.sql(f"drop database {original_db_name} cascade")
return spark.sql(f"SHOW TABLES FROM {new_db_name}")

Is there a way to create contents of the schema into a Table in BigQuery?

Is there a way to create the contents of the BigQuery schema into a Table?
I don't want to create a table from the schema instead I want to move the contents of the schema into a BigQuery Table. I couldn't find any trivial method to do this.
Currently I download the table schema as JSON and then create a new table from it
This worked for me!
SELECT *
FROM
mydataset.INFORMATION_SCHEMA.COLUMN_FIELD_PATHS

Creating DB Schema from sqlite in sqlite manager

I have a sqlite database in sqlite manager. I would like to create DB schema. How can i do that? Or is it best to use some software to create the DB Schema?
Need some suggestions and guidance.
there is a system table called sqlite_master. It contains the SQL CREATE statements for all objects.
So, all you need to do is to select all rows from this table, and run the sql statements.
You can also use Database->Export Database Structure, which will effectiveley do the same thing
there is only one database in SQLite for file.
probably you can create a new file and open that with sqlite manager.
certainly you can
go to Database menu + create new Database
http://code.google.com/p/sqlite-manager/wiki/CommonTasks

Copy table from one database to another

I've just created an empty database on my machine. I now wish to copy a table from our server database to this local database.
What sql commands do I need to run to do this? I wish to create the new table, copy data from the old table and insert it into the new table.
Create a linked server to the source server. The easiest way is to right click "Linked Servers" in Management Studio; it's under Management -> Server Objects.
Then you can copy the table using a 4-part name, server.database.schema.table:
select *
into DbName.dbo.NewTable
from LinkedServer.DbName.dbo.OldTable
This will both create the new table with the same structure as the original one and copy the data over.
Assuming that they are in the same server, try this:
SELECT *
INTO SecondDB.TableName
FROM FirstDatabase.TableName
This will create a new table and just copy the data from FirstDatabase.TableName to SecondDB.TableName and won't create foreign keys or indexes.
Another method that can be used to copy tables from the source database to the destination one is the SQL Server Export and Import wizard, which is available in SQL Server Management Studio.
You have the choice to export from the source database or import from the destination one in order to transfer the data.
This method is a quick way to copy tables from the source database to the destination one, if you arrange to copy tables having no concern with the tables’ relationships and orders.
When using this method, the tables’ indexes and keys will not be transferred. If you are interested in copying it, you need to generate scripts for these database objects.
If these are Foreign Keys, connecting these tables together, you need to export the data in the correct order, otherwise the export wizard will fail.
Feel free to read more about this method, as well as about some more methods (including generate scripts, SELECT INTO and third party tools) in this article:
https://www.sqlshack.com/how-to-copy-tables-from-one-database-to-another-in-sql-server/
SELECT ... INTO :
select * into <destination table> from <source table>
INSERT INTO ProductPurchaseOrderItems_bkp
(
[OrderId],
[ProductId],
[Quantity],
[Price]
)
SELECT
[OrderId],
[ProductId],
[Quantity],
[Price]
FROM ProductPurchaseOrderItems
WHERE OrderId=415
If migrating constantly between two databases, then insert into an already existing table structure is a possibility. If so, then:
Use the following syntax:
insert into DESTINATION_DB.dbo.destination_table
select *
from SOURCE_DB.dbo.source_table
[where x ...]
If migrating a LOT of tables (or tables with foreign keys constraints) I'd recommend:
Generating Scripts with the Advanced option / Types of data to script : Data only OR
Resort using a third party tool.
Hope it helps!
Assuming that you want different names for the tables.
If you are using PHPmyadmin you can use their SQL option in the menu. Then you simply copy the SQL-code from the first table and paste it into the new table.
That worked out for me when I was moving from localhost to a webhost.
Hope it works for you!
If you need the entire table structure (not just the basic data layout), use Task>Script Table As>Create To>New Query Window and run in your new database. Then you can copy the data at your leisure.
The quickest way is to use a tool, like RazorSQL or Toad for doing this.
To copy the table from one database to another database, first you have to create the exact table structure for the new table as old one, than copy the table entries from one table to another.
Solution for MySQL database
create table [new database.]<new table name> like [old database.]<old table name>;
insert [new database.]<new table name> select * from [old database.]<old table name>;