SQL NOT NULL constraint syntax and NOT NULL instance

Source: Internet
Author: User
Tags table name

SQL NOT NULL constraint syntax and NOT NULL instance


If we create a table

CREATE TABLE AA
(
id_p int not NULL,
LName varchar () not NULL,
FName varchar (25),
Ass varchar (25),
C varchar (205)
)

The NOT NULL constraint enforces that the column does not accept NULL values.

A NOT NULL constraint forces a field to always contain a value. This means that if you do not add a value to a field, you cannot insert a new record or update the record.

The following SQL statement forces the "id_p" column and the "LastName" column to not accept NULL values:

Allow NULL is the basic property of a column definition. Of course, to define the primary key, you must set the Allow NULL to false.


Let's take a look at a netizen question Www.111cn.net

I want to know if this NOT NULL is a constraint or an attribute in the database tutorial, I think it's a constraint?
Since such constraints as primary keys, foreign keys, and Unique,check can be added through SQL statements, I want to know how to add a NOT NULL constraint to a column. That I did not define the NOT NULL constraint for the password column pwd, and now I want to modify it through the SQL statement, what can I do to change it, please tell the friend in detail

ALTER TABLE [table name] ALTER COLUMN [column name] data type (length) not null such as: ALTER TABLE [T_M_MEMBER] ALTER COLUMN [vch_memberno] varchar () not Null


Question Two

I'm a beginner of SQL Server, and I read this example. * Example 5-13 establishes a S2 table with a NOT NULL constraint on the Sno field.
The list of procedures is as follows

Use DB
CREATE TABLE S2
(SNO CHAR (a) CONSTRAINT s_cons not NULL,
SN VARCHAR (20),
Age INT,
SEX CHAR (2) DEFAULT ' Male ',
DEPT VARCHAR (20)) I am a bit strange: 1. Why do I have to add a constraint name before NOT NULL? Some of the examples I've seen are directly followed by NOT NULL or null on the field name,
2. Why does the age field in the example not have any constraints, NULL or NOT NULL, and why is it that you can omit not to write null or not NULL?


Solving method

SQL By default
Age int = Age int NULL
DEPT VARCHAR (a) =dept VARCHAR (a) null

SNO CHAR (a) CONSTRAINT s_cons not NULL
This is a redundant formulation.
Change to Sno CHAR (Ten) not NULL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.