Can a table in different schema have totally different rows? - database

Please help me to clarify with the following question about Oracle database:
Say a database has a table called Employee. Both SchemaA and SchemaB use employee table. SchemaA.Employee and SchemaB.Employee are totally different?
In terms of content (rows)
In terms of structures (Columns names and data types)
Please let me know. I use Oracle a lot under fixed Schema.
If any of the above is true, then under the same database, we can have developer schema, test schema, production Schema and training schema. Is that true?

Related

Is there a way to view the relationships of just one column in Oracle SQL Developer?

I´m quite new to the Oracle SQL Developer. I try to handle a big database and I need to define views. Therefore, I need the relationships of some tables. with the relational models of the Browser, I´m able to show ALL relationships of the table, which is quite confusing.
Is there a way to show the ralationships of just one column?
I want to pick the table and the column and I want to see to which other tables the column is connected.
Thank you.
My suggestion:
First find out in which tables the column name has been used :
select * from all_tab_columns where column_name='COLUMN_NAME';
And then you can see constraints defined for the respective columns in that tables

SQL Server - auto-populate field in one table with value from another table in another Database

I have two Azure SQL Server databases with the following as example:
Database Name: DataProp
Table Name: DataImports
Columns: SearchID, SourceID, Text, Status, Country
Database Name: Sources
Table Name: SourceInformation
Columns: SourceID, SourceTitle, Country
Right now, the Country column in the DataProp database is all NULL. I need to auto-populate the Country field in DataProp with the values of the Country fields in the Sources database. The common field between the two tables is SourceID. I need to do this for all existing data, as well as have it occur for future records.
What is the best way to accomplish this? A stored procedure that's set to run on a schedule? If so, I would appreciate guidance on the T-SQL syntax.
As a side-note, I looked at the possibility of a computed column, but this will not work for us b/c we maintain an Azure Search Index on our tables, and Azure Search can't index computed columns.
I don't think you'll be able to directly write a join between tables in two different DBs. We had a similar problem and decided to move all tables into a single DB in separate schemas. I think in your case you can write a Webjob to pull in data from one table and update the second table. I also found one article related to this but haven't personally tried, so not sure if it works.
https://ppolyzos.com/2016/07/30/cross-database-queries-in-azure-sql-databases/

Deleting an employee record from multiple tables in a database

I am having a database where i am having employee id's.
And some how the employee details are having relationship with multiple tables. Now I need to delete all the data from all the tables specific to an employee id.
In a clear way what i want is i need to remove the employee and its associated data from the database.
There are many tables and i can't check all the tables how the employee was associated. Is there any solution for this.
Thanks in advance.
Does your database have foreign keys? Is there documentation (like an E-R diagram and related architecture documentation) for the database that tells you all of the tables that are related to employee?
Without proper documentation, you could guess (based on table and column names), or by going through all application code that accesses the database to try to determine the relationships. Neither of these options are particularly helpful, and may result in missed relationships.

First timers database schema for a MySQL database

I'm looking for some critical feedback on a relational database design I made for a project.
The database is needed for a 2nd year CS project at my school but we have not yet had any teachings on this subject so help is much appreciated.
The database needs to consist of tables such as shopping lists, Users, wares etc. which needs to be shared and updated between units.
As can be seen in the design every user is related to many shoppinglists and likewise mealplans and both of those are also related to groups. Users must also be related to groups and each row in UserGroup linking table has a role as Users must have a role in their group relations.
Is this a structurally sound design?
Is it inadvisable to have columns besides foreign keys in a linking table?
Thanks for your time and help!
http://imgur.com/ypuTVzW.png Full size picture of design schema
it's looks good.
Q> Is this a structurally sound design?
A> Yes
Q> Is it inadvisable to have columns besides foreign keys in a linking table?
A> It's common to have additional columns
Be careful, don't over-normalize.
Sometimes it's wise to have 1 table with duplicates records, than 10 tables join.
best regards

What is the difference between a schema and a table and a database?

This is probably a n00blike (or worse) question. But I've always viewed a schema as a table definition in a database. This is wrong or not entirely correct. I don't remember much from my database courses.
schema -> floor plan
database -> house
table -> room
A relation schema is the logical definition of a table - it defines what the name of the table is, and what the name and type of each column is. It's like a plan or a blueprint. A database schema is the collection of relation schemas for a whole database.
A table is a structure with a bunch of rows (aka "tuples"), each of which has the attributes defined by the schema. Tables might also have indexes on them to aid in looking up values on certain columns.
A database is, formally, any collection of data. In this context, the database would be a collection of tables. A DBMS (Database Management System) is the software (like MySQL, SQL Server, Oracle, etc) that manages and runs a database.
In a nutshell, a schema is the definition for the entire database, so it includes tables, views, stored procedures, indexes, primary and foreign keys, etc.
This particular posting has been shown to relate to Oracle only and the definition of Schema changes when in the context of another DB.
Probably the kinda thing to just google up but FYI terms do seem to vary in their definitions which is the most annoying thing :)
In Oracle a database is a database. In your head think of this as the data files and the redo logs and the actual physical presence on the disk of the database itself (i.e. not the instance)
A Schema is effectively a user. More specifically it's a set of tables/procs/indexes etc owned by a user. Another user has a different schema (tables he/she owns) however user can also see any schemas they have select priviliedges on. So a database can consist of hundreds of schemas, and each schema hundreds of tables. You can have tables with the same name in different schemas, which are in the same database.
A Table is a table, a set of rows and columns containing data and is contained in schemas.
Definitions may be different in SQL Server for instance. I'm not aware of this.
Schema behaves seem like a parent object as seen in OOP world. so it's not a database itself. maybe this link is useful.
But, In MySQL, the two are equivalent. The keyword DATABASE or DATABASES
can be replaced with SCHEMA or SCHEMAS wherever it appears. Examples:
CREATE DATABASE <=> CREATE SCHEMA
SHOW DATABASES <=> SHOW SCHEMAS
Documentation of MySQL
SCHEMA & DATABASE terms are something DBMS dependent.
A Table is a set of data elements (values) that is organized using a model of vertical columns (which are identified by their name) and horizontal rows. A database contains one or more(usually) Tables . And you store your data in these tables. The tables may be related with one another(See here).
As per https://www.informit.com/articles/article.aspx?p=30669
The names of all objects must be unique within some scope. Every
database must have a unique name; the name of a schema must be unique
within the scope of a single database, the name of a table must be
unique within the scope of a single schema, and column names must be
unique within a table. The name of an index must be unique within a
database.
From the PostgreSQL documentation:
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.
There are several reasons why one might want to use schemas:
To allow many users to use one database without interfering with each other.
To organize database objects into logical groups to make them more manageable.
Third-party applications can be put into separate schemas so they do not collide with the names of other objects.
Schemas are analogous to directories at the operating system level, except that schemas cannot be nested.
Contrary to some of the above answers, here is my understanding based on experience with each of them:
MySQL: database/schema :: table
SQL Server: database :: (schema/namespace ::) table
Oracle: database/schema/user :: (tablespace ::) table
Please correct me on whether tablespace is optional or not with Oracle, it's been a long time since I remember using them.
As MusiGenesis put so nicely, in most databases:
schema : database : table :: floor plan : house : room
But, in Oracle it may be easier to think of:
schema : database : table :: owner : house : room
More on schemas:
In SQL 2005 a schema is a way to group objects. It is a container you can put objects into. People can own this object. You can grant rights on the schema.
In 2000 a schema was equivalent to a user. Now it has broken free and is quite useful. You could throw all your user procs in a certain schema and your admin procs in another. Grant EXECUTE to the appropriate user/role and you're through with granting EXECUTE on specific procedures. Nice.
The dot notation would go like this:
Server.Database.Schema.Object
or
myserver01.Adventureworks.Accounting.Beans
A Schema is a collection of database objects which includes logical structures too.
It has the name of the user who owns it.
A database can have any number of Schema's.
One table from a database can appear in two different schemas of same name.
A user can view any schema for which they have been assigned select privilege.
I try answering based on my understanding of the following analogy:
A database is like the house
In the house there are several types of rooms. Assuming that you're living in a really big house. You really don't want your living rooms, bedrooms, bathrooms, mezzanines, treehouses, etc. to look the same. They each need a blueprint to tell how to build/use them. In other words, they each need a schema to tell how to build/use a bathroom, for example.
Of course, you may have several bedrooms, each looks slightly different. You and your wife/husband's bedroom is slightly different from your kids' bedroom. Each bedroom is analogous to a table in your database.
A DBMS is like a butler in the house. He manages literally everything.
In oracle Schema is one user under one database,For example scott is one schema in database orcl.
In one database we may have many schema's like scott
Schemas contains Databases.
Databases are part of a Schema.
So, schemas > databases.
Schemas contains views, stored procedure(s), database(s), trigger(s) etc.
A schema is not a plan for the entire database. It is a plan/container for a subset of objects (ex.tables) inside a a database. This goes to say that you can have multiple objects(ex. tables) inside one database which don't neccessarily fall under the same functional category. So you can group them under various schemas and give them different user access permissions. That said, I am unsure whether you can have one table under multiple schemas. The Management Studio UI gives a dropdown to assign a schema to a table, and hence making it possible to choose only one schema. I guess if you do it with TSQL, it might create 2 (or multiple) different objects with different object Ids.
A database schema is a way to logically group objects such as tables, views, stored procedures etc. Think of a schema as a container of objects.
And tables are collections of rows and columns.
combination of all tables makes a db.

Resources