Free Study Material

Database Administration & Utilities

Introduction

A MySQL server can hold many separate databases at once — before running any table commands, SQL needs to know which one you mean. Aliases, separately, are about making query output more readable.

Concept

  • USE database_name; — selects which database your subsequent commands apply to.
  • DESC table_name; (short for DESCRIBE) — displays a table's structure: its columns, their data types, and constraints — not the data inside it.
  • AS — creates an alias, a temporary display name for a column (or table) in a query's output only.

Worked Example

USE school;

DESC STUDENT;

USE school; tells the server every command after it applies to the school database — the same one connected to in the Python-SQL Interface topics. DESC STUDENT; then lists STUDENT's structure: each column's name, its data type (INT, VARCHAR(20), and so on), and whether it allows NULL — not a single row of actual data.

SELECT Name, Marks AS Score_Card FROM STUDENT;

The actual column in the table is still named Marks — Score_Card only appears as the heading in this one query's result. The underlying table is completely unchanged.

Note: DESC shows a table's STRUCTURE (columns and types), not its data — for that, you'd still use SELECT * FROM table_name.

Summary

USE database_name; selects which database to work in. DESC table_name; shows a table's structure. AS creates a temporary alias for a column's display name in query output, without renaming anything in the actual table.

Starting from Scratch

Before you can create a Table, you must create a **Database** to hold it. Here are the commands to manage the database environment.

1. Create Database

Creates a new empty container.

CREATE DATABASE School;
2. Use Database

Selects the database to work on.

USE School;
3. Show Tables

Lists all tables in the current DB.

SHOW TABLES;
4. Describe Table

Shows structure (cols, types) of a table.

DESC Student;

Aliasing (Nicknames)

You can give a temporary "nickname" to a column or table using the keyword AS. This is useful for making output readable.

SELECT Name, Marks * 0.5
AS Half_Marks
FROM Student;
Name Half_Marks
Ravi 45.0

Challenge: Nicknames

The STUDENT table has a column Marks.

Your Task:

Write a query to display Name and Marks, but rename the Marks column to "Score_Card" in the output.

↓ Write your Query below ↓

/* ==================================================
   SYSTEM SETUP
   ================================================== */
DROP TABLE IF EXISTS STUDENT;
CREATE TABLE STUDENT (Name VARCHAR(20), Marks INT);
INSERT INTO STUDENT VALUES ('Ravi', 90);

/* ==================================================
   YOUR TASK
   Select Name and Marks.
   Use alias 'AS Score_Card' for Marks.
   ================================================== */

-- SELECT Name, Marks AS ...

Want the full course?

Unlock all chapters, video lectures, and SQL practice exercises.

Get Full Access