How to define table constraint to check non empty datetime? - sql

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.

Related

How implement the function of `case` in DDL?

I have a table order. Its DDL is listed below
create table "order"
(
id serial primary key,
quantity integer not null,
estimated_delivery_date date not null,
lodgement_date date check ( lodgement_date > '2022-03-02' ),
product_model varchar(128) not null,
sales_id integer not null,
contract_number varchar(10) not null
);
I want to make the lodgement_date attribute to be null if it is later than 2022-03-02 when inserting a new row. How can I achieve this function in DDL.
To do that, you can't do it in the creation of the table. It is gonna be a constraint.
Example:
CONSTRAINT check_lodgement_date
CHECK (lodgement_date > '2022-03-02')

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.

SQL: Missing Parenthesis

I have tried editing the command and looking at other questions in order to answer this question, but I still get the dreaded
ORA-00907: missing right parenthesis.
Here is my code. Is there any key word that I may need to drop here? Thanks
CREATE TABLE Loan
(
LoanID INT IDENTITY ( 1, 1 ) UNIQUE,
BranchID INT NOT NULL REFERENCES Branch(BranchID) ON DELETE CASCADE,
LoanNumber CHAR(20) NOT NULL UNIQUE,
LoanType VARCHAR(30) NOT NULL,
Amount MONEY NOT NULL,
ModifiedDate DATETIME DEFAULT (getdate()),
PRIMARY KEY ( LoanID )
);
The following ddl is syntactically correct. Of course, you have to check whether that statement really produces what you want ( in particular the IDENTITY keyword in your original statement is not accounted for ):
CREATE TABLE Loan
(
LoanID INTEGER NOT NULL PRIMARY KEY
, BranchID INTEGER NOT NULL CONSTRAINT tl_fk_branchid REFERENCES Branch(BranchID) ON DELETE CASCADE
, LoanNumber CHAR(20) NOT NULL CONSTRAINT tl_u_loannumber UNIQUE
-- right padded to length of 20 with blanks
, LoanType VARCHAR2(30) NOT NULL
, Amount Number(*,4) NOT NULL
-- cf. http://stackoverflow.com/a/29014422, changed per #BobJarvis' comment
, ModifiedDate DATE DEFAULT SYSDATE
);
The syntax deviations are as follows:
IDENTITYkeyword
inline constraint specification
datatype to represent date and time
datatype for string content
stand-in for money datatype
current datetime

When I create a table in SQL, how do I set an attribute not null?

I wanna create a table in SQL, and if the I wanna set the attributes to NULL, how do I do it?
For example, I wanna create a table named Courses and its attributes "CourseNo" and "Title" must not be null. I created one below:
CREATE TABLE Courses(
CourseNo INTEGER CHECK(100<=CourseNo<=999) PRIMARY KEY,
Title VARCHAR(100),
)
ALTER TABLE Courses
ALTER COLUMN CourseNo INTEGER NOT NULL
ALTER COLUMN Title VARCHAR(100) NOT NULL
Is this correct?
whatever you wrote that one correct but good practice is to write not null at a time of defining table. do like this:
CREATE TABLE Courses(
CourseNo INTEGER CHECK(100<=CourseNo<=999) PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
)
primary key is by default NOT NULL always, hence you no need to declare it as NOT NULL
NOT NULL in alter is used when u want to make change in column definations of table later
Change that Title VARCHAR(100) to Title VARCHAR(100) NOT NULL.