MySQL: DEFAULT constraint is easy

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • #MySQL #course #tutorial
    00:00:00 intro
    00:02:25 DEFAULT when creating a table
    00:03:26 DEFAULT for an existing table
    00:05:19 example 2
    00:07:56 conclusion
    - EXAMPLE 1
    CREATE TABLE products (
    product_id INT,
    product_name varchar(25),
    price DECIMAL(4, 2) DEFAULT 0
    );
    ALTER TABLE products
    ALTER price SET DEFAULT 0;
    INSERT INTO products (product_id, product_name)
    VALUES (104, "straw"),
    (105, "napkin"),
    (106, "fork"),
    (107, "spoon");
    SELECT * FROM products;
    - EXAMPLE 2
    CREATE TABLE transactions(
    transaction_id INT,
    amount DECIMAL(5, 2),
    transaction_date DATETIME DEFAULT NOW()
    );
    SELECT * FROM transactions;
    INSERT INTO transactions (transaction_id, amount)
    VALUES (1, 4.99);
    SELECT * FROM transactions;
    INSERT INTO transactions (transaction_id, amount)
    VALUES (2, 2.89);
    SELECT * FROM transactions;
    INSERT INTO transactions (transaction_id, amount)
    VALUES (3, 8.37);
    SELECT * FROM transactions;
    DROP TABLE transactions;

Komentáře • 15

  • @BroCodez
    @BroCodez  Před rokem +7

    - EXAMPLE 1
    CREATE TABLE products (
    product_id INT,
    product_name varchar(25),
    price DECIMAL(4, 2) DEFAULT 0
    );
    ALTER TABLE products
    ALTER price SET DEFAULT 0;
    INSERT INTO products (product_id, product_name)
    VALUES (104, "straw"),
    (105, "napkin"),
    (106, "fork"),
    (107, "spoon");
    SELECT * FROM products;
    - EXAMPLE 2
    CREATE TABLE transactions(
    transaction_id INT,
    amount DECIMAL(5, 2),
    transaction_date DATETIME DEFAULT NOW()
    );
    SELECT * FROM transactions;
    INSERT INTO transactions (transaction_id, amount)
    VALUES (1, 4.99);
    SELECT * FROM transactions;
    INSERT INTO transactions (transaction_id, amount)
    VALUES (2, 2.89);
    SELECT * FROM transactions;
    INSERT INTO transactions (transaction_id, amount)
    VALUES (3, 8.37);
    SELECT * FROM transactions;
    DROP TABLE transactions;

  • @sweethomes674
    @sweethomes674 Před rokem +1

    Thank you-excellent 🕊

  • @bichoron3603
    @bichoron3603 Před měsícem

    Great!!
    Please keep uploading videos of sql more & more

  • @timon1816
    @timon1816 Před rokem

    Dude, I was just thinking about trying Mysql today and you're releasing this video. Thank you very much!

  • @bartsworkshop
    @bartsworkshop Před rokem

    Thank you for making these videos, they are easy to follow and understand.

  • @omarhashem6358
    @omarhashem6358 Před rokem

    Please continue the series❤

  • @imanelmzk2064
    @imanelmzk2064 Před 4 měsíci

    you're a Legend, thank you so much!!, that's so helpful!!!!

  • @vaishnutejass2724
    @vaishnutejass2724 Před rokem +3

    very easy to understand thanku

  • @GldnClaw
    @GldnClaw Před 4 měsíci +1

    2:24 looks like you answered my question from a few videos ago.

  • @Crazy_moments_of_footbal-qz1qr

    you great!!!!!!

  • @techboomers8935
    @techboomers8935 Před rokem

    Kindly kindly Bro share us your Programming life journey or Q/A OR you interview and face revealed, love from Pakistan

  • @MISA-qy4hx
    @MISA-qy4hx Před rokem +4

    Why did you use ALTER instead of MODIFY? 03:40

    • @dixsonjoy5394
      @dixsonjoy5394 Před 6 měsíci

      I also have this doubt

    • @apoorvsxna
      @apoorvsxna Před 2 dny

      This would also work-
      ALTER TABLE products
      MODIFY price DECIMAL (5,2) DEFAULT 0.00;