MS SQL Server Concepts and Programming Question:
Download Questions PDF

PHP MSSQL - How To Make a Column Nullable?

Answers:

Answer #1
Based on the testing result from the previous tutorial you can find out that there is a big difference in the column definition when running CREATE TABLE statement with mssql_query():

* If CREATE TABLE is executed through mssql_query() and "NULL/NOT NULL" keyword is omitted in column definition, mssql_query() will assume NOT NULL.
* If CREATE TABLE is executed directly on SQL Server and "NULL/NOT NULL" keyword is omitted in column definition, SQL Server will use NULL as the default.

Now you have to modify the CREATE TABLE statement to create "ggl_links" again by adding NULL to columns: notes, counts, and time:

Answer #2
<?php
$con = mssql_connect('LOCALHOST','sa','GlobalGuideLine');
mssql_select_db('GlobalGuideLineDatabase', $con);

# dropping an existing table
$sql = "DROP TABLE ggl_links";
$res = mssql_query($sql,$con);
print("Table ggl_links dropped. ");

# creating a new table
$sql = "CREATE TABLE ggl_links ("
. " id INT NOT NULL"
. ", url VARCHAR(80) NOT NULL"
. ", notes VARCHAR(1024) NULL"
. ", counts INT NULL"
. ", time DATETIME NULL"
. ")";
$res = mssql_query($sql,$con);
print("Table ggl_links created. ");

mssql_close($con);
?>

If you run this script, "ggl_links" will be dropped and created again with correct column definitions:

Table ggl_links dropped.
Table ggl_links created.


Download MS SQL Server Interview Questions And Answers PDF

Previous QuestionNext Question
PHP MSSQL - How To Insert Data into an Existing Table?PHP MSSQL - How To Insert Data with NULL Values?