Home > Database > Mysql Tutorial > Introduction to SQL and Basic Commands

Introduction to SQL and Basic Commands

PHPz
Release: 2024-08-30 06:33:02
Original
677 people have browsed it

Introduction to SQL and Basic Commands

Introduction to SQL

What is SQL?

SQL (Structured Query Language) is a standard programming language used for managing and manipulating relational databases. It allows users to create, read, update, and delete data within a database. SQL provides a way to interact with databases using simple, declarative statements.

Why Learn SQL?

Learning SQL is essential for several reasons:

  • Data Management: SQL is the primary language for managing and querying relational databases, which are widely used in various applications.
  • Career Opportunities: Proficiency in SQL is a valuable skill for many IT and data-related roles, including database administrators, data analysts, and software developers.
  • Data Analysis: SQL enables you to perform complex data analysis and reporting tasks, making it easier to extract valuable insights from large datasets.
  • Versatility: SQL is used in many popular database systems, such as MySQL, PostgreSQL, and Microsoft SQL Server, making it a versatile skill across different platforms.

SQL Database and Table Operations Guide

Database Operations

Creating a Database

CREATE DATABASE FirstDB;
Copy after login

Note: FirstDB is the database name.

Using a Database

USE FirstDB;
Copy after login

Note: This selects the database for use.

Dropping a Database

DROP DATABASE FirstDB;
Copy after login

Note: This permanently deletes the database and all its contents.

Altering Database (Set to Read-Only)

ALTER DATABASE FirstDB READ ONLY = 1;
Copy after login

Note: This makes the database read-only, preventing any modifications.

Table Operations

Creating a Table

CREATE TABLE student (
    student_id INT,
    first_name VARCHAR(30),
    last_name VARCHAR(50),
    student_address VARCHAR(50),
    hourly_pay DECIMAL(5,2),
    student_date DATE
);
Copy after login

Note: This creates a table named 'student' with specified columns and data types.

Selecting All Data from a Table

SELECT * FROM student;
Copy after login

Note: This retrieves all rows and columns from the 'student' table.

Renaming a Table

RENAME TABLE student TO students;
Copy after login

Note: This changes the table name from 'student' to 'students'.

Altering Table Structure

Adding a New Column

ALTER TABLE students
ADD phone_number VARCHAR(15);
Copy after login

Note: This adds a new column 'phone_number' to the 'students' table.

Renaming a Column

ALTER TABLE students
CHANGE phone_number email VARCHAR(100);
Copy after login

Note: This changes the column name from 'phone_number' to 'email' and modifies its data type.

Modifying a Column's Data Type

ALTER TABLE students 
MODIFY COLUMN email VARCHAR(100);
Copy after login

Note: This changes the data type of the 'email' column to VARCHAR(100).

Changing a Column's Position

ALTER TABLE students
MODIFY email VARCHAR(100) AFTER last_name;
Copy after login

Note: This moves the 'email' column to be after the 'last_name' column.

ALTER TABLE students
MODIFY email VARCHAR(100) FIRST;
Copy after login

Note: This moves the 'email' column to be the first column in the table.

Dropping a Column

ALTER TABLE students
DROP COLUMN email;
Copy after login

Note: This permanently removes the 'email' column from the table.

Combining Multiple Operations

ALTER TABLE students
MODIFY email VARCHAR(100) AFTER last_name;
SELECT * FROM students;
Copy after login

Note: This changes the column position and then displays the new table structure in one operation.

The above is the detailed content of Introduction to SQL and Basic Commands. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template