How to name the constraint when add NOT NULL on ALTER TABLE - sql

ALTER TABLE ACTOR MODIFY FIRST_NAME VARCHAR2(45) NOT NULL;
I know have to set FIST_NAME to NOT NULL, but I don not know how to name this constraint.
The name of this constraint should be "CK_Fanme"

You can use
ALTER TABLE actor MODIFY ( first_name CONSTRAINT not_null_first_name NOT NULL );
and query to see the result through use of user_constraints data dictionary view such as
SELECT constraint_name
FROM user_constraints
WHERE table_name = 'ACTOR'
Demo

Related

What steps should we follow while changing the datatype of a primary key in oracle sql

I have a table similar to this.
CREATE TABLE customers (
customer_id NUMBER(7,0) NOT NULL,
customer_name VARCHAR2(50) NOT NULL,
CONSTRAINT customers_pk PRIMARY_KEY (customer_id)
);
with some values in the table.
I want to change the data type of the primary key customer_id to NUMBER(10, 0). So what steps do we follow before executing the ALTER command? (This column is not being referenced as a foreign key in any table)
Specifically,
We have an index on the primary column by default in oracle SQL. So should we drop the primary key constraint and then execute the ALTER command?
And any other considerations we need to make?
For case like yours, you don't have to do anything - just do it:
SQL> create table customers (
2 customer_id number(7, 0),
3 customer_name varchar2(50),
4 constraint customer_pk primary key (customer_id));
Table created.
SQL> insert into customers
2 select 1234566, 'Little' from dual union all
3 select 98876 , 'Foot' from dual;
2 rows created.
SQL> alter table customers modify customer_id number(8, 0);
Table altered.
SQL> select constraint_name from user_constraints where table_name = 'CUSTOMERS';
CONSTRAINT_NAME
------------------------------
CUSTOMER_PK
SQL>
But, if you had to make the column smaller or modify its datatype - that's another story. Lucky you, not yours.

How to set a primary key?

SELECT TOP 1000 [LicensePlate]
,[Manufacturer]
,[Model]
,[Colour]
,[Year]
,[EngineSize]
,[Value]
FROM [Cars2].[dbo].[Cartable1]
Above is my layout for a cars table. I am completely new to SQL and was wondering how I would set 'Licence Plate' as the primary key?
First find any duplicates by that column.
SELECT
C.LicensePlate,
AmountDuplicates = COUNT(*)
FROM
Cars2.dbo.Cartable1 AS C
GROUP BY
C.LicensePlate
HAVING
COUNT(*) > 1
If any record shows up, you need to either delete all the duplicates or update their license plates so they don't repeat anymore.
You will also need to check for NULL values and update or delete them (primary key can't be null).
SELECT
C.*
FROM
Cars2.dbo.Cartable1 AS C
WHERE
C.LicensePlate IS NULL
Then you can add the PRIMARY KEY constraint with:
ALTER TABLE Cars2.dbo.Cartable1
ADD CONSTRAINT PK_Cartable1 -- Name of the constraint
PRIMARY KEY (LicensePlate)
You might get an error if LicensePlate can hold NULL values. You can change it with an ALTER TABLE:
ALTER TABLE Cars2.dbo.Cartable1 ALTER COLUMN LicensePlate VARCHAR(20) NOT NULL -- The proper data type
If you already have a primary key defined on that table you will have to drop it and then create your new one (one table can only have 1 primary key constraint at a time). You can check which one is it with the following query:
USE Cars2; -- The database name here
DECLARE #TableName VARCHAR(100) = 'Cartable1'
DECLARE #SchemaName VARCHAR(100) = 'dbo'
SELECT
ColumnName = Col.Column_Name,
ConstraintName = tab.CONSTRAINT_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS Tab
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS Col ON
Col.Constraint_Name = Tab.Constraint_Name AND
Col.Table_Name = Tab.Table_Name
WHERE
Constraint_Type = 'PRIMARY KEY' AND
Col.Table_Name = #TableName AND
Col.TABLE_SCHEMA = #SchemaName
Once you see the result, you can drop the current primary key with another ALTER TABLE:
ALTER TABLE Cars2.dbo.Cartable1 DROP CONSTRAINT ConstraintNameFromThePreviousQuery
CREATE TABLE Cartable1(
LicensePlate int NOT NULL PRIMARY KEY,
Manufacturer varchar(255) NOT NULL,
Model varchar(255),
Colour varchar(255),
Model varchar(255),
Year int,
EngineSize int,
Value float
);
You do this when you create a table or through an alter table statement:
create table [Cars2].[dbo].[Cartable1] (
LicensePlace varchar(?) primary key,
. . .
);
I recommend adding the primary key when you create the table and before you add any data into it.
If You already have table with data try this ,
ALTER TABLE [Cars2].[dbo].[Cartable1]
ADD CONSTRAINT PRIMARY_KEY_LicensePlate PRIMARY KEY(LicensePlate)

create index, Oracle

I have Oracle database. I want to create INDEX:
CREATE INDEX indexID ON Employee(id_employee);
But it writes -> SQL Error: ORA-01408: such column list already indexed
So before create index I put:
DROP INDEX indexID;
But it writes -> SQL Error: ORA-01418: specified index does not exist
my Employee table:
CREATE TABLE Employee (
id_employee NUMBER(5) NOT NULL,
name VARCHAR(25) NOT NULL,
surname VARCHAR(25) NOT NULL,
day_of_birth DATE NOT NULL,
salary NUMBER(6) NOT NULL,
PRIMARY KEY(id_employee)
);
Have you some idea? it looks like index does not create.
You have a different index on that column.
Say you create the table like this:
SQL> CREATE TABLE Employee (
2 id_employee NUMBER(5) NOT NULL ,
3 name VARCHAR(25) NOT NULL,
4 surname VARCHAR(25) NOT NULL,
5 day_of_birth DATE NOT NULL,
6 salary NUMBER(6) NOT NULL
7 );
Table created.
Then you add the PK constraint:
SQL> alter table employee add primary key(id_employee);
Table altered.
Now Oracle already created a unique index on the PK field, so you already have it, with no need for manual creation.
SQL> select index_name, column_name
2 from user_ind_columns c
3 inner join user_indexes i
4 using (index_name)
5 where i.table_name = 'EMPLOYEE';
INDEX_NAME COLUMN_NAME
-------------------- --------------------
SYS_C007892 ID_EMPLOYEE
In your example:
SQL> CREATE TABLE Zamestnanec (
2 id_zamestnance NUMBER(5) PRIMARY KEY ,
3 jmeno VARCHAR(25) NOT NULL,
4 prijmeni VARCHAR(25) NOT NULL,
5 datum_narozeni DATE NOT NULL,
6 prava CHAR(3) CHECK(prava IN ('ano', 'ne')) NOT NULL,
7 plat NUMBER(6) NOT NULL
8 );
Table created.
SQL> select index_name, column_name
2 from user_ind_columns c
3 inner join user_indexes i
4 using (index_name)
5 where i.table_name = 'ZAMESTNANEC';
INDEX_NAME COLUMN_NAME
-------------------- --------------------
SYS_C007899 ID_ZAMESTNANCE
I slightly modified your syntax; besides, you can avoid the NOT NULL constraint on a PK field: the PK will force the field to be NOT NULL.
See here for a similar problem.
The CREATE TABLE for ZAMESTNANEC is
CREATE TABLE Zamestnanec (
id_zamestnance NUMBER(5) NOT NULL,
jmeno VARCHAR(25) NOT NULL,
prijmeni VARCHAR(25) NOT NULL,
datum_narozeni DATE NOT NULL,
prava CHAR(3) CHECK(prava IN ('ano', 'ne')) NOT NULL,
plat NUMBER(6) NOT NULL,
PRIMARY KEY(id_zamestnance)
);
You've created a primary key constraint for ID_ZAMESTNANCE, which by default creates an index on ID_ZAMESTNANCE; thus you don't need to create another index on ID_ZAMESTNANCE.
I would suggest a freakish solution, but I guarantee you that it will work.
Drop the primary key constraint from the table.
ALTER TABLE Employee
DROP CONSTRAINT pk_id_employee
Drop the index, then…
Create the primary key constraint

Turn existing column into a primary key

create table emp
(
emp_id int,
emp_name varchar(40) not null,
emp_address varchar(35) not null,
)
Now I need to add primary key to emp_id . I tried using alter query
alter table emp add constraint emp_id primary key
its showing error as
Table level constraint does not specify column list, table 'emp'.
ALTER TABLE emp
ADD PRIMARY KEY (emp_id)
OR
ALTER TABLE emp
ADD CONSTRAINT pk_empID PRIMARY KEY (emp_id)

SQL Server 2008 select constraints order by primary key

I have the following SQL statement to retrieve drop constraint statements.
SELECT DISTINCT 'ALTER TABLE '+TABLE_NAME+' DROP CONSTRAINT '+CONSTRAINT_NAME AS 'DropConstraintStatement'
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE TABLE_NAME = 'USER';
The result of this query is:
ALTER TABLE UNITS DROP CONSTRAINT FK_USER_TASK_ID
ALTER TABLE UNITS DROP CONSTRAINT PK_USER
ALTER TABLE UNITS DROP CONSTRAINT UQ_USER_NAME_VERSION
I want to execute these statements within a Java application, hence I need these statements ordered such that the drop primary key constraint is at the last position like:
ALTER TABLE UNITS DROP CONSTRAINT FK_USER_TASK_ID
ALTER TABLE UNITS DROP CONSTRAINT UQ_USER_NAME_VERSION
ALTER TABLE UNITS DROP CONSTRAINT PK_USER
Is there any nice possibility to do this with native SQL?
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE does not hold the necessary information to distinguish between PK and other constraints. If you are sure your PK names start with PK
you could try adding ORDER BY like this:
ORDER BY CASE WHEN CONSTRAINT_NAME LIKE 'PK%' THEN 1 ELSE 0 END
However, I would suggest switching to SQL Server system views for more precise data. sys.key_constraints holds info about primary keys and unique constraints, and sys.foreign_keys about foreign keys.
WITH CTE AS
(
SELECT OBJECT_NAME(parent_object_id) AS TABLE_NAME
, name AS CONSTRAINT_NAME, Type
FROM sys.key_constraints
WHERE parent_object_id = object_id('YourTable')
UNION
SELECT OBJECT_NAME(parent_object_id) AS TABLE_NAME
, name AS CONSTRAINT_NAME, Type
FROM sys.foreign_keys
WHERE parent_object_id = object_id('YourTable')
)
SELECT 'ALTER TABLE '+TABLE_NAME+' DROP CONSTRAINT '+CONSTRAINT_NAME AS 'DropConstraintStatement'
FROM CTE
ORDER BY CASE WHEN Type = 'PK' THEN 1 ELSE 0 END