Change domain value - sql

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

Related

How to create a constraint that allows 2 options using SQL Server?

Could anyone help saying how I could create a constraint that allows 2 options using SQL Server?
For exapmle, I want it to be possible to add new candidates only if they have an intermediate or advanced knowledge in SQL.
So, I coded like below, but it doesn’t work.
CREATE TABLE Candidates
(
Name VARCHAR(150) NOT NULL UNIQUE,
BirthDate DATE NOT NULL,
SQLlevel VARCHAR(100) NOT NULL
CHECK (SQLlevel = 'Intermediate' OR 'Advanced')
)
Thanks in advance.
You can use in:
CREATE TABLE Candidates (
Name VARCHAR(150) NOT NULL UNIQUE,
BirthDate DATE NOT NULL,
SQLlevel VARCHAR (100) NOT NULL,
CHECK (SQLlevel IN ('Intermediate', 'Advanced'))
)
To explain why what you had didn't work it's because of your boolean expression:
SQLlevel = 'Intermediate' OR 'Advanced'
Your expression after the OR isn't a boolean expression, it's just a literal. You have to define the comparison operator each time. If you wanted to use a OR you would need to do the following:
CREATE TABLE Candidates (Name varchar(150) NOT NULL UNIQUE,
BirthDate date NOT NULL,
SQLlevel varchar(100) NOT NULL CHECK (SQLlevel = 'Intermediate' OR SQLlevel = 'Advanced'));
You're missing the word CONSTRAINT and a name
CREATE TABLE Candidates
(
Name VARCHAR(150) NOT NULL UNIQUE,
BirthDate DATE NOT NULL,
SQLlevel VARCHAR(100) NOT NULL,
constraint chck_candidates_levels
CHECK (SQLlevel in('Intermediate', 'Advanced'))
);

Writing NULL is not an essential thing in CREATE TABLE?

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.

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)
);

Check constraint when table is created with default value?

I am using oracle 11g. i am creating a table which has few columns. One of the column should have some default value. If no value is passed then DEFAULT must be considered. Can i do as below?
Column with default value:
ATTENDENT CHAR(1 BYTE) DEFAULT 'N'
Constraint:
CONSTRAINT "CC_ATTENDENT_CHECK" CHECK (attendent is not null and attendent in ('Y','N')) ENABLE
Thanks!
When you create / alter a table with a default value, like this:
Alter table table_test modify (foo varchar(3) NULL default 'bar');
... should be enough to set a default value when you insert a null
Yes You can,
eg:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Sandnes'
)