How reset primary key in Active Record with SQLITE3 - ruby-on-rails-3

I have this problem.I create a table called Post and then I entered some records
How I can truncate the table Post so that the primary key again becomes 1 with out use rake db:drop
This code not working for me
$ rails console
> ActiveRecord::Base.connection.execute("DELETE from sqlite_sequence where name = 'yourtablename'"

Make sure that you are using the proper yourtablename and following name conventions. The table name is always lower-case and pluralized.
For example, if your model name is User, your table name is users.
If your model name is Image, your table name will be images.
Let me know if this solved the problem.

Related

Error: ORA-04043: object table name does not exist when describing any table within a specific user workstation from the SQL command line

I have created tables in Oracle DB from the SQL command line, and I'm having a problem when describing the table, when going through the oracle application express web page I can see them there.
The oracle version I have is the following:
SQL*Plus: Release 11.2.0.2.0 Production
The following is the command I used to create a table in the database:
CREATE TABLE "Product"
( "ProuctID" VARCHAR2(8) NOT NULL ENABLE,
"ProductExpiryDate" DATE,
"CustomerID" VARCHAR2(8),
CONSTRAINT "Product_PK" PRIMARY KEY ("ProductID") ENABLE
) ;
Command for describing the table:
Desc Product;
But at the end after creating each table and describe it I get this:
ORA-04043: object Product does not exist
Can anyone please tell me why I am I getting this, when I can see it in Oracle Xpress web page?
By enclosing the table name in double quotes, you created the table with a case-sensitive name. To correctly specify the name, you now have to always enclose it in double quotes.
So instead of Desc Product, you need Desc "Product".
Because this is quite cumbersome and error-prone, it's usually best to avoid enclosing table and column names in double quotes in the first place. If possible, I'd recommend you either drop & recreate the table or rename it.

Remove constraint and table name version details in sql developer data model

I have problem with Table and Constraint name ,which are coming with version in Model Diagram representation.Using Oracle SQL Developer Data Modle shows my table name as APP_REALm_ENTRIESv4,but my table name used in create statement is APP_REALm_ENTRIES .Table name comes with extra characters v4.Because that no of time I have re-created table is four.Same problem persists with contraints too like ARE_ID_PK added with v4.
When the modeler adds "v1" or "v2" or .. on your table name your chosen table name is already defined in your model and your edited table should get the same name. To avoid that conflict the modeler adds "v1".

HSQLDB user lacks privilege or object not found error when making select statements with where

I use SQuirrel SQL Client Version 3.5.3 and HSQLDB for my database. I have been able to specify the corresponding driver (In-memory) to it and create an Alias.
I have created a table
CREATE TABLE ENTRY(
NAME VARCHAR(100) NOT NULL,
DESC VARCHAR(500) NOT NULL,
PRIMARY KEY (NAME))
and added a few lines of data into it. While statements like these work:
select * from ENTRY
select NAME from ENTRY
select DESC from ENTRY
I always get Error: user lacks privilege or object not found"
when adding a where clause to my statement, e.g. select DESC from ENTRY where NAME=CAR
Any help is greatly appreciated as I can slowly feel my sanity waning
I had the same problem, but my table name and other things were ok except my query for VARCHAR were inside double quotes("") but it should be in single quotes('')
example:
assume you have table like this which flightId is primary key
now this query is wrong:
SELECT * FROM flights WHERE flightId="0f3ae9b3-6bb1-4c95-9394-6179555f5879"
while this one is ok:
SELECT * FROM flights WHERE flightId='0f3ae9b3-6bb1-4c95-9394-6179555f5879'
I was finally able to fix this myself. I had used a wrong table name for my select statements and after changing it to the real one it worked. The only thing that confuses me is that I also used the wrong table name for my insert statements but they were executed successfully and all data is showing up in them.
HSQLDB has default schema called PUBLIC. All SQL queries will be pointing to PUBLIC; If you have created your own schema like eg:OWNSCHEMA then edit the xxx.script and change the following line
SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC
to
SET DATABASE DEFAULT INITIAL SCHEMA OWNSCHEMA
When I received the same exception the root cause was that I had a table in the SELECT clause that was not present in the FROM clause.
Your problem is:
I always get Error: user lacks privilege or object not found" when
adding a where clause to my statement, e.g. select DESC from ENTRY
where NAME=CAR
Yes, of course you do.
NAME is a field of the ENTRY table. CAR isn't a field of anything.
Perhaps your WHERE clause should look like this instead:
WHERE NAME='CAR'
Thereby comparing a field value with a literal string value instead of trying to compare it with a nonexistent other field value.

Customized Table Names in Sql Server

I have a table called Table 1. I'm trying to create an after-insert trigger for a Table 1; whereby, whenever a user enters a record, the trigger will create a new table named after the record that triggered its creation.
Please help, I'm using SQL Server 2008
This sounds super non-relational-database-design-ish. I would heavily advise against this in almost every case. And I say "almost" only to allow for artistic freedom of development, I can't think of a single case where this would be appropriate.
That said, if you do in fact want this, you can use dynamic SQL to create a table.
You can build the SQL in your trigger, but basically you want something like:
EXEC 'CREATE TABLE ' + #tableName + ' (ID INT IDENTITY(1,1))';
Of course, the columns are up to you, but that should get you started.
But while we're at it, what you should (probably) be doing is using a single table with a one-to-many relationship to the table on which your trigger is currently assigned.
For instance, if you have a table Users with a column for email and you're looking to create a table for each user's favorites on your website, you should instead consider adding an identity column for user IDs, then reference that in a single UserFavorites table that has UserId and PostId columns, and the appropriate foreign keys implemented.

Purpose of uploading a schema file

I'm attempting to make a table for the first time using postgres and the examples I'm seeing are kind of throwing me off. When it comes to creating a schema, I have a schema.sql file that contains my schema as follows:
CREATE TABLE IF NOT EXISTS orders
(
order_id INTEGER NOT NULL,
order_amount INTEGER NOT NULL
);
COMMENT ON COLUMN orders.order_id IS 'The order ID';
COMMENT ON COLUMN orders.order_amount IS 'The order amount';
Now I'd upload that schema by doing the following:
psql -d mydb -f /usr/share/schema.sql
Now when it comes time to create the table I'm suppose to do something like this:
create table schema.orders(
order_id INT NOT NULL,
order_amount INT NOT NULL
);
The uploading of the schema.sql file is what confuses me. What is all the information inside the file used for. I thought by uploading the schema i'm providing the model to create the table, but running create table schema.orders seems to be doing just that.
What you call "upload" is actually executing a script file (with SQL DDL commands in it).
I thought by uploading the schema i'm providing the model to create the table
You are creating the table by executing that script. The second CREATE TABLE command is almost but not quite doing the same. Crucial difference (besides the missing comments): A schema-qualified table name. And your schema happens to be named "schema", which is a pretty bad idea, but allowed.
Now, the term "schema" is used for two different things:
The general database structure created with SQL DDL commands.
A SCHEMA which is similar to a directory in a file system.
The term just happens to be the same for either, but one has nothing to do with the other.
Depending on the schema search path, the first invocation of CREATE TABLE may or may not have created another table in a different schema. You need to understand the role of the search path in Postgres:
How does the search_path influence identifier resolution and the "current schema"