0% found this document useful (0 votes)
5 views4 pages

L1 MySQL Create DB and Table

The document provides SQL commands for managing databases and tables, including creating, dropping, and using databases and tables. It also includes examples for creating specific tables, such as 'cats', 'dogs', and 'pastries', along with commands to show and describe tables. Additionally, it mentions SQL data types and comment syntax in SQL.

Uploaded by

rajsinghcool0708
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

L1 MySQL Create DB and Table

The document provides SQL commands for managing databases and tables, including creating, dropping, and using databases and tables. It also includes examples for creating specific tables, such as 'cats', 'dogs', and 'pastries', along with commands to show and describe tables. Additionally, it mentions SQL data types and comment syntax in SQL.

Uploaded by

rajsinghcool0708
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

To list available databases:

show databases;
________________________________________________________

The general command for creating a database:


CREATE DATABASE <database_name>;

A specific example:

CREATE DATABASE soap_store;

_______________________________________________________________

To drop a database:

DROP DATABASE <database-name>;

Example:

DROP DATABASE soap_store;

_________________________________________________

To use a database:

USE <database-name>;

Example:

USE DATABASE soap_store;

_____________________________________________________

To show the current database we are working on:

SELECT database();

_____________________________________________________________
Databases have lots of tables.

An attempt at wikipedia schema


https://en.wikipedia.org/wiki/Database_schema#/media/File:MediaW
iki_1.28.0_database_schema.svg

An attempt at simplified facebook schema


https://upload.wikimedia.org/wikipedia/commons/9/95/Metamodel_of
_Facebook.jpg

Some SQL Datatypes

Docs - https://dev.mysql.com/doc/refman/8.4/en/data-types.html
Creating Tables:

​ CREATE TABLE cats (


​ name VARCHAR(50),
​ age INT
​ );

​ CREATE TABLE dogs (
​ name VARCHAR(50),
​ breed VARCHAR(50),
​ age INT
​ );

______________________________________________________________
Showing Tables

SHOW tables;

SHOW COLUMNS FROM cats;

DESCRIBE cats;
DESC cats;

______________________________________________________________

To drop a table:

DROP TABLE <table-name>;

To specifically drop the cats table:

DROP TABLE cats;

______________________________________________________________
Activity

Create a pastries table. It should include two columns: name and


quantity. Name is 50 characters max. Then delete the table.

Solution
Create the table:

​ CREATE TABLE pastries


​ (
​ name VARCHAR(50),
​ quantity INT
​ );
View tables:

SHOW TABLES;

View details of pastries table:

DESC pastries;

Delete the whole pastries table:

DROP TABLE pastries;

_______________________________________________________________

-- or # can be used for single line comments.

Multi-line comments are enclosed in /* and */.

You might also like