0% found this document useful (0 votes)
13 views

SQL DDL Summary

The document provides a summary of SQL DDL commands for creating and managing databases, tables, columns, primary keys, and foreign keys. It lists commands for creating databases and tables, adding/deleting columns, adding primary keys during or after table creation, adding or dropping foreign keys, and dropping databases and tables.

Uploaded by

Binoj Charuka
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)
13 views

SQL DDL Summary

The document provides a summary of SQL DDL commands for creating and managing databases, tables, columns, primary keys, and foreign keys. It lists commands for creating databases and tables, adding/deleting columns, adding primary keys during or after table creation, adding or dropping foreign keys, and dropping databases and tables.

Uploaded by

Binoj Charuka
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

SQL DDL Summary

CREATE DATABASE<database name> CREATE DATABASE school;


Create Database

View Databases SHOW DATABASES;

Using the Database USE <Database>; USE school;

To see Tables SHOW TABLES;

Describing a Table DESCRIBE <table_name>; DESCRIBE student;


Adding field to the Table ALTER TABLE <table name> ALTER TABLE student
->ADD <col name> <data type>(size); ->ADD email varchar(20);

Deleting field from the table ALTER TABLE <table name> ALTER TABLE student
->Drop <col name>; ->Drop email;

Adding the primary key after ALTER TABLE <table name> ALTER TABLE student
creating the table ->ADD PRIMARY KEY<col name>; ->ADD PRIMARY KEY(sid);

Removing Primary Key ALTER TABLE <table name> ALTER TABLE student
->DROP PRIMARY KEY; ->DROP PRIMARY KEY;

Removing foreign key ALTER TABLE <table name> ALTER TABLE enroll
->DROP FORIGN KEY <col name>; ->DROP FORIGN KEY coid;

Drop(Delete) a Table DROP TABLE <table name>; DROP TABLE students;


CREATE TABLE <table name>( CREATE TABLE student(
Create Table <col name1> <data type> (size), ->stid int,
<col name2> <data type> (size),); ->fname varchar(20),
->sex char(1),);

Adding a primary key when CREATE TABLE <table name>( CREATE TABLE hobby(
<col1 name> <data type> (size), ->hid varchar(3),
creating a table <col2 name> <data type> (size), ->name varchar(10),
PRIMARY KEY (<col name>)); PRIMARY KEY (hid));

Adding a primary key after creating ALTER TABLE <table name> ALTER TABLE student
the table ->ADD PRIMARY KEY (col name); ->ADD PRIMARY KEY (sid);

Adding a foreign key after ALTER TABLE <table name> ALTER TABLE students
ADD FORIRGN KEY (col name) ->ADD FORIRGN KEY (hid)
creating the table REFERENCES <tb name> (col name); ->REFERENCES hobby (hid);

Delete a Database DROP DATABASE <db name>; DROP DATABASE Students;


Adding a CREATE TABLE <table name>( CREATE TABLE students(
foreign <col1 name> <data type> (size), ->sid int,
key when <col2 name> <data type> (size), ->hid varchar(20),
creating a PRIMARY KEY (col1 name,col2 name), PRIMARY KEY (sid,hid),
table FOREIGN KEY (col name) REFERENCES <tb name>(col name)); FOREIGN KEY (sid) REFERENCES students (sid));

You might also like