How do i add a column in oracle with Enum values? - sql

How to use enums in Oracle?
The above post gives me an option to create a Enum column while creating a table. But I have a table that is having values. I wanted to add another column with Enum values.
ALTER TABLE CARS **(ADD** BODY_TYPE VARCHAR2(20)
CHECK (BODY_TYPE IN ('COUPE','SEDAN','SUV')) );
I'am getting a syntax error near ADD. Please guide.

Place "add" before "(".
alter table cars
add
(
body_type varchar2(20) not null check (body_type in ('COUPE','SEDAN','SUV'))
);

Related

Why is the column not altering when I try to convert it to UUID?

I have a primary key column in my SQL table in PostgreSQL named "id". It is a "bigseries" column. I want to convert the column to a "UUID" column. It entered the below command in the terminal:
alter table people alter column id uuid;
and
alter table people alter column id uuid using (uuid_generate_v4());
but neither of them worked.
In both tries I got the error message
ERROR: syntax error at or near "uuid"
LINE 1: alter table people alter column id uuid using (uuid_generate...
What is the correct syntax?
First of all uuid_generate_v4() is a function which is provided by an extension called uuid-ossp. You should have install that extension by using;
CREATE EXTENSION uuid-ossp;
Postgresql 13 introduced a new function which does basically the same without installing extension. The function is called gen_random_uuid()
Suppose that we have a table like the one below;
CREATE TABLE people (
id bigserial primary key,
data text
);
The bigserial is not a real type. It's a macro which basically creates bigint column with default value and a sequence. The default value is next value of that sequence.
For your use case, to change data type, you first should drop the old default value. Then, alter the type and finally add new default value expression. Here is the sample:
ALTER TABLE people
ALTER id DROP DEFAULT,
ALTER id TYPE uuid using (gen_random_uuid() /* or uuid_generate_v4() */ ),
ALTER id SET DEFAULT gen_random_uuid() /* or uuid_generate_v4() */ ;
CREATE TABLE IF NOT EXISTS people (
id uuid NOT NULL CONSTRAINT people_pkey PRIMARY KEY,
address varchar,
city varchar(255),
country varchar(255),
email varchar(255),
phone varchar(255)
);
This is the correct syntax to create table in postgres SQL, it's better to do these constraints at beginning to avoid any error.
For using alter command you would do the following:
ALTER TABLE customer ADD COLUMN cid uuid PRIMARY KEY;
Most of errors that you could find while writing command either lower case or undefined correct the table name or column.

How to add NOT NULL constraint along with default value for the column?

I have a column 'name' in student table. I need to add NOT NULL constraint on this column. But I get SQL error saying cannot add null constraint since the existing rows in the table has null values in the column. How would I add a null constraint along with default value in a single alter statement. Below is my query.
alter table Student alter column name nvarchar NOT NULL;
SQL Server does not make this easy. I think the only way is to set the existing values to the default that you want. Then change the column to have a default value and not null:
-- Get rid of the existing `NULL` values
update students set name = '' where name is null;
-- Add the NOT NULL constraint
alter table students
alter column name varchar(255) not null;
-- Add a default value
alter table students
add constraint df_t_name default '' for name ;
Here is what it looks like in practice.
But I get SQL error saying cannot add null constraint since the existing rows in the table has null values in the column.
Have you tried overwriting the existing NULL values?
You can't have a constraint on a column when existing values would violate that constraint. You need to make your table compliant first.

altering the data type of a column

In the previous part of this problem I had to create a PHONE_LIST_TYPE varray(3).
A. Define a user-defined object type data type named phone_typewith attributes COUNTRY_CODE, AREA_CODE and PHONE_NUMBER.
B.Define a user-defined VARRAY data type named Phone_List_type as an array of size three of the type phone_type.
CREATE OR REPLACE TYPE phone_list_type AS VARRAY(3) of PHONE_TYPE
c.Modify the table STUDENT1 such that the attribute PHONE is of data type Phone_List_type.
ALTER TABLE student1
ALTER COLUMN phone phone_list_type not null;
There is a syntax error line under the alter column portion and I cant figure out how to resolve this error.
Presuming previously you have :
CREATE TYPE PHONE_TYPE AS OBJECT ( COUNTRY_CODE int,
AREA_CODE int,
PHONE_NUMBER varchar2(15) );
CREATE TABLE student1( id int, phone varchar2(15) );
To be able change the data type you normally need the syntax :
ALTER TABLE student1
MODIFY ( phone phone_type );
But in this case you'd get ORA-22858 : invalid alteration of datatype error. Because it's not possible to convert a column with basic type(string,numeric...) to a composite one in a classical manner like the way among them (by the way, also in that case, the column which's data type will be converted should be empty).
So, add a new column with a temporary name :
ALTER TABLE student1 ADD (phone2 phone_type);
And, populate the phone_number component of that new column with the existing data :
UPDATE student1 s
SET s.phone2.phone_number = phone;
Lastly, drop and rename with the existing name of the column :
ALTER TABLE student1 DROP COLUMN phone;
ALTER TABLE student1 RENAME COLUMN phone2 TO phone;
Depends on what database are you using. Here are the mst common ones:
SQL Server / MS Access:
ALTER TABLE student1
ALTER COLUMN phone phone_list_type NOT NULL;
My SQL / Oracle(before v. 10G):
ALTER TABLE student1
MODIFY COLUMN phone phone_list_type NOT NULL;
Oracle 10G and later:
ALTER TABLE student1
MODIFY phone phone_list_type NOT NULL;

How to add a column in MS Access using SQL?

Everytime I try to add a column using ALTER and ADD COLUMN I am getting a syntax error.
ALTER TABLE EMP_1
ADD COLUMN EMP_PCT NUMBER(4,2),
ADD COLUMN PROJ_NUM CHAR(3);
I am not familiar with a number data type. Perhaps you mean decimal:
ALTER TABLE EMP_1 ADD COLUMN EMP_PCT DECIMAL(5, 2);
You may also need two ALTER TABLE statements. I am just not sure if MS Access allows two changes in one statement.
For a numeric type you should use NUMERIC, not NUMBER, and for a text type you should use VARCHAR.

Can not add a column to existing table

I have a table viz. expenses with three columns as under
ExpenseId int NOT NULL,
ExpenseName varchar(50) NOT NULL,
Invalid bit NOT NULL
To add a new column (OldCode char(4) not null), I used design feature for tables in Microsoft SQL Server Management Studio. But I get following error
'Expenses' table
- Unable to modify table. Cannot insert the value NULL into column 'OldCode', table 'TransportSystemMaster.dbo.Tmp_Expenses'; column does not allow nulls. INSERT fails. The statement has been terminated.
Incidentally I have been able to add same column with same specifications to other tables of the same database.
Any help?
Your Table Consist of Existing Records
and you are pushing a new column of type NOT NULL.
so for older records the data have to be something.
try something like this
ALTER TABLE MY_TABLE ADD Column_name INT NULL
GO
UPDATE MY_TABLE <set valid not null values for your column>
GO
ALTER TABLE MY_TABLE ALTER COLUMN Column_name INT NOT NULL
GO
Since OldCode is NOT NULL, you should specify a default value for it.
when you have some rows on your table you can't add a column that is not nullable you should provide a default value for it
Alter Table table_name add OldCode int not null DEFAULT(0);
You have to specify values for all the 4 fields of the table, its purely because, while designing the table you set the definition of the columns to be not null. Again you are adding a new column called OldCode and setting to be not null, all ready existing records hasn't got a value. So that is the reason its complains