Sunday, July 21, 2019

How to Insert, Update & Delete data in table Mysql Mariadb database on xampp server?


Sample Table for Reference:

Table Name:
      person
Fields:
      id(INT), name(VARCHAR), designation(VARCHAR), city(VARCHAR), age(INT)

How to select database?

Syntax:
       USE database_name

Example:
       USE test;

How to create table?

Syntax:
      CREATE TABLE table_name (column_name data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT] [PRIMARY KEY], ...)

Example:
       CREATE TABLE person (id INT(11) AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255), designation VARCHAR(50),
city VARCHAR(50),
age INT(3));

How to check table structure?

Syntax:
        DESC table_name;

Example:
        DESC person;

How to insert record in a table?

Syntax:
INSERT INTO  table_name (column) VALUE (value);
INSERT INTO  table_name (column1, column2, ...) VALUES (value1, value2, ...);
INSERT INTO  table_name VALUES (value1, value2, valueN);

Example:
INSERT INTO person (name) VALUE ('Amita');
INSERT INTO person (name, city) VALUES ('Akash', 'Greater Noida');
INSERT INTO person VALUES ('Aviral Singh', 'Engineer', 'Kanpur', 23);

How to update record(s) in a table?

Syntax:
UPDATE table_name SET column1=value1, column2=value2, ... WHERE condition;

Example:
UPDATE person SET name='Aarav Singh', city='Greater Noida' WHERE id=2;
Delete records in the table

Note: If where condition is ignored all records will be update.

How to delete record(s) in a table?

Syntax:
DELETE FROM table_name WHERE condition;

Example:
DELETE FROM person WHERE age < 18;

NoteIf where condition is ignored all records will be delete.

How to delete table?

Syntax:
       DROP table table_name;

Example:
       DROP table sample;



No comments:

Post a Comment