Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between database name and schema name in SQLAlchemy?

Tags:

sql

sqlalchemy

When connecting to a particular table in a database I might supply the following information

Database: protocol://user:pass@host:port/dbname
Table:    myschema.mytable

What is the difference here between dbname and myschema? Their use seems redundant to me.

like image 985
MRocklin Avatar asked Oct 31 '14 18:10

MRocklin


People also ask

What is a schema in SQL?

A schema is a grouping of database objects (tables, views, etc) within a database. It is a way of logically partitioning objects within a database. Assuming the user had the proper permissions, they could access tables in multiple schemas with one database connection. Joining tables (even from multiple schemas) is also trivial.

What is the difference between table name and schema name?

The table name, along with the value of the schema parameter, forms a key which uniquely identifies this Table within the owning MetaData collection. Additional calls to Table with the same name, metadata, and schema name will return the same Table object.

Does SQLAlchemy support a dynamic schema name system?

Finally, SQLAlchemy also supports a “dynamic” schema name system that is often used for multi-tenant applications such that a single set of Table metadata may refer to a dynamically configured set of schema names on a per-connection or per-statement basis. What’s “schema” ?

What is the difference between a schema and multiple databases?

When objects are stored in multiple databases, you'd need a connection per database to access these objects, and joins are generally more difficult (your RDBMS or ORM may hide some of these difficulties). Each database would also have separate logins. After a bit more reading, I believe schemas are just ways or partition up a DB.


2 Answers

A schema is a grouping of database objects (tables, views, etc) within a database. It is a way of logically partitioning objects within a database.

Assuming the user had the proper permissions, they could access tables in multiple schemas with one database connection. Joining tables (even from multiple schemas) is also trivial.

When objects are stored in multiple databases, you'd need a connection per database to access these objects, and joins are generally more difficult (your RDBMS or ORM may hide some of these difficulties). Each database would also have separate logins.

like image 193
Gerrat Avatar answered Oct 16 '22 14:10

Gerrat


After a bit more reading, I believe schemas are just ways or partition up a DB. This can be useful for both managing permission and providing uniqueness to similarly named tables. So can collect a set of tables, view, triggers, into a schema and set permissions there. I can then set perms for that schema and hand that to users.

Additionally, I can also overload a table name but provide uniqueness through the schema. I believe this happens more often in transaction DBs where users maybe have a similarly named table but the schema is different per user.

like image 42
quasiben Avatar answered Oct 16 '22 14:10

quasiben