database-table

Create table in SQLite only if it doesn't exist already

≡放荡痞女 提交于 2019-11-26 10:17:47
问题 I want to create a table in a SQLite database only if doesn\'t exist already. Is there any way to do this? I don\'t want to drop the table if it exists, only create it if it doesn\'t. 回答1: From http://www.sqlite.org/lang_createtable.html: CREATE TABLE IF NOT EXISTS some_table (id INTEGER PRIMARY KEY AUTOINCREMENT, ...); 来源: https://stackoverflow.com/questions/4098008/create-table-in-sqlite-only-if-it-doesnt-exist-already

Is there any reason to worry about the column order in a table?

ε祈祈猫儿з 提交于 2019-11-26 10:06:44
问题 I know you can ALTER the column order in MySQL with FIRST and AFTER, but why would you want to bother? Since good queries explicitly name columns when inserting data, is there really any reason to care what order your columns are in in the table? 回答1: Column order had a big performance impact on some of the databases I've tuned, spanning Sql Server, Oracle, and MySQL. This post has good rules of thumb: Primary key columns first Foreign key columns next. Frequently searched columns next

Import CSV to mysql table

牧云@^-^@ 提交于 2019-11-26 09:54:51
What is the best/fastest way to upload a csv file into a mysql table? I would like for the first row of data be used as the column names. Found this: How to import CSV file to MySQL table But the only answer was to use a GUI and not shell? Instead of writing a script to pull in information from a CSV file, you can link MYSQL directly to it and upload the information using the following SQL syntax. To import an Excel file into MySQL, first export it as a CSV file. Remove the CSV headers from the generated CSV file along with empty data that Excel may have put at the end of the CSV file. You can

Limit an sqlite Table's Maximum Number of Rows

人盡茶涼 提交于 2019-11-26 09:49:42
问题 I am looking to implement a sort of \'activity log\' table where actions a user does are stored in a sqlite table and then presented to the user so that they can see the latest activity they have done. However, naturally, I don\'t feel it is necessary to keep every single bit of history, so I am wondering if there is a way to configure the table to start pruning older rows once a maximum set limit is reached. For example, if the limit is 100, and that\'s how many rows there currently are in

SQL Server: drop table cascade equivalent?

拟墨画扇 提交于 2019-11-26 09:46:00
问题 In oracle, to drop all tables and constraints you would type something like DROP TABLE myTable CASCADE CONSTRAINTS PURGE; and this would completely delete the tables and their dependencies. What\'s the SQL server equivalent?? 回答1: I don't believe SQL has a similarly elegant solution. You have to drop any related constraints first before you can drop the table. Fortunately, this is all stored in the information schema and you can access that to get your whack list. This blog post should be

Why use multiple columns as primary keys (composite primary key)

做~自己de王妃 提交于 2019-11-26 09:16:17
问题 This example is taken from w3schools. CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName) ) My understanding is that both columns together ( P_Id and LastName ) represent a primary key for the table Persons . Is this correct? Why would someone want to use multiple columns as primary keys instead of a single column? How many columns can be used together as a

Add a new table column to specific ordinal position in Microsoft SQL Server

南楼画角 提交于 2019-11-26 08:26:55
问题 Is it possible to add a column to a table at a specific ordinal position in Microsoft SQL Server? For instance, our tables always have CreatedOn, CreatedBy, LastModifiedOn, LastModifiedBy columns at the \"end\" of each table definition? I\'d like the new column to show up in SSMS above these columns. If I am scripting all my database changes, is there a way to preserve this order at the end of the table? FYI, I\'m not trying to institute a flame war on if this should even be done. If you want

How can I create a copy of an Oracle table without copying the data?

会有一股神秘感。 提交于 2019-11-26 06:11:49
问题 I know the statement: create table xyz_new as select * from xyz; Which copies the structure and the data, but what if I just want the structure? 回答1: Just use a where clause that won't select any rows: create table xyz_new as select * from xyz where 1=0; Limitations The following things will not be copied to the new table: sequences triggers indexes some constraints may not be copied materialized view logs This also does not handle partitions 回答2: I used the method that you accepted a lot,

Copy tables from one database to another in SQL Server

最后都变了- 提交于 2019-11-26 05:51:52
I have a database called foo and a database called bar. I have a table in foo called tblFoobar that I want to move (data and all) to database bar from database foo. What is the SQL statement to do this? Amy B On SQL Server? and on the same database server? Use three part naming. INSERT INTO bar..tblFoobar( *fieldlist* ) SELECT *fieldlist* FROM foo..tblFoobar This just moves the data. If you want to move the table definition (and other attributes such as permissions and indexes), you'll have to do something else. David SQL Server Management Studio's "Import Data" task (right-click on the DB

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

╄→尐↘猪︶ㄣ 提交于 2019-11-26 04:11:27
问题 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