I'm, new to SQL. I have created few tables:
CREATE TABLE MAINTINANCE
(Maint_mname char(10),
Maint_date date,
Maint_duedate date NOT NULL,
Maint_mdesc char (15));
CREATE TABLE DESIGNERR
(Dez_emp_number varchar(11),
Dez_field char(12),
Dez_qualification char(10) NOT NULL,
Dez_experience smallint);
For the first table I am adding the following constraint:
ALTER TABLE MAINTINANCE ADD CONSTRAINT CHK_maintdate CHECK(Maint_date<MAint_duedate);
but I am getting the error invalid ALTER TABLE option. Could you please let me know why this is appearing? The same is working for a friend but not for me.
For the second table I have to write a SQL command for the business rule:
If the Qualification of a Designer is BS then a Minimum of 4 years
Experience is required. But, if the Qualification of the Designer is
MS then a Minimum of 2 years Experience is sufficient.
How can we define this business rule in SQL?
The code you posted works
SQL> CREATE TABLE MAINTINANCE
2 (Maint_mname char(10),
3 Maint_date date,
4 Maint_duedate date NOT NULL,
5 Maint_mdesc char (15));
Table created.
SQL> ALTER TABLE MAINTINANCE ADD CONSTRAINT CHK_maintdate CHECK(Maint_date<MAint_duedate);
Table altered.
If you're getting an error,
Cut and paste from a SQL*Plus session that shows exactly what statements you are executing
Post the full error stack
Although it does not affect your code, the word "MAINTINANCE" is misspelled. It should be "Maintenance". Future human developers will be grateful if your table names are spelled correctly.
Related
I'm making two tables, STUDENT and STUDENTREPORT. I made STUDENT, but not the other. I ran the code, but it says missing or invalid option.
My STUDENTREPORT command:
Both commands are correctly written, they execute OK in e.g. SQL*Plus or SQL Developer:
SQL> CREATE TABLE studentreport (
2 sr_number VARCHAR2(5),
3 sr_rade VARCHAR2(5),
4 sr_semester VARCHAR2(5),
5 class_attended NUMBER,
6 s_id VARCHAR2(5),
7 PRIMARY KEY ( sr_number )
8 USING INDEX enable
9 );
Table created.
SQL> ALTER TABLE studentreport
2 ADD FOREIGN KEY ( s_id )
3 REFERENCES student ( s_id )
4 ENABLE;
Table altered.
SQL>
But, in Oracle Apex' SQL Workshop, you can execute only one command at a time. Therefore:
remove alter table (delete it from the editor) so that you'd first execute create table; then delete that statement and execute alter table, or
select (with a mouse, so that text turns blue) create table and hit RUN to execute it; then select alter table and execute it with RUN
That's just how SQL Workshop behaves, there's nothing you can do about it (at least, I don't know what you could/should do, apart from what I already said).
I have an assignment asking me to add a column to a table and give it a constraint.
"Add a column named Base_Salary with a datatype of NUMBER(7,2) to the
store_reps table. Ensure that the amount entered is above 0."
I thought it was pretty straight forward, but maybe I have to ALTER the table twice, once for the new column, and again for adding the constraints? That seems redundant and inefficient.
I've tried taking off the second 'ADD' and moving around the parentheses, and moving the commas, it seems there is a similar error for everything I do.
This is my attempt:
ALTER TABLE store_reps;
ADD (Base_Salary NUMBER(7, 2)),
ADD CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0);
I get an error
ADD CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0)
ERROR report -
UNKNOWN COMMAND
and another:
Error starting at line : 74 in command -
ALTER TABLE store_reps
ADD (Base_Salary NUMBER(7, 2)),
CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0)
Error report -
Any help is greatly appreciated. I just started SQL a few weeks ago, and am very much noob.
Your query contains two syntax problems that need to be fixed:
get rid of the comma before CONSTRAINT
get rid of the extra pair of braces around Base_Salary NUMBER(7, 2)
Working example:
create table store_reps (id number);
ALTER TABLE store_reps
ADD Base_Salary NUMBER(7, 2)
CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0);
create table tr (col1 number);
alter table tr add col2 varchar2(100) constraint tr_cons CHECK (col2 = 'a');
DB Fiddle demo
Cheers!!
I have created one table in PL SQL Developer.
CREATE TABLE Patient_List
(
Patient_ID number NOT NULL,
Patient_Name varchar(50) NOT NULL,
Patient_Address varchar(100) NULL,
App_Date date NULL,
Descr varchar(50),
CONSTRAINT patient_pk PRIMARY KEY(Patient_ID)
);
I want to auto increment Patient_ID, I tried altering the table and modifying the Patient_ID column but it's showing an error "invalid ALTER TABLE option"
ALTER TABLE Patient_List
MODIFY Patient_ID NUMBER NOT NULL GENERATED ALWAYS AS IDENTITY;
Please help, Thanks in advance.
This is not possible.
Oracle 10g didn't even have identity columns, they were introduced in Oracle 12.1
But even with a current Oracle version, you can't convert a regular column to an identity column. You would need to add a new one.
Before identity columns, the usual way was to create a sequence and a trigger to populate the column.
See here: How to create id with AUTO_INCREMENT on Oracle?
If anybody wants to modify existing column as auto_increment use this three lines
alter table Product drop column test_id;
create sequence Product_test_id_seq INCREMENT BY 1 nocache;
alter table Product add test_id Number default Product_test_id_seq.nextval not null;
I am new to SQL and I am trying to run a CREATE TABLE query in Ms Access 2016 but I get an error saying that "mytablename" already exits which can't be true because I also ran a DROP TABLE "mytablename" query and I got an error saying "mytablename" does not exist. Please help. Point me in the right direction at least. Here is the CREATE TABLE query.
CREATE TABLE Team(
Team_ID AUTOINCREMENT UNIQUE NOT NULL,
Name VARCHAR(40) NOT NULL,
Origin VARCHAR(40) NOT NULL,
NetWorth CURRENCY NOT NULL,
PRIMARY KEY(Team_ID)
);
See check by VBA and check by SQL for check existence of your database.
If table exists you can recreate (drop and create again) table. Alternative way is to create table if table is not exist and do nothing if table exists.
This question already has answers here:
Set ORACLE table fields default value to a formular
(2 answers)
Closed 8 years ago.
I got a table which I used the below code to create.
create table Meter (MeterID CHAR(8) CONSTRAINT MeterPK PRIMARY KEY,
Value CHAR(8) CONSTRAINT ValueNN NOT NULL,
InstalledDate Date CONSTRAINT InDateNN NOT NULL);
Then I tried adding a derived column that adds 6 months to the installeddate.
alter table meter add ExpiryDate as (add_months(installedDate,6)) not null;
This returns an error of invalid datatype.
I read somewhere that I do not have to specify the datatype of ExpiryDate as it can be derived from the function. So where did I go wrong?
EDIT: Turns out Mike was right. I used the trigger method to get things going, but I was confused whether I'm using mysql or oracle. Think in the end I'm using oracle actually. Have problems with the trigger but turns out I do not need to have the command "set" in the trigger. Below is the code that works.
CREATE OR REPLACE
TRIGGER trigexpdate1
BEFORE INSERT ON Meter
FOR EACH ROW
BEGIN
:NEW.ExpiryDate := ADD_MONTHS(:NEW.InstalledDate, 6);
END;
If I don't have the begin and end in the statement, it will throw an error saying illegal trigger specification.
MySQL doesn't support
derived columns in table definitions,
a function named add_months(), or
inline constraints.
This is a more or less standard way to write that statement in MySQL.
create table `Meter` (
`MeterID` CHAR(8) NOT NULL,
`Value` CHAR(8) NOT NULL,
`InstalledDate` Date NOT NULL,
primary key (`MeterID`)
);
You have two options for a derived column like "ExpiryDate".
Create a view, and do the date arithmetic in the view. Use date_add().
Add the column "ExpiryDate" to the table, and keep it up-to-date with a trigger.
BEFORE INSERT trigger example
create table `Meter` (
`MeterID` CHAR(8) NOT NULL,
`Value` CHAR(8) NOT NULL,
`InstalledDate` Date NOT NULL,
`ExpiryDate` Date not null,
primary key (`MeterID`)
);
create trigger trigexpdate1
before insert on `Meter`
FOR EACH ROW
SET NEW.`ExpiryDate` = date_add(NEW.`InstalledDate`, interval 6 month);
Note how ExpiryDate changes from the insert statement to the select statement below.
insert into Meter
values ('1', '1', '2014-07-01', '2014-07-01');
select * from Meter;
MeterID Value InstalledDate ExpiryDate
--
1 1 2014-07-01 2015-01-01