database-table

PostgreSQL create table if not exists

霸气de小男生 提交于 2019-11-26 19:47:50
In a MySQL script you can write: CREATE TABLE IF NOT EXISTS foo ...; ... other stuff ... and then you can run the script many times without re-creating the table. How do you do this in PostgreSQL? This feature has been implemented in Postgres 9.1 : CREATE TABLE IF NOT EXISTS myschema.mytable (i integer); For older versions , here is a function to work around it: CREATE OR REPLACE FUNCTION create_mytable () RETURNS void AS $func$ BEGIN IF EXISTS (SELECT 1 FROM pg_catalog.pg_tables WHERE schemaname = 'myschema' AND tablename = 'mytable') THEN RAISE NOTICE 'Table myschema.mytable already exists.'

Facebook database design?

梦想与她 提交于 2019-11-26 17:59:34
I have always wondered how Facebook designed the friend <-> user relation. I figure the user table is something like this: user_email PK user_id PK password I figure the table with user's data (sex, age etc connected via user email I would assume). How does it connect all the friends to this user? Something like this? user_id friend_id_1 friend_id_2 friend_id_3 friend_id_N Probably not. Because the number of users is unknown and will expand. Keep a friend table that holds the UserID and then the UserID of the friend (we will call it FriendID). Both columns would be foreign keys back to the

Table-level backup

我们两清 提交于 2019-11-26 15:12:53
问题 How to take table-level backup (dump) in MS SQL Server 2005/2008? 回答1: You cannot use the BACKUP DATABASE command to backup a single table, unless of course the table in question is allocated to it's own FILEGROUP . What you can do, as you have suggested is Export the table data to a CSV file. Now in order to get the definition of your table you can 'Script out' the CREATE TABLE script. You can do this within SQL Server Management Studio, by: right clicking Database > Tasks > Generate Script

Mysql: Select rows from a table that are not in another

醉酒当歌 提交于 2019-11-26 15:08:37
How to select all rows in one table that do not appear on another? Table1: +-----------+----------+------------+ | FirstName | LastName | BirthDate | +-----------+----------+------------+ | Tia | Carrera | 1975-09-18 | | Nikki | Taylor | 1972-03-04 | | Yamila | Diaz | 1972-03-04 | +-----------+----------+------------+ Table2: +-----------+----------+------------+ | FirstName | LastName | BirthDate | +-----------+----------+------------+ | Tia | Carrera | 1975-09-18 | | Nikki | Taylor | 1972-03-04 | +-----------+----------+------------+ Example output for rows in Table1 that are not in Table2:

T-SQL Pivot? Possibility of creating table columns from row values

我的未来我决定 提交于 2019-11-26 14:34:47
Is it actually possible to rotate a T-SQL (2005) so that (for the sake of argument) the values of the first column's rows become the titles of the output table's columns? I realise this is not really what PIVOT is for, but it's what I need - the ability to request a table where the columns are not known before-hand because they have been entered as values into a table. Even a hack would be nice, tbh. Itzik Ben-Gan's example on how to build dynamic PIVOT, I highly recommend his Inside Microsoft SQL Server 2008: T-SQL Programming book -- Creating and Populating the Orders Table USE tempdb; GO IF

MySQL - Select from a list of numbers those without a counterpart in the id field of a table

荒凉一梦 提交于 2019-11-26 14:18:51
问题 I have a list of numbers, say {2,4,5,6,7} I have a table, foos, with foos.ID, including say, {1,2,3,4,8,9} Id like to take my list of numbers, and find those without a counterpart in the ID field of my table. One way to achieve this would be to create a table bars, loaded with {2,4,5,6,7} in the ID field. Then, I would do SELECT bars.* FROM bars LEFT JOIN foos ON bars.ID = foos.ID WHERE foos.ID IS NULL However, I'd like to accomplish this sans temp table. Anyone have any input on how it might

Maximum number of records in a MySQL database table

柔情痞子 提交于 2019-11-26 12:45:01
What is the upper limit of records for MySQL database table. I'm wondering about autoincrement field. What would happen if I add milions of records? How to handle this kind of situations? Thx! mysql int types can do quite a few rows: http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html unsigned int largest value is 4,294,967,295 unsigned bigint largest value is 18,446,744,073,709,551,615 The greatest value of an integer has little to do with the maximum number of rows you can store in a table. It's true that if you use an int or bigint as your primary key, you can only have as many rows

MySQL: Select Random Entry, but Weight Towards Certain Entries

只愿长相守 提交于 2019-11-26 11:45:48
I've got a MySQL table with a bunch of entries in it, and a column called "Multiplier." The default (and most common) value for this column is 0, but it could be any number. What I need to do is select a single entry from that table at random. However, the rows are weighted according to the number in the "Multiplier" column. A value of 0 means that it's not weighted at all. A value of 1 means that it's weighted twice as much, as if the entry were in the table twice. A value of 2 means that it's weighted three times as much, as if the entry were in the table three times. I'm trying to modify

Truncating all tables in a Postgres database

大兔子大兔子 提交于 2019-11-26 11:32:52
I regularly need to delete all the data from my PostgreSQL database before a rebuild. How would I do this directly in SQL? At the moment I've managed to come up with a SQL statement that returns all the commands I need to execute: SELECT 'TRUNCATE TABLE ' || tablename || ';' FROM pg_tables WHERE tableowner='MYUSER'; But I can't see a way to execute them programmatically once I have them. Henning FrustratedWithFormsDesigner is correct, PL/pgSQL can do this. Here's the script: CREATE OR REPLACE FUNCTION truncate_tables(username IN VARCHAR) RETURNS void AS $$ DECLARE statements CURSOR FOR SELECT

MySQL > Table doesn&#39;t exist. But it does (or it should)

拜拜、爱过 提交于 2019-11-26 11:30:45
I did change the datadir of a MySQL installation and following some steps it worked fine. Every base I had was moved correctly but one. I can connect and USE the database, even SHOW TABLES returns me all the tables correctly and the files of each table exists on the mysql data directory. But when I try to SELECT something there, it says the table doesn't exists. But the table does exists, it even shows at SHOW TABLES statement! My guess is that the SHOW TABLES lists the files existence somehow that the files are corrupt or something like that but it doesn't check it. So I can list them but not