T able Creation and Data manipulation commands
18.1 Introduction
18.2 Database in MsSQL
18.3 Creating Tables
18.4 Changing Data with DML Commands
18.5 More DDL Commands
18.1 Introduction
Here you learn commands that let you create tables , add data in table,modify/change data in tables, delete
tuples from tables etc..
18.2 Database in , MYSQL
The first step in setting up a MySQL database is to create the actual database object, which serves as a container for the
tables in that database.
18.2.1 Creating database
CREATE DATABASE RISfbd;
Or
CREATE DATABASE IF NOT EXISTS RISfbd;
18.2.2 To check the names of existing databases
SHOW DATABASES;
18.2.3 Opening Databases
USE RISfbd;
18.2.4 Removing Databasaes
DROP DATABASE RISfbd;
18.3 Creating Tables
CREATE TABLE RISfbd
(Addno integer primary key notNULL,
Stuname char(20),
Sex char(1));
SQL Constraints
Constraints are the rules enforced on data columns on a table. These are used to limit the type of data that can go into
a table. Constraints can either be column level or table level.
Following are some of the most commonly used constraints available in SQL −
NOT NULL Constraint − Ensures that a column cannot have a NULL value.
DEFAULT Constraint − Provides a default value for a column when none is specified.
UNIQUE Constraint − Ensures that all the values in a column are different.
PRIMARY Key − Uniquely identifies each row/record in a database table.
1|Page
FOREIGN Key − Uniquely identifies a row/record in any another database table.
CHECK Constraint − The CHECK constraint ensures that all values in a column satisfy certain conditions.
INDEX − Used to create and retrieve data from the database very quickly.
Viewing a Table Structure
DESC RISfbd;
18.4 Changing Data with DML Commands
1. Inserting Data into table
INSERT INTO RISfbd VALUES(1001,’AKSHIT’,’M’);
2. Modifying Data
UPDATE RISfbd
SET stuname=”Vivaan”
WHERE addno=3090;
3. Deleting Data
DELETE FROM RISfbd
WHERE stuname=”[Link]”;
18.5 More DDL Commands
1. Add new column
ALTER TABLE RISfbd
ADD (Mob_Number integer);
2. Removing Table
DROP TABLE RISfbd;
Assignment
1.
Create a table Department based on following table instance chart
Column Name ID NAME
Data Type integer varchar
Length 8 80
Constraints Primary key NOT NULL
*** end ***
2|Page