www.webdeveloper.com
Results 1 to 4 of 4

Thread: NOT NULL, column definition question

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    NOT NULL, column definition question

    I guess i'm confused about what NOT NULL means when defining a table column. i thought it meant it had to contain something, and would return an error if you did an insert without putting something into it.(?)

    my create table query looks like this,

    create table links_other (
    id int not null auto_increment primary key,
    manid int not null,
    url varchar(50) not null,
    link varchar(50) not null
    )

    but then my populate query is this. this populate query does not contain any data for the link column. yet it runs successfully,

    INSERT INTO links_other (manid, url) VALUES (
    1, 'www.thisisawebsiteurl.com'),
    (2, 'www.mywebsite.net'),
    (3, 'www.websitethree.com'),
    (4, 'www.websitefour.com')

    how then do i achieve that, creating a column the HAS to contain data, not optional to be empty?,

  2. #2
    Join Date
    May 2012
    Posts
    17
    By default MySQL will fill a VARCHAR which has not been specified with an empty string if the column is defined as NOT NULL. My question would be, why would you want a NOT NULL, could you not test for empty strings?

  3. #3
    Join Date
    Mar 2010
    Posts
    281
    thank you, i didn't know that

    suppose that is what i will do, test for empty strings.

  4. #4
    Join Date
    Mar 2006
    Location
    Discworld
    Posts
    75
    Your logic was correct but not for MySQL.
    Microsoft SQL will do exactly what you think would of happen here.
    You could also define default value which will be inserted by system automatically if your query does not provide value for it....
    Your statement for table create in MySQL will look like this:

    create table links_other (
    id int not null auto_increment primary key,
    manid int not null,
    url varchar(50) DEFAULT 'www.google.com',
    link varchar(50) DEFAULT 'none'
    )

    and if really want to mess up your life you can define default like this:
    default NULL -- once again this is for MySQL not for MS SQL database...
    Last edited by alexsts; 06-08-2012 at 01:28 PM.

    Please consider the
    environment before printing

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles