SQL CURD Operation

Last updated: Nov. 21, 2024

SQL CURD Operation

SQL stands for Structured Query Language. SQL use to communicate any relational database. Many relational databases software are available in market like mysql, postgres, oracale etc. We don't need to learn every database individually. Using SQL, we can perform our required operations in all relational databases.

CURD Operations:

CURD stands for Create, Update, Read, Delete. Mainly this 4 operations are require to manipulate data in organize way. Now I will show you simple CURDs operations based on MySQL database. Let's start.

 

Enter or login database:

mysql -r root -p

Then press enter and enter your password.

If you don't set password and you are root user the you can use command like

mysql

Show available existing all databases:

show databses;

Create a new database:

Syntax:

CREATE DATABASE databasename;

CREATE DATABASE appbyte;

Now again show all databases. And you will must see a new database is created and name is appbyte;

show databses;

Enter in created database:

After create new database, you should enter inside database and create table for storing data.

Syntax:

use databasename;

use appbyte;

If you want to see available tables in database:

show tables;

You follow this blog. then you must see nothing. Because we don't create any table.

Create table for inserting data:

Syntax:

CREATE TABLE table_name (

    column1 datatype,

    column2 datatype,

    column3 datatype,

   ....

);

CREATE TABLE contact (
	id int,
	name varchar(255),
	phone_number varchar(255),
	address varchar(255),
	city varchar(100),
	country varchar(255)
	);

Now show all tables:

show tables;

You follow this blog. then you must see a new table that's name is contact.

Insert data in table:

Two way is available to insert data in table.

Syntax:

1. Specify both the column names and the values for inserting

INSERT INTO table_name (column1, column2, column3, ...)

VALUES (value1, value2, value3, ...);

Here you must insert value according to your specify column position like :

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

Or

INSERT INTO table_name (column1, column3, column2, ...) VALUES (value1, value3, value2, ...);

2. Without specify column name from inseting:

INSERT INTO table_name  VALUES (value1, value2, value3, ...);

Here you cann't change sequence. You must insert data according to the sequence that you specify when create the table.

Insert First Data

INSERT INTO contact (id, phone_number, name, address, city, country) VALUES (1, "0171123355", "AppByte", "Istambul", "Istambul", "Turkey");

Insert Second Data

INSERT INTO contact VALUES (2, "Jon", "0171123377", "Antalya", "Ankara", "Turkey");

Read or fetch data:

Syntax:

SELECT column1, column2, ...   FROM table_name;

SELECT id, name, phone_number, address, city, country FROM contact;

You can see all data in table format. If you all field of data, then you can use  query like:

SELECT * FROM contact;

If you want to get just specific column like name and phone_number, then you can use query like:

SELECT id, name, phone_number FROM contact;

Update data from database:

If you want you update and field of any particular row, then you can use update query like ths:

Syntax:

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

UPDATE contact SET name="app-byte", address="Beside Grand Bazar" WHERE id=1;

If you want to update all row of tables, then you should avoid where condition.

UPDATE contact SET address="Beside Grand Bazar"

Delete data or row from database:

Syntax:

DELETE FROM table_name WHERE condition;

DELETE FROM contact WHERE id=2;

 

If you want to more learn SQL, you should complete this course.

 

0 comments

Create new comment