Im trying to build a sample database for a project about a real estate agency,on the table about the realties i have a column Realtie_id and i want it to start with 11%%% what type should it be (int or varchar ) and how do I make the constraint ?
create table Realties (
rid int not null,
address varchar(50),
m2 real not null,
r_type varchar(20),
primary key (rid),
constraint c_rid check(rid in (.....
);
It depends on what you'll be storing in there.
if it is a string, use VARCHAR2
if it is a number, use NUMBER (or INT)
Constraint in any case might be
SQL> create table realties
2 (rid int constraint ch_rid check (substr(to_char(rid), 1, 2) = '11'));
Table created.
SQL> insert into realties (rid) values ('abc');
insert into realties (rid) values ('abc')
*
ERROR at line 1:
ORA-01722: invalid number
SQL> insert into realties (rid) values ('245');
insert into realties (rid) values ('245')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_RID) violated
SQL> insert into realties (rid) values ('1245');
insert into realties (rid) values ('1245')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CH_RID) violated
SQL> insert into realties (rid) values ('11245');
1 row created.
SQL>
If your value is a number then make the column a numeric data type. The exception to this is when your value is a number string that can start with zeroes, like a phone number, when it should be a string data type; otherwise if you use a number data type then the, semantically important, leading zeroes will be stripped off. Since you want the number to start with 11 then this caveat does not apply.
As for the CHECK constraint, you can use LIKE '11%' and Oracle will implicitly convert it to a string to perform the check.
create table Realties (
rid int
not null
CONSTRAINT realties__rid__pk PRIMARY KEY
CONSTRAINT realties__rid__chk CHECK ( rid LIKE '11%' ),
address varchar(50),
m2 real
not null,
r_type varchar(20)
);
Related
I have this table and i added a check to the column duree
with this condition alter table operation modify Duree integer check(Duree>=2);
but in the homework it's specified that it needs to be like (duree>=44) isntead
how this be fixed.
create table Operation (
CodeOP varchar(20) not null ,
Duree integer ,
Chef integer ,
DateDeb Date ,
Budget float ,
CodeS varchar (20),
constraint pk_operation primary key (CodeOP),
constraint fk_operation foreign key(CodeS)REFERENCES Service(CodeS) on delete set null
i tried alter table operation modify Duree integer check(Duree>=44);
but i'm getting this message :
ERROR at line 1: ORA-02293: cannot validate (TPBDD2.SYS_C008292) - check constraint violated
I have added a attribute by altering the TYPE with char datatype for existing type in Oracle DB as
ALTER TYPE MDPHEADEROBJ ADD ATTRIBUTE EMPNBR char(10) CASCADE
TYPE "MDPHEADEROBJ" IS OBJECT (
POSTXNDATE DATE,
LOCNBR NUMBER (13),
TRMNNBR CHAR (3),
POSTXNNBR NUMBER (13),
TOTSLSAMT NUMBER (13, 2),
VDDRGTXNIND CHAR (1),
TXNVDITMIND CHAR (1),
CSHRNBR CHAR (6),
SPRNNBR CHAR (10),
ITEMCNT NUMBER,
TAXCNT NUMBER,
TENDERCNT NUMBER,
DSCNTCNT NUMBER
)
DDL of the table looks like
CREATE TABLE "POS"."SLS_TXN_T"
( "SLS_TXN_MO_NBR" NUMBER(3,0) NOT NULL ENABLE,
"POS_TXN_TM_DT" DATE NOT NULL ENABLE,
"LOC_NBR" NUMBER(13,0) NOT NULL ENABLE,
"TRMN_NBR" CHAR(3) NOT NULL ENABLE,
"POS_TXN_NBR" NUMBER(13,0) NOT NULL ENABLE,
"PVOID_TXN_TM_DT" DATE,
"PVOID_LOC_NBR" NUMBER(13,0),
"PVOID_TRMN_NBR" CHAR(3),
"PVOID_POS_TXN_NBR" NUMBER(13,0),
"PVOID_RSN_CD" CHAR(2),
"TOT_SLS_AMT" NUMBER(13,2),
"VD_DRG_TXN_IND" CHAR(1),
"VD_ITM_IND" CHAR(1),
"CSHR_NBR" CHAR(6),
"SPRN_NBR" CHAR(10),
"SLS_UPDT_DT" DATE DEFAULT sysdate NOT NULL ENABLE,
"EMP_NBR" CHAR(10) DEFAULT null,
CONSTRAINT "TXN_T_LOC_NBR_CK" CHECK ("LOC_NBR" IS NOT NULL) ENABLE NOVALIDATE,
CONSTRAINT "TXN_T_TRMN_NBR_CK" CHECK ("TRMN_NBR" IS NOT NULL) ENABLE NOVALIDATE,
CONSTRAINT "TXN_T_TXN_NBR_CK" CHECK ("POS_TXN_NBR" IS NOT NULL) ENABLE NOVALIDATE,
CONSTRAINT "TXN_T_TXN_TM_DT_CK" CHECK ("POS_TXN_TM_DT" IS NOT NULL) ENABLE NOVALIDATE
)
Stored procedure select query looks like this:
SELECT
pos_txn_tm_dt, loc_nbr, trmn_nbr, pos_txn_nbr, tot_sls_amt,
vd_drg_txn_ind, vd_itm_ind, cshr_nbr , sprn_nbr, emp_nbr
FROM
SLS_TXN_T
WHERE
loc_nbr = '5548'
AND trmn_nbr = '060'
AND pos_txn_nbr = '261'
AND (vd_drg_txn_ind IS NULL OR vd_drg_txn_ind <> 'Y');
Sample data how it looks int the table
CSHNBR SPRNNBR EMPNBR
000000 0000 01234567
Here I've a quick question:
From the above fields we many char declared variables CSHRNBR, SPRNNBR, EMPNBR etc. For example: if CSHRNBR is saved as 0012356 while fetching the data through a stored procedure I'm able to get the actual value from the DB as same 0012356. But for the newly added attribute in type empNbr if the value is saved as 0012356 while fetching it is coming as 12356 leading zero's are disappeared.
Looks strange both are declared with same datatype char. Please help me why this is happening?
Without more information about what you are doing/seeing and what errors are being produced we can not provide much more assistance than whats currently in the comments. Running a test that I believe to be close to what you are doing does not show the problem mentioned:
SQL> drop table ttch purge;
Table TTCH dropped.
SQL> drop type tch;
Type TCH dropped.
SQL> /
SQL> create or replace type tch as object (empnbr number, empchr char(5));
2 /
Type TCH compiled
SQL>
SQL> create table ttch (empl tch);
Table TTCH created.
SQL>
SQL> insert into ttch values (tch(1,'00001'));
1 row inserted.
SQL>
SQL> select t.empl.empnbr, t.empl.empchr from ttch t;
EMPL.EMPNBR EMPL.
----------- -----
1 00001
SQL>
SQL> alter type tch add attribute nemp char(4) cascade;
Type TCH altered.
SQL>
SQL> insert into ttch values (tch(2,'00002','0020'));
1 row inserted.
SQL>
SQL>
SQL> select t.empl.empnbr, t.empl.empchr, t.empl.nemp from ttch t;
EMPL.EMPNBR EMPL. EMPL
----------- ----- ----
1 00001
2 00002 0020
Hello there i am studying for the Oracle Certification of SQL Associate .
And trying to do some Examples .
I have an issue where i cannot find easily a reference on this .
create table employees
(employee_id number NOT NULL,
first_name varchar(20),
last_name varchar(30),
constraint employee_pk primary key (employee_id));
create table employee_notes
(employee_notes_id number,
employee_notes varchar(500),
constraint pk_employee_notes primary key (employee_notes_id));
create sequence employee_notes_seq start with 1 increment by 1
Now i want to add a new column at employee_notes table with a foreign key constraint .
I can't find out in syntax where is the problem .
****alter table employee_notes
add employee_id number
constraint fk_employee_notes foreign key (employee_id) references employees (employee_id);****
i get this error
ORA-02253: constraint specification not allowed her
I also tried to alter the table and add column and then the constraint but cannot
alter table employee_notes
add employee_id number;
--
alter table employee notes
add constraint fk_employee_notes foreign key (employee_id) references employees (employee_id);
ORA-02253: constraint specification not allowed here
I would like to know how i can do this
and why this syntax is wrong :)
You did something wrong because - it works OK:
SQL> CREATE TABLE employees
2 (
3 employee_id NUMBER NOT NULL,
4 first_name VARCHAR (20),
5 last_name VARCHAR (30),
6 CONSTRAINT employee_pk PRIMARY KEY (employee_id)
7 );
Table created.
SQL>
SQL> CREATE TABLE employee_notes
2 (
3 employee_notes_id NUMBER,
4 employee_notes VARCHAR (500),
5 CONSTRAINT pk_employee_notes PRIMARY KEY (employee_notes_id)
6 );
Table created.
SQL> ALTER TABLE employee_notes ADD employee_id NUMBER;
Table altered.
SQL> ALTER TABLE employee_notes ADD CONSTRAINT fk_employee_notes
2 FOREIGN KEY (employee_id)
3 REFERENCES employees (employee_id);
Table altered.
SQL>
When you use ALTER TABLE ... ADD in order to add a column and a constraint in one statement, do the following:
-- notice the () and the comma!
alter table employee_notes
add (
employee_id number
, constraint fk_employee_notes
foreign key (employee_id) references employees (employee_id)
) ;
That should do the trick. See dbfiddle. The syntax is similar to CREATE TABLE, where you'd also write all column names, data types etc in (), separated by commas.
I've tried to debug this code to the best of my ability to eliminate the possibility of little mistakes being the reason these errors are occurring but I keep getting two different errors on three create table statements.
The CREATE TABLE SECTION statement gives an invalid identifier error on line 7 pointing to course#, here's my code:
CREATE TABLE SECTION
(SECTION# VARCHAR2(8) constraint pk_section# primary key,
TIME CHAR(5),
MAXST NUMBER(2),
ROOM VARCHAR2(14),
constraint chk_maxst check(maxst<=35),
constraint fk_crs foreign key(course#)
REFERENCES course(course#),
constraint fk_pro foreign key(empid)
REFERENCES professor(empid));
The CREATE TABLE TAKES statement gives an invalid identifier error on line 4 pointing to section#, here's my code:
CREATE TABLE TAKES
(GRADE CHAR(5) constraint nn_grade not null,
constraint chk_grade check(grade IN ('A','B','C')),
constraint fk_sec foreign key(section#)
REFERENCES section (section#),
constraint fk_stu foreign key(sid)
REFERENCES student(sid));
Full Context:
drop table professor cascade constraints;
drop table course cascade constraints;
drop table student cascade constraints;
drop table section cascade constraints;
drop table takes cascade constraints;
CREATE TABLE PROFESSOR
(NAME CHAR(15) constraint nn_name not null,
EMPID VARCHAR2(8) constraint pk_empid primary key,
PHONE NUMBER(10),
DATEHIRED DATE,
SALARY NUMBER);
CREATE TABLE COURSE
(NAME CHAR(24) constraint nn_names not null,
COURSE# CHAR(10) constraint pk_course# primary key,
CREDIT CHAR(6) constraint nn_credit not null,
COLLEGE CHAR(20),
HRS NUMBER(1),
constraint chk_credit check(credit IN('U','G')),
constraint chk_college check(college IN ('Arts and Sciences','Education','Engineering','Business')),
constraint chk_course check((credit='U' AND hrs<=4) OR (credit = 'G' AND hrs=3)),
constraint unq_course unique(name, college));
CREATE TABLE STUDENT
(SID VARCHAR2(7) constraint pk_sid primary key,
NAME CHAR(14),
ADDRESS CHAR(22),
BIRTHDATE DATE,
GRADELEVEL CHAR(2) constraint nn_glvl not null);
CREATE TABLE SECTION
(SECTION# VARCHAR2(8) constraint pk_section# primary key,
TIME CHAR(5),
MAXST NUMBER(2),
ROOM VARCHAR2(14),
constraint chk_maxst check(maxst<=35),
constraint fk_crs foreign key(course#)
REFERENCES course(course#),
constraint fk_pro foreign key(empid)
REFERENCES professor(empid));
CREATE TABLE TAKES
(GRADE CHAR(5) constraint nn_grade not null,
constraint chk_grade check(grade IN ('A','B','C')),
constraint fk_sec foreign key(section#)
REFERENCES section (section#),
constraint fk_stu foreign key(sid)
REFERENCES student(sid));
Textbook references:
[https://drive.google.com/open?id=1eDdBShzgnSjISqxByJ7FKgbkLCEwXzpd][1]
[https://drive.google.com/open?id=1WhDsgQy2xSwjxVMqDzaGOcBh7zSokneT][2]
[https://drive.google.com/open?id=12N51OCEucRn_unagqHYsqufEGK3tKJH_][3]
Did you follow documentation that describes how you are supposed to do what you are doing? Because, it seems that you didn't pay much attention at classes, nor read documentation and tend to make up things, doing something that is either wrong or doesn't ever exist.
Consider deleting all that mess and starting over.
Here are some guidelines; try to fix those mistakes, come back if it still doesn't work.
In the COURSE table:
don't use CHAR but VARCHAR2 data type
you can't create constraints on non-existent columns (for example, a check constraint on CRS_CREDIT column, while you named the column as CREDIT)
In the SECTION table:
don't use CHAR data type
you can't use columns (in FOREIGN KEY constraints) that don't exist in the SECTION table (such as SEC_CRS_COURSE#), nor in the table you reference (such as CRS_COURSE# in the COURSE table)
there are no ON UPDATE CASCADE nor ON DELETE RESTRICT in Oracle
The same goes for the TAKES table.
[EDIT, after you almost made it work]
Congratulations! You're now so close! SECTION and TAKES table need some adjustment (missing columns - have a look, I marked them with a comment) and then tables are successfully created.
Once again (as you won't listen): get rid of CHAR data type columns - use VARCHAR2 instead.
SQL> CREATE TABLE PROFESSOR
2 (
3 NAME CHAR (15) CONSTRAINT nn_name NOT NULL,
4 EMPID VARCHAR2 (8) CONSTRAINT pk_empid PRIMARY KEY,
5 PHONE NUMBER (10),
6 DATEHIRED DATE,
7 SALARY NUMBER
8 );
Table created.
SQL>
SQL> CREATE TABLE COURSE
2 (
3 NAME CHAR (24) CONSTRAINT nn_names NOT NULL,
4 COURSE# CHAR (10) CONSTRAINT pk_course# PRIMARY KEY,
5 CREDIT CHAR (6) CONSTRAINT nn_credit NOT NULL,
6 COLLEGE CHAR (20),
7 HRS NUMBER (1),
8 CONSTRAINT chk_credit CHECK (credit IN ('U', 'G')),
9 CONSTRAINT chk_college CHECK
10 (college IN ('Arts and Sciences',
11 'Education',
12 'Engineering',
13 'Business')),
14 CONSTRAINT chk_course CHECK
15 ( (credit = 'U' AND hrs <= 4) OR (credit = 'G' AND hrs = 3)),
16 CONSTRAINT unq_course UNIQUE (name, college)
17 );
Table created.
SQL>
SQL> CREATE TABLE STUDENT
2 (
3 SID VARCHAR2 (7) CONSTRAINT pk_sid PRIMARY KEY,
4 NAME CHAR (14),
5 ADDRESS CHAR (22),
6 BIRTHDATE DATE,
7 GRADELEVEL CHAR (2) CONSTRAINT nn_glvl NOT NULL
8 );
Table created.
SQL>
SQL> CREATE TABLE SECTION
2 (
3 SECTION# VARCHAR2 (8) CONSTRAINT pk_section# PRIMARY KEY,
4 TIME CHAR (5),
5 MAXST NUMBER (2),
6 ROOM VARCHAR2 (14),
7 course# CHAR (10), -- added by LF
8 empid VARCHAR2 (8), -- added by LF
9 CONSTRAINT chk_maxst CHECK (maxst <= 35),
10 CONSTRAINT fk_crs FOREIGN KEY (course#) REFERENCES course (course#),
11 CONSTRAINT fk_pro FOREIGN KEY (empid) REFERENCES professor (empid)
12 );
Table created.
SQL>
SQL> CREATE TABLE TAKES
2 (
3 GRADE CHAR (5) CONSTRAINT nn_grade NOT NULL,
4 section# VARCHAR2 (8), -- added by LF
5 sid VARCHAR2 (7), -- added by LF
6 CONSTRAINT chk_grade CHECK (grade IN ('A', 'B', 'C')),
7 CONSTRAINT fk_sec FOREIGN KEY (section#) REFERENCES section (section#),
8 CONSTRAINT fk_stu FOREIGN KEY (sid) REFERENCES student (sid)
9 );
Table created.
SQL>
I've executed following query in toad:
CREATE TABLE ACTWEB.usuarios
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
matricula INTEGER,
nome CHAR(50) NOT NULL,
senha CHAR(50),
nivel INTEGER,
maleta INTEGER,
email CHAR(50),
acessos INTEGER,
datacriacao DATE,
dataalteracao DATE
UNIQUE (id)
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL;
And got this ORA message:
ORA-00907 missing right parenthesis
Cause: A left parenthesis has been entered without a closing right parenthesis, or extra information was contained in the parentheses. All parentheses must be entered in pairs.
Action: Correct the syntax and retry the statement.
There are a couple of errors in your syntax. The NOT NULL has to occur after the IDENTITY clause. The range has to be specified without a , and the UNIQUE keyword has to appear at the column directly:
CREATE TABLE ACTWEB.usuarios
(
id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL UNIQUE,
matricula INTEGER,
nome CHAR(50) NOT NULL,
senha CHAR(50),
nivel INTEGER,
maleta INTEGER,
email CHAR(50),
acessos INTEGER,
datacriacao DATE,
dataalteracao DATE
)
LOGGING NOCOMPRESS NOCACHE NOPARALLEL;
You specified unique not right, to solve it you have several ways:
First, create unique key explicitly:
create table tab1(
id number(10),
CONSTRAINT id_uk UNIQUE (id)
)
Second, create unique key as an option of the column:
create table tab1(
id number(10) UNIQUE
)
Third, add the unique key constraint using alter table:
create table tab1(
id number(10)
);
alter table tab1 add constraint id_uk unique(id);
Forth, create unique key implicitly by creating unique index:
create table tab1(
id number(10)
);
CREATE UNIQUE INDEX id_uk ON tab1 (id);