0% found this document useful (0 votes)
5 views

SQL Queries - Notes

Uploaded by

Ritu Arora
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL Queries - Notes

Uploaded by

Ritu Arora
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

 How to add Primary Keys after table creation:

1. Can also use to add primary key with constraint name:

2. Another way is: alter table one


Add constraint pk_id primary key(id)

 How to set Default Values for a column:

1.

Not like this :

2. Another way, using alter

 Using Numeric Datatype:

1. Create table using Numeric data type


 Unique Key can have NULL Values
 Foreign Key can be based on Unique (if not NULL values) or Primary.

Difference between NULL and empty


NULL

In a database, null means ‘nothing’, absolutely nothing. Like a vacuum or a void. This
means that it does not, in actuality (despite what a column says), have a data type (e.g.
string, integer etc.) and no functions can be called on it. E.g. we can’t SUM it or check it’s
length with the len() function in Tableau. An error would be raised if we tried. Null is simply
the word used to show the absence of any data in that field.

EMPTY

Empty on the other hand looks like nothing but is not actually nothing. Empty is really an
empty string, so something like this:

“ ” (without the space)

Therefore empty does have a data type (string) and therefore functions can be called on
them. For example if we were to call the len() function on an empty, the function would
execute and the result would be ‘0’.
Empty cannot exist with other data types either. For example, an 'empty’ integer would
instead contain the value 0.

Lets look at the difference between Null and Empty as given below:

1. Null can be a unknown value or an absence of a value, where as an Empty or Blank


string is a value, but is just empty.
2. Null can be used for string , Integer ,date , or any fields in a database where as
Empty is used for string fields.
3. As NULL is a unknown value, so a field having NULL is not allocated any memory,
where as empty fields have empty value with allocated space in memory.
Lets try to understand NULL and Empty using an example.

Here , we have sample table named Department.

In table you can see column phoneNo1 have a null value while PhoneNo2 have
an empty value .

As you know Null value is considered as unknown value and memory is not allocated for it
while Empty value is value with empty and memory allocated for it.

Lets check the length for these fields in bytes using Datalength function.

Select DptId, DepartmentName, DATALENGTH(PhoneNo1) as PhoneNo1,

DATALENGTH(PhoneNo2) as PhoneNo2 from Department


As you can see , PhoneNo1 length in bytes is still null means memory is not allocated to it
yet which means that it is unknown value, while phoneNo2 length is zero (0), that means
memory is allocated to empty field.

UNIQUE CONSTRAINT:

 Can only be added if field contains no duplicate


 Can only be dropped using Constarint_name . If know the name, use it

 else use command to get constraint _name

SELECT TABLE_NAME, CONSTRAINT_TYPE, CONSTRAINT_NAME

FROM information_schema.table_constraints
WHERE table_name='student'

CHECK Constarint
Again, need to know the name to delet it

You might also like