Change schemas' structure in a PostgreSQL database - sql

I have a Postgres database with some schemas (all have the same structure), I want to know if there is the possibility to change the structure (Table names, new columns etc) for all the schemas in the same database. Is it possible or what's the purpose of the schemas in a database?
Thanks.

I'm going to focus on the second half of your question, because I think it'll answer the first half (and I'm not sure I understand the first half).
what's the purpose of the schemas in a database?
This confused me when I first switched from MySQL to PostgreSQL. A Postgres schema is essentially the same as a MySQL database. In fact, according to the MySQL Reference Manual:
In MySQL, physically, a schema is synonymous with a database.
That begs the question of what is a PostgreSQL database, then? From the PostgreSQL Documentation:
More accurately, a database is a collection of schemas and the schemas contain the tables, functions, etc. So the full hierarchy is: server, database, schema, table (or some other kind of object, such as a function).
So a PostgreSQL database is essentially a collection of schemas? Seems kind of pointless, why do we need that step in the hierarchy? Let's take a look at the docs for a PostgreSQL schema:
A PostgreSQL database cluster contains one or more named databases. Users and groups of users are shared across the entire cluster, but no other data is shared across databases. Any given client connection to the server can access only the data in a single database, the one specified in the connection request.
A database contains one or more named schemas, which in turn contain tables. Schemas also contain other kinds of named objects, including data types, functions, and operators. The same object name can be used in different schemas without conflict; for example, both schema1 and myschema can contain tables named mytable. Unlike databases, schemas are not rigidly separated: a user can access objects in any of the schemas in the database he is connected to, if he has privileges to do so.
So, in PostgreSQL, a schema contains tables, functions, etc. And a database manages user/group connectivity and access/roles to specific clusters of schemas. Typically, I work under one database and have information broken into schemas to segment information.

Related

Custom Name for `information_schema` database

Is there a way to modify the SQL server to give the virtual information_schema database a different name by default?
Or is information_schema a standard so that software knows where to look and query for information?
I'm using 10.5.15-MariaDB - MariaDB Server
I'd like to rename it to .information_schema so that the database doesn't show up in the middle of the databases list on my CMS.
I don't have control over hiding/displaying databases by name.
information_schema cannot be modified or deleted.
If I were you, I would avoid any attempt to modify the default databases.
Here is what I found searching similar questions:
INFORMATION_SCHEMA is a database within each MySQL instance, the place
that stores information about all the other databases that the MySQL
server maintains. The INFORMATION_SCHEMA database contains several
read-only tables. They are actually views, not base tables, so there
are no files associated with them, and you cannot set triggers on
them. Also, there is no database directory with that name.
*(source)

Auto-Match Fields to Columns SQL LOADER

I'm trying to load some data from csv to Oracle 11g database tables through sqlldr
So I was I thinking if there's a way to carry those data matching the columns described on the ctl file with the table columns by the name. Just like an auto-match, with no sequential order or filler command
Anyone knows a thing about that? I've been searching in documentation and forums but haven't found a thing
Thank you, guys
Alas you're on 11g. What you're looking for is a new feature in 12c SQL Loader Express Mode. This allows us to load a comma-delimited file to a table without defining a Loader control file; instead Oracle uses the data dictionary ALL_TAB_COLUMNS to figure out the mapping.
Obviously there are certain limitations. Perhaps the biggest one is that external tables are the underlying mechanism so it requires the same privileges , including privileges on Directory objects. I think this reduces the helpfulness of the feature, because many people need to use SQL Loader precisely because their DBAs or sysadmins won't grant them the privileges necessary for external tables.

What is schema and database?

What is the difference between schema and database? I have no knowledge other than running basic commands in sql such as select, update, insert and delete.
A Database is a collection of organized data, while database schema describes the structure and organization of data in a database system. The database holds the records, fields and cells of data. The database schema describes how these fields and cells are structured and organized and what types of relationships are mapped between these entities. Understandably, the schema of a database keeps constant once created, while the actual data in the database tables may change all the time.

Does an information schema exist before it is queried?

In a SQL database, I can run a query to present information as it exists, and I can create new compilations of data that did not previously exist.
For instance, SELECT * FROM Table1 would return information that already existed, while a series of nested joins and WHERE statements could present data in ways that didn't exist before the query was run.
My question is whether the database's information schema -- assuming it's never been pulled up before -- falls into the first category or the second.
Information schema views query already existent system tables in database. You can control yourself as sys.tables etc which are called catalog views in Sql server.
Therefore using these views falls to second type of usage in your question. Using existent data in a different way.
Everything in INFORMATION_SCHEMA is just a view on the system tables. So the answer to your question is both that the data has always been there (because every object in the database has one or more rows in system tables somewhere representing it) and also that it's generated for your viewing pleasure upon querying (to present it in the form that INFORMATION_SCHEMA requires).
Note that even what we normally call "the system tables" (sys.tables and related) are also just views on the real, actual, physical system tables, which are not accessible to any user but only to the database engine itself -- viewing those directly requires a direct administrator connection and tweaking some flags, and is typically not something done by anyone other than SQL Server developers.
As to what this implies in a FOIA context is probably best answered in a legal setting, not an information-theoretical one.

SQL Server - Same table in multiple schemas

I am creating a smaller sized database in Microsoft SQL Server 2012 to keep run data from machines. The company has production machines and R & D machines. I would like to use the same table for production and R&D with a Type field specifying what the run was for simplicity. I have two schemas (prod and r_d). The permissions for the production and r_d schemas will be different. Is it possible to create a table that belongs to more than one schema? I know you can have the same table name in multiple schemas, but this creates separate objects. I would like to have the one table object to belong to multiple schemas.
Example:
CREATE TABLE db_name.prod.r_d.table_name
Consider creating a synonym in one the of schemas, referencing the other schema table:
CREATE SYNONYM r_d.table_name FOR prod.table_name;
No, but you can create a view in each schema on to a single table that filters the rows
When you said Development and Production. You should consider using separate database and as we go separate server !
For sampling the data you could use a backup of the production database. If you don't want the dev team to have access to production data (avoid data leak), they have to generate their sample data themselves.
Using Synonym or View in your case looks like a bad and dangerous practice !