relational-database

Postgresql delete multiple rows from multiple tables

若如初见. 提交于 2019-12-06 02:22:05
问题 Consider 2 or more tables: users (id, firstname, lastname) orders (orderid, userid, orderdate, total) I wish to delete all users and their orders that match first name ' Sam '. In mysql, I usually do left join. In this example userid is unknown to us. What is the correct format of the query? 回答1: http://www.postgresql.org/docs/current/static/sql-delete.html DELETE FROM orders o USING users u WHERE o.userid = u.id and u.firstname = 'Sam'; DELETE FROM users u WHERE u.firstname = 'Sam'; You can

Difference between DB::table('table') and model::('table')

为君一笑 提交于 2019-12-06 01:51:21
问题 on laravel we can access by using DB::table('table')->get(); or using model::('table')->all(); My question is what's the difference between them ? thanks. 回答1: You can do this because Model and the DB facade both implement functions that yield a Builder instance. https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Model.html https://laravel.com/api/5.2/Illuminate/Database/Query/Builder.html The difference is, instances of Model have properties which set up a Builder with predesignated

How to set up Primary Keys in a Relation?

二次信任 提交于 2019-12-06 00:03:39
I wish to know how to correctly set up Primary Keys in a Relation . E.g. we have ER-diagram which contain elements: Key attributes Weak key attributes Identifying relationships Associative entities In order to translate it into Relational Model we should do some tricks. All elements above deal with Primary Keys of relations but they all are Natural Keys - so we can leave them as is or replace with Surrogate Keys . Consider some cases. Case 1 Key Attribute is a name - so it must be of type CHAR or VARCHAR . Generally names become Key Attributes . Case 2 Two (or more) Identifying Relationships

Multiple tables in one view?

狂风中的少年 提交于 2019-12-05 21:48:37
Today my question is how would I go about creating a view in a MySQL database that uses more than two tables? Here is my query (it works) I am not looking to change my current query, mostly looking for a nice reference with examples on this topic. CREATE OR REPLACE VIEW vw_itemsPurchased AS SELECT `tbl_buyers`.`fldPrimaryKey` as fldFKeyBuyer, `tbl_buyers`.`fldEmail` as fldBuyerEmail, `tbl_buyers`.`fldAddressStreet`, `tbl_buyers`.`fldAddressCity`, `tbl_buyers`.`fldAddressState`, `tbl_buyers`.`fldAddressZip`, `tbl_buyers`.`fldAddressCountry`, `fldPaymentCurrency`, `fldPaymentGross`,

Normalize an Address

你说的曾经没有我的故事 提交于 2019-12-05 21:27:09
I am trying to normalize an address. The diagram below shows the relevant tables for this question I believe. I want to know how ZipCodes should be integrated into the model. This would be for international addresses so I know that a Zip/PostalCode is not used everywhere. I think City::ZipCode is 1::0-n (I have read others saying this is not always the case but they never provided evidence). If they are correct then I guess this would be a many-to-many relationship. Since each Address can only have at most one ZipCode while a ZipCode can contain many addresses I am lost at how to normalize

How to reference groups of records in relational databases?

♀尐吖头ヾ 提交于 2019-12-05 19:27:38
Suppose we have the following table structures: Humans | HumanID | FirstName | LastName | Gender | |---------+-----------+----------+--------| | 1 | Issac | Newton | M | | 2 | Marie | Curie | F | | 3 | Tim | Duncan | M | Animals | AmimalID | Species | NickName | |----------+---------+----------| | 4 | Tiger | Ronnie | | 5 | Dog | Snoopy | | 6 | Dog | Bear | | 7 | Cat | Sleepy | I wonder how to reference a group of records in other tables. For example, I have a Foods table and an EatenBy column. Foods | FoodID | FoodName | EatenBy | |--------+----------+---------| | 8 | Rice | ??? | What I want

SQLAlchemy: How to conditionally choose type for column by depending on its backend

依然范特西╮ 提交于 2019-12-05 19:02:03
问题 I want to use HSTORE type for a column if it uses PostgreSQL as its backend, or PickleType otherwise. The problem is that we cannot determine which backend will be used when schema is being defined (in Python). How can I determine this and conditionally choose the data type when the table actually is created on the backend database? 回答1: You can accomplish something like this with TypeEngine.with_variant: from sqlalchemy.types import PickleType from sqlalchemy.dialects import postgresql

Converting a SQLite Database to a triple store

流过昼夜 提交于 2019-12-05 18:32:17
Can somebody please describe the steps neccessary to convert a SQLite Database to a triple store? Is there a tool that can accomplish the task? This is a more complicated question then it seemed when I asked it, but the simple answer is that you normalize your database completely. After it is completely normalized each table stands for a predicate, one columns values represent the subject and one columns values represent the object. You can convert an arbitrary sql database to a triplestore on this basis. The function transform to triple convert any kind of relational data into triple format:

Using Oracle database links without unreadable dynamic SQL

▼魔方 西西 提交于 2019-12-05 17:31:00
How can I write a neat PL/SQL stored procedure that can execute against a given database link? It gets really messy, writing stuff like this: PROCEDURE my_proc(aDbLink IN VARCHAR2) IS BEGIN EXECUTE IMMEDIATE ' SELECT mycolumn, anothercolumn FROM MYTABLE@' || aDbLink || ' WHERE such-and-such...' END as the query gets bigger. What else might I do? I'm stuck using stored procedures, and expect that my procedures will execute against one of several db links. The simplest way to avoid using dynamic SQL would be to create synonyms. CREATE OR REPLACE SYNONYM MyTableRemote FOR MyTable@database_link

Mysql order by on column with unicode characters

谁都会走 提交于 2019-12-05 17:20:27
I am running a select query on mysql table and trying to order it by the "name" column in the table. The name column contains both English character names and names with Latin character like â. I am running into the below problem. The query I run returns the results ordered in the below manner i.e. Eg: if Name contains "archer", "aaakash", "â hayden", "bourne", "jason" The results returned by the query is ordered as below "aaakash", "archer", "â hayden", "bourne", "jason" However I want to order it based on unicode code points (like below) "aaakash", "archer", "bourne", "jason", "â hayden"