Writing NULL is not an essential thing in CREATE TABLE? - sql

EMP_NO : VARCHAR2(10)NOT NULL
------------------------------
EMP_NM : VARCHAR2(30)NOT NULL
DEPT_CODE : VARCHAR2(4)NOT NULL
JOIN_DATE : DATE NOT NULL
REGIST_DATE : DATE NULL
when I create table by using above one, is it okay to just write "REGIST_DATE DATE" instead of "REGIST_DATE DATE NULL"
CREATE TABLE EMP
(EMP_NO VARCHAR2(10) NOT NULL,
EMP_NM VARCHAR2(30) NOT NULL,
DEPT_CODE VARCHAR2(4) DEFAULT '0000' NOT NULL,
REGIST_DATE DATE);
ALTER TABLE EMP ADD CONSTRAINT EMP_PK PRIMARY KEY(EMP_NO);
CREATE INDEX IDX_EMP_01 ON EMP(JOIN_DATE);

Null is the default behaviour in most DBs, so if you dont write it system will automatically make the column as NULL.

The default is that columns may contain nulls - ie if you don't code anything, no constraint will be defined that allows nulls - they are already allowed..
Adding NOT NULL adds a constraint that disallows nulls.
There is no such constraint like NULL: Just leave it out.

Related

SQL CREATE TABLE missing left parentheses error

CREATE TABLE residents
(
R_ID NUMBER(4), CONSTRAINTS pk_residents_R_ID PRIMARY KEY,
R_FN VARCHAR2(15), NOT NULL,
R_LN VARCHAR2(15), NOT NULL,
R_Contact NUMBER(10), NOT NULL,
DoB DATE, NOT NULL
);
Tried a few changes but I'm unable to resolve this error. Any help would be appreciated!
It works for Oracle in this way:
CREATE TABLE residents(
R_ID NUMBER(4),
R_FN VARCHAR2(15) NOT NULL,
R_LN VARCHAR2(15) NOT NULL,
R_Contact NUMBER(10) NOT NULL,
DoB DATE NOT NULL,
pk_residents_R_ID NUMBER(4) PRIMARY KEY
);
Code which is the closest to your attempt is:
SQL> CREATE TABLE residents
2 (
3 r_id NUMBER (4) CONSTRAINT pk_residents_r_id PRIMARY KEY,
4 r_fn VARCHAR2 (15) NOT NULL,
5 r_ln VARCHAR2 (15) NOT NULL,
6 r_contact NUMBER (10) NOT NULL,
7 dob DATE NOT NULL
8 );
Table created.
SQL>
keyword is constraint, not constraintS
don't separate NOT NULL (or any other constraint) from its column with a comma
Also, using mixed letter case is irrelevant as Oracle - by default - stores object names in UPPERCASE. That's so unless you decide to enclose names into double quotes, but then you'll always have to use double quotes and exactly match letter case so - that's not what anyone should do, not in Oracle.

How to enforce two foreign key columns to be either both null or both pointing to some row in other table in Oracle?

I have two tables, Employees and Tasks (this is not an actual SQL code of course, listed just important stuff):
CREATE TABLE Employees (
employee_id NUMBER(6) NOT NULL,
is_boss NUMBER(1) DEFAULT 0 NOT NULL,
name VARCHAR2(32) NOT NULL,
CHECK (is_boss IN (0,1)),
UNIQUE (is_boss, employee_id)
);
CREATE TABLE Tasks (
task_id NUMBER(6) NOT NULL,
name VARCHAR2(32) NOT NULL,
is_boss NUMBER(1),
employee_id NUMBER(6),
finish_date DATE,
CHECK (is_boss IN (1)),
FOREIGN KEY (employee_id) REFERENCES Employees (employee_id),
FOREIGN KEY (is_boss) REFERENCES Employees (is_boss)
);
So the Tasks table contains some tasks. When they are added to the table we only need the name and id, that's why other fields are nullable. At some point in time every task has to be confirmed by a "boss", so an employee which has is_boss == 1 and only then finish_date is put, quite simple.
Those check constraints and foreign/unique keys work well if a task is updated with both is_boss and employee_id - if an employee is not a boss, it throws an error, if there isn't such employee also. But if one of those is null everything goes wrong. So what I want is to somehow enforce the database to check to have those two fields be either both not null or both null. Actually, I want 3 fields (finish_date too) to be either all null or all not null.
A trigger is probably an option, but my database teacher is very much against using them if there is a different, simpler possibility. So my question is - is there a way to enforce it without a trigger? DBMS is Oracle 11g.
Thanks in advance.
You need to combine your two foreign keys into a single foreign key - otherwise I think you'll find they're not doing quite what you think they're doing. Also, you need a check constraint to ensure that all three fields are set or all three are NULL. Your TASKS table needs to be something like:
CREATE TABLE TASKS (
TASK_ID NUMBER(6) NOT NULL,
NAME VARCHAR2(32) NOT NULL,
IS_BOSS NUMBER(1),
EMPLOYEE_ID NUMBER(6),
FINISH_DATE DATE,
CONSTRAINT TASKS_CK1
CHECK (is_boss IN (1)),
CONSTRAINT TASKS_FK1
FOREIGN KEY (IS_BOSS, EMPLOYEE_ID)
REFERENCES EMPLOYEES (IS_BOSS, EMPLOYEE_ID),
CONSTRAINT TASKS_CK2
CHECK((IS_BOSS IS NULL AND
EMPLOYEE_ID IS NULL AND
FINISH_DATE IS NULL)
OR
(IS_BOSS IS NOT NULL AND
EMPLOYEE_ID IS NOT NULL AND
FINISH_DATE IS NOT NULL))
);

How to define table constraint to check non empty datetime?

While creating a table how to add table level constraint so as to check that a column with datatype datetime is not empty?
You would use something like this with NOT NULL:
CREATE TABLE [dbo].[MyTable](
[ID] [int] NOT NULL,
[MyField] [DATETIME] NOT NULL)
The NOT NULL constraint enforces a column to NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field.
The following SQL enforces the "P_Id" column and the "LastName" column to not accept NULL values:
CREATE TABLE PersonsNotNull
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
If the date field has a point beyond which you know there will be no valid dates, you can set that as a lower boundary.
Date_Field date check( Date_Field > '2000-01-01' ),
If it is peoples birthdate, you will have to set it back to a more reasonable value. Now, now matter how the date is entered or what is is converted from, it must be within a reasonable range to be considered valid.

create table in Oracle BD but gives error

CREATE TABLE employees (
id INT NOT NULL auto_increment PRIMARY KEY (ID),
first_name VARCHAR(20) DEFAULT NULL,
last_name VARCHAR(20) DEFAULT NULL,
salary INT DEFAULT NULL);
I think this is correct query to create table in Oracle database.. but it gives the following error:
ORA-00907: missing right parenthesis
How to correct the statement?
You can validate your SQL using formatting tools such as http://www.dpriver.com/pp/sqlformat.htm
auto_increment seems like a proprietary MySQL extension, so it's not valid for Oracle.
also, "id int not null auto_increment primary key (id)" does not need the last "(id)"
Using Oracle, you shoud try something like this
CREATE SEQUENCE seq;
CREATE TABLE employees
(
id INTEGER NOT NULL PRIMARY KEY,
first_name VARCHAR2(20) DEFAULT NULL,
last_name VARCHAR2(20) DEFAULT NULL,
salary INTEGER DEFAULT NULL
);
INSERT INTO employees
VALUES (seq.NEXTVAL,
'name',
'last name',
1);
Sometimes, SQL is fancy, because even having a standard (ANSI), most DBMS vendors add their proprietary extensions to the SQL creating their own languages, so it's rare the situation where you can port one SQL from one DB into another without any changes.
Also, it's a pretty useless error message. It could at least say which position. (also, there's no missing parenthesis, but an unexpected token)
EDITED : New feature 12c
CREATE TABLE employees(
id NUMBER GENERATED ALWAYS AS IDENTITY,
first_name VARCHAR2(30)
etc.
);
Why would you do default null?
The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings.
Replace
id INT NOT NULL auto_increment PRIMARY KEY (ID),
with
id INT NOT NULL auto_increment PRIMARY KEY,
this is more efficient
CREATE TABLE EMPLOYEES_T(
ID NUMBER,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(20) DEFAULT NULL,
SALARY INTEGER DEFAULT NULL,
CONSTRAINT PK_EMPLOYEES_T PRIMARY KEY(ID)
);

Change domain value

I have three fields with NOT NULL domain. How can I change them to accept NULL data?
frais_inscription NUMBER(6,2) NOT NULL,
date_paiement DATE NOT NULL,
type_paiement VARCHAR2(15) NOT NULL,
Are you just trying to alter your table's columns to accept null? Here is the generic syntax:
alter table yourtable
modify (yourfield number(6,2) null);
SQL Fiddle Demo