Raw query to set AutoIncrement to false? - laravel

The id column in the student table is an auto incrementing one.I wanted to make that to non - autoincrementing. May i know, how can i modify the below query to work as such?
DB::statement("ALTER TABLE student SET AUTO_INCREMENT=FALSE;");
the above code shows the below error.
Illuminate\Database\QueryException
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 1: ALTER TABLE student SET AUTO_INCREMENT=FALSE;
^ (SQL: ALTER TABLE student SET AUTO_INCREMENT=FALSE;)

The correct syntax in PostgreSQL would be:
ALTER TABLE student ALTER COLUMN id DROP DEFAULT;
Where id is the serial column.
You might also want to drop the not null constraint :
ALTER TABLE student ALTER COLUMN id DROP NOT NULL;

This is an example of why posting table definitions (ddl) and Postgres version can be critical. #Zakaria is correct if the Postgres version is prior to version 10, or if version 10 or later and is still defined as serial/bigserial. However the preferred definition for version 10 and later is generated always as identity. If defined as identity you need:
alter table student alter column id drop identity.
I would not drop a not null constraint, and if it is the PK it is moot point as it will automatically be not null.

Related

ORA-02289: sequence doesn't exist

I created a sequence EMP_SEQ then added a column to a table with EMP_SEQ as column as below:
ALTER TABLE EMPLOYEE ADD EMP_SEQ;
UPDATE EMPLOYEE SET EMP_SEQ = EMP_SEQ.NEXTVAL;
ALTER TABLE EMPLOYEE MODIFY(EMP_SEQ DEFAULT ON NULL EMP_SEQ.NEXTVAL);
Later I had to rename the sequence to EMP_SEQ_AUTO_INC and column name of EMPLOYEE table to the same as below:
ALTER TABLE EMPLOYEE COLUMN EMP_SEQ TO EMP_SEQ_AUTO_INC;
RENAME EMP_SEQ TO EMP_SEQ_AUTO_INC;
And now the ORA 02289 occurs when I try to insert data into the table with column_name EMP_SEQ_AUTO_INC.
Should I complete the below steps with new column name to overcome this error:
UPDATE EMPLOYEE SET EMP_SEQ_AUTO_INC = EMP_SEQ_AUTO_INC.NEXTVAL;
ALTER TABLE EMPLOYEE MODIFY(EMP_SEQ_AUTO_INC DEFAULT ON NULL EMP_SEQ_AUTO_INC.NEXTVAL);
That's not Oracle 11g, it doesn't support syntax you used. I suggest you fix the database version tag.
Anyway: with all modifications you made, you'll just need to re-run
ALTER TABLE employee MODIFY
(emp_seq_auto_inc DEFAULT ON NULL emp_seq_auto_inc.NEXTVAL);
and that's all. No need to update rows once again, you won't get anything new because values already exist, and sequence was just renamed (so it continues with its values, i.e. you won't get duplicates which might happen if you dropped it and created a new one).

Add notnull constraint to a column of an existing table

How to add notnull constraint to a column of an existing table in which no value is yet inserted i.e. only table is created and now i want to alter the table
i am writing the following query
ALTER TABLE TABLENAME MODIFY COLUMNAME DATATYPE NOT NULL;
but oracle is throwing error "invalid alter table option"
In an alter table you only have to specify what changes
ALTER TABLE TABLENAME MODIFY COLUMNAME NOT NULL;
As you don't change the datatype, you don't have to specify it again.

how to add a column after another one in monetDB

I am trying to add a new column in a monetDB database and I want it positioned after a specific one. In mysql this is possible using the AFTER keyword.
ALTER TABLE myTable ADD myNewColumn VARCHAR(255) AFTER myOtherColumn
I am trying this in the mclient:
sql>ALTER TABLE dbname.table_name ADD COLUMN new_name AFTER existing_name SET DEFAULT NULL;
What I get is a syntax error:
syntax error, unexpected AFTER in: "ALTER TABLE dbname.table_name ADD COLUMN new_name AFTER"
It is true that the ALTER documentation does not specify that AFTER exists, but I am hoping that anybody knows an alternative.
The safe way is to create an new table with the columns properly ordered and move the data; you probably know this already.
However if you really cannot do that, create a view:
CREATE VIEW AS SELECT [order the columns however you want here] FROM your_table;

Add enum type column to table

im trying to add a new column to my existing table 'Results', and it seems to be very easy but I cant see the mistake.
Here is my code:
SQL> Alter table results add column CAL ENUM('A','B');
ERROR at line 1: ORA-00904: : invalid identifier
What am I missing?
I've read this from w3 and this from java2s but cant see the difference to mine.
Thanks, and sorry for the dumb question.
OK, with an ORA- error I am assuming that this is an oracle database, and not mysql. you have both tags and you are linking to MySQL example, but the error is not a MySQL error.
Assuming that this IS an Oracle DB, then there is no native ENUM data type. You have to do this as follows: First - you add the column with a correctly defined data type, and second you create a constrained list of permitted values on that column as a check constraint.
Alter table results add (cal varchar2(1));
Alter table results add constraint chk_cal CHECK (cal in ('A','B')) ENABLE;
(EDITED to fix brackets in constraint creation line)

Oracle drop constraint unique and re-add it

I've drop a constraint unique key with command:
ALTER TABLE table
DROP CONSTRAINT UNIQUE uk_nome;
it's removed because I don't see it, but when I try to re-add it with different parameter:
ALTER TABLE tale ADD CONSTRAINT UK_name UNIQUE (uk_1, uk_2);
I receive the error:
Errore SQL: ORA-00955: name is already used by an existing object.
Where is the problem?
You drop uk_nome and then add uk_name. I mean that there is a typo in uk_nome.
Resolved, the unique key is present, like index, into the system in the table dba_objects

Resources