MySQL Queries

Discussion in 'Programming' started by StormForum, Nov 27, 2006.

  1. #1
    I am learning PHP & MySQL from a PHP & MySQL book. And I'm on Chapter 4, and I've got a bit confused as to what this definition is used for.

    It says that this value is stored in the column when the row is created if no other value is given for the column. Does it mean that value is stored in the row of whatever column the row is in? Or..?

    Any help will be appreciated :).
     
    StormForum, Nov 27, 2006 IP
  2. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #2
    say you have created a table ,mytable with two columns- a1,b1. If you set 0 as defult value for column a1 , whenever you create a row if you specifically do not enter any value for a1 then a1 will have 0 as its value.
     
    kashem, Nov 27, 2006 IP
  3. StormForum

    StormForum Well-Known Member

    Messages:
    206
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Thanks for your reply! So if it had a value of 0, 1, 2, 3, 4, 5, 6 or whatever, does it make a difference? If so, what is the difference? If you get what I mean.

    I'm new to this, so please bear with me. Thanks.
     
    StormForum, Nov 27, 2006 IP
  4. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yes with 0,1 2 .. etc not a problem

    what is fact is the data type you have to keep in mind. Say if you define int as data type for a column you can nt define a value that varchar.

    the default value should be the kind of data that you define as data type
     
    kashem, Nov 27, 2006 IP
  5. StormForum

    StormForum Well-Known Member

    Messages:
    206
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #5
    So if I wanted all rows on a1 to be this:

    So that will do that if no other value is done for every row I make?
     
    StormForum, Nov 27, 2006 IP
  6. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #6
    create table mytable
    (a1 int,
    b1 varchar(20) default 'stom')


    then when I create any row if i dont enter any value for b1 then it will hold stom

    insert mytable(a1) value(10)

    then it will create the following row

    a1 b1
    -- ---
    10 stom

    then if i do the followin

    insert mytable(a1,b1) value(10,'kashem')


    then it will create the following row

    a1 b1
    -- ---
    10 kashem
     
    kashem, Nov 27, 2006 IP
  7. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #7
    kashem, Nov 27, 2006 IP
  8. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #8
    Lets say you have a field "is_active" to check if the record is active or not.

    0 means it is not active, 1 is active, 2 is .. etc...

    You would want the default value to be 0, especially if it is user entered content and then you would go about reviewing the record and changing the value to 1.

    Peace,
     
    Barti1987, Nov 27, 2006 IP
  9. StormForum

    StormForum Well-Known Member

    Messages:
    206
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #9
    Ah, so like AUTO_INCREMENT every time they post once, so when they register, the default value is 0? I get it now! :)

    Also, about PRIMARY KEY's, for example;

    CREATE TABLE Member (

    loginName VARCHAR(20) NOT NULL PRIMARYKEY,
    createDate DATE NOT NULL);
    PRIMARY KEY(columnname)

    firstly, what is a primary key and what are they used for?

    Secondly, the bottom of the query is PRIMARY KEY(columnname) - what do I put in the columnname bit?

    Sorry about this :S. Any help appreciated! :)
     
    StormForum, Nov 27, 2006 IP