0% found this document useful (0 votes)
51 views10 pages

SQL Database Creation and Management Guide

The document describes the creation of three database tables - Student, Subject, and Grade. The Student table has a primary key of StudentID and includes fields for student name, address, hobby, and identification number. The Subject table has a primary key of SubjectID and fields for subject name and credit units. The Grade table has a composite primary key of StudentID and SubjectID and includes a grade field. Grade table also has foreign key relationships to the Student and Subject tables. The document then provides examples of SQL commands to insert data into the tables, update data, display data with SELECT statements, and explains how to combine data from multiple tables in a query.

Uploaded by

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

SQL Database Creation and Management Guide

The document describes the creation of three database tables - Student, Subject, and Grade. The Student table has a primary key of StudentID and includes fields for student name, address, hobby, and identification number. The Subject table has a primary key of SubjectID and fields for subject name and credit units. The Grade table has a composite primary key of StudentID and SubjectID and includes a grade field. Grade table also has foreign key relationships to the Student and Subject tables. The document then provides examples of SQL commands to insert data into the tables, update data, display data with SELECT statements, and explains how to combine data from multiple tables in a query.

Uploaded by

Cakep Cantik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Table Student.

Primary Key(studentid)

UNIQUE(KTPNumber)

StudentID StudentName Address Hobby KTPNumber


1 Lexy Cawang Playing games 123
2 Sirly Cikarang cycling 231
3 Adinda Cikarang Cooking 321
CREATE TABLE Student
( StudentID Char(12) ,
StudentName Varchar(30) ,
Address Varchar(50) ,
Hobby Varchar(15) ,
KTPNumber Char(10),
CONSTRAINT PKStudent PRIMARY KEY(studentid),
CONSTRAINT CKStudent UNIQUE(KTPNumber)
)

INSERT INTO Student


VALUES('3','Michael','Jakarta','playing game','432')
Table Subject. Primary Key(Subjectid)

SubjectID SubjectName Credit Units


STAT Statistics 3
DB Database System 3

CREATE TABLE Subject


( SubjectID Char(4) ,
SubjectName Varchar(20) ,
CreditUnits integer,
CONSTRAINT PK_Subject PRIMARY KEY(subjectid)
)
INSERT INTO Subject
VALUES('DB','Database',3)

Table Grade. Primary Key(studentid,subjectid)

FOREIGN KEY(studentid) REFERENCES Student(Studentid)

FOREIGN KEY(subjectid) REFERENCES subject(subjectid)

StudentID SubjectID Grade


1 STAT A
1 DB A
8 ERROR DB B
ERROR

CREATE TABLE Grade

StudentID char(12),

SubjectID char(4),

Grade char(2),

CONSTRAINT PK_Grade PRIMARY KEY(StudentID,SubjectID),

CONSTRAINT FK_Grade_Student FOREIGN KEY(StudentID) References Student(Studentid),

CONSTRAINT FK_Grade_Subject FOREIGN KEY(SubjectID) References Subject(Subjectid)

INSERT INTO grade

VALUES('1','XXY','A')
SQL is case insensitive : Upper-case letter and lower-
case letter is considered to be the same.
How to create a database ?
CREATE DATABASE ……………(database name. it is up to
you, space is not allowed) ……….
Example : CREATE DATABASE PresidentUniversity

How to create a table ?


CREATE TABLE …Table Name (space is not allowed) …
(
…….columnName…… …….DataType……. ,
…….columnName…… …….DataType……. ,
…….columnName…… …….DataType……. ,
CONSTRAINT ….constraint name …. PRIMARY KEY(columnName),
CONSTRAINT ….constraintName…. FOREIGN KEY (columnName)
REFERENCES ……TableName……. (…columnName…)
CONSTRAINT …..constraintName …. UNIQUE(columnName)

DataType can be Char(….), varchar(….), integer,


decimal, etc.
HOW TO INSERT A DATA INTO A TABLE ????
INSERT INTO table-name
VALUES(valueColumn1, valueColumn2, valueColumn3)

Remember that the value that has data type char or


varchar has to be surrounded by single quote(‘)

HOW TO DROP A TABLE ???

DROP TABLE table-name

UNIQUE(KTPNumber)
StudentID StudentName Address Hobby KTPNumber
1 Lexy Cawang Playing games 123
2 Sirly Cikarang cycling 231
3 Adinda Cikarang Cooking 321

Table Subject. Primary Key(Subjectid)

SubjectID SubjectName Credit Units


STAT Statistics 3
DB Database System 3

Table Grade. Primary Key(studentid,subjectid)

StudentID SubjectID Grade


1 STAT A
1 DB B
2 STAT C

HOW TO UPDATE a DATA IN A TABLE in SQL ???


UPDATE table-name
SET column-name = new-value
WHERE column-name = value
AND column name = value
AND column name = value
AND column name = value

HOW TO DELETE ALL THE CONTENT OF A TABLE ?


DELETE FROM table-name
This Instrution will delete all rows in a table.

HOW TO DELETE A CERTAIN ROWS OF A TABLE ?


DELETE FROM table-name
WHERE column-name = value
AND column name = value
AND column name = value
AND column name = value

I want to change the table student column hobby to


become ‘reading books’ for Laetare
UPDATE student
SET hobby = ‘reading books’
WHERE studentname = ‘laetare’

How to display the content of a table ???


SELECT column name separated by comma
FROM table name
WHERE column name = value
AND column name = value
AND column name = value
AND column name = value

SELECT *
FROM student
WHERE hobby = ‘swimming’

How if we want change all students that have address


cikarang to Bekasi.

How if I want to see all students that have address in


Bekasi and the hobby is swimming ?
Select *
From student
WHERE address = ‘bekasi’
AND hobby = ‘swimming’

HOW TO use SELECT by combining 2 tables ????


SELECT column name separated by comma
FROM table name1, table name2, table name3, table
name4
WHERE column name = value
AND column name = value
select studentname, subjectid, grade
from student, grade

From the results of that SQL instruction we have to


display all rows that have the value of column
studentid from table student is the same as the value
of column studentid from table grade.

select studentname, subjectid, grade


from student, grade
where [Link] = [Link]

select studentid, subjectname, Grade


from subject, grade
where [Link] = [Link]

studentname subjectname grade

We need to combine 3 tables : student, grade,


SubjectID
What is the common columns between table student
and grade : StudentID
What is the common columns between table subject
and grade :SubjectID
What is the common columns between table student
and subject : nothing

select studentname, subjectname, grade


from grade, subject, student
where [Link] = [Link]
AND [Link] = [Link]

You might also like