0% found this document useful (0 votes)
20 views8 pages

SQL Database Management Commands Guide

The document provides SQL commands for managing databases, including creating, dropping, and altering databases and tables. It also covers data manipulation commands such as inserting, deleting, and adding constraints, as well as handling foreign keys and identity columns. Examples are included for clarity on how to execute each command effectively.

Uploaded by

cgichira
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)
20 views8 pages

SQL Database Management Commands Guide

The document provides SQL commands for managing databases, including creating, dropping, and altering databases and tables. It also covers data manipulation commands such as inserting, deleting, and adding constraints, as well as handling foreign keys and identity columns. Examples are included for clarity on how to execute each command effectively.

Uploaded by

cgichira
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

1.

#Create DB

CREATE DATABASE database_name;

Example:

CREATE DATABASE equip_db;

2. #Drop DB

DROP DATABASE database_name;

Example:

DROP DATABASE equip_db;

NB: Above command may fail if anyone is connected to the DB hence use below
command first

ALTER DATABASE database_name SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

DROP DATABASE database_name;

Example:

USE master

go

ALTER DATABASE equip_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

DROP DATABASE equip_db;

3. #ALTER DATABASE
ALTER DATABASE database_name MODIFY NAME = new_database_name;

Example
ALTER DATABASE equip_db MODIFY NAME = equip_new_db;

ALTER DATABASE equip_new_db MODIFY NAME = equip _db;

4. #Create Table

use database_name

go
create table table_name(

column_name data_type(length) (null / not null),

column_name data_type(length) (null / not null),

column_name data_type(length) (null / not null),

PRIMARY KEY (column_name)

);

Example

Use equip_db

go

create table tbl_units(

unit_id int NOT NULL,

unit_name VARCHAR(50) NOT NULL,

PRIMARY KEY(unit_id)

);

CREATE TABLE tbl_students(

student_id int NOT NULL,

first_name VARCHAR(50) NOT NULL,

last_name VARCHAR(50) NOT NULL,

email VARCHAR(50) NULL,

age INT NOT NULL,

gender INT NOT NULL,

PRIMARY KEY (student_id)

);

5. #Rename Table

RENAME TABLE old_table_name TO new_table_name;

Example:
STD METHOD IS BELOW

RENAME TABLE old_table_name TO new_table_name;

HOWEVER DIFFERENT DBS EXECUTE THIS RULE DIFFERENTLY

MS SQL USES

EXEC sp_rename 'old_table_name', 'new_table_name';

Example

EXEC sp_rename 'dbo.tbl_units', 'units_table'

MYSQL USES

RENAME TABLE Customers TO Clients;

PostgreSQL

ALTER TABLE old_table_name RENAME TO new_table_name;

6. #Drop Table

DROP TABLE table_name;

Example

DROP TABLE tbl_new_units;

7. #Add Data to Table

INSERT INTO table_name(column1,column2,..)


VALUES(value1,value2,...);

Example:

INSERT INTO tbl_students(student_id,first_name,last_name,email,age,gender)

VALUES(1,'angela', 'achieng','aachieng@[Link]',22,2);

INSERT INTO tbl_students(student_id,first_name,last_name,email,age,gender)

VALUES(2,'benard', 'busolo','bbusolo@[Link]',23,1);

INSERT INTO tbl_students(student_id,first_name,last_name,email,age,gender)


VALUES(3,'cate', 'chebet','cchacha@[Link]',20,3);

INSERT INTO tbl_students(student_id,first_name,last_name,email,age,gender)

VALUES(4,'dan', 'donkol','ddonol@[Link]',24,1);

8. # DELETE A VALUE FROM A TABLE


DELETE FROM tablename WHERE column=uniqu_iden fier;

Example
DELETE FROM tbl_students WHERE id=1;

9. #Add default constraint

use database_name

go

ALTER TABLE table_name;

ADD CONSTRAINT constrain_name;

DEFAULT value FOR column_name

Example:

#Add default constraint

use equip_db

go

ALTER TABLE tbl_students;

ADD CONSTRAINT DF_tblStudents_genderId

DEFAULT 3 FOR gender

#Add new record without gender specified and let the default gender be
automa cally added

USE [equip_db]

GO
INSERT INTO tbl_students(id,first_name,last_name,email,age)

VALUES(4, 'eddy','echesa','eechesa@[Link]',20);

INSERT INTO tbl_students(id,first_name,last_name,age,gender)

VALUES(4, 'fatuma','farah','ffarah@[Link]',21,2);

10. #Remove default gender constraint

use equip_db

go

ALTER TABLE table_name

DROP CONSTRAINT constraint_name

Example

use equip_db

go

ALTER TABLE tbl_students

DROP CONSTRAINT DF_tblStudents_genderId

11. #FOREIGN KEY CONSTRAINT

ALTER TABLE table_name


ADD CONSTRAINT constraint_name
FOREIGN KEY (column_name) REFERENCES table_name(column_name);

Example

Lets create the table to reference first and insert data

create table tbl_genders(

gender_id int NOT NULL,


gender_name VARCHAR(20) NOT NULL,

);

INSERT INTO tbl_genders(1,’Male’);

INSERT INTO tbl_genders(2,’Female’);

INSERT INTO tbl_genders(3,’Unknown’);

ALTER TABLE tbl_students


ADD CONSTRAINT FK_studentsTable_genderTable
FOREIGN KEY (gender) REFERENCES tbl_genders (gender_id);

12. CASCADE CONSTRAINT

#CASCADE ON DELETE (NO ACTION/SET TO DEFAULT/SET TO NULL/CASCADE)

use [test1]

go

SELECT * FROM tbl_genders;

SELECT * FROM tbl_students;

DELETE FROM tblGender WHERE id=3;

*****************************************

ALT + F1 Gives you details about a table

*****************************************

USE CHECK CONTRAINTS TO PREVENT UNACCEPTABLE VALUES e.g. nega ve age or


age over 130yrs

SELECT * FROM tbl_people;

ADD USING GUI


****************

INSERT INTO tbl_people VALUES(1004,'Dikolo','Dan',-772);

REMOVE USING SQL COMMAND PROMPT

*********************************

ALTER TABLE tbl_people

DROP CONSTRAINT CK_tbl_people_age

ADD USING COMMAND SQL

*********************

ALTER TABLE tbl_people

ADD CONSTRAINT CK_tbl_people_age CHECK(age > 0 AND age < 150);

BELOW SHOULDN'T WORK

*********************

INSERT INTO tbl_people VALUES(1004,'Dikolo','Dan',-772);

13. IDENTITY COLUMN

IDENTITY COLUMN ENABLE USERS NOT TO BE REQUIRED TO SUPPLY IDENTITY


COLUMN VALUES e.g. student adm no, Invoice no, cket number, these should be
provided by the system automa cally

CREATE TABLE tbl_std(

id INT NOT NULL PRIMARY KEY IDENTITY(1001,1),

student_name VARCHAR(50) NOT NULL

);

INSERT INTO tbl_std(student_name)

VALUES('aakirapa');
INSERT INTO tbl_std(student_name)

VALUES('bbusolo');

SELECT * FROM tbl_std

SET IDENTITY_INSERT tbl_std OFF

INSERT INTO dbo.tbl_employees VALUES('Muindi',29);

SELECT * FROM tbl_employees;

DBCC CHECKIDENT(tbl_employees, RESEED,3001)

INSERT INTO dbo.tbl_employees VALUES('Muindi',29);

SELECT * FROM tbl_employees;

You might also like