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

Create Database, - Create Table, - Alter Table, - Create Table With Constraint - Primary Key - Foreign Key - Date Time - Time Stamp

The document discusses SQL commands like CREATE DATABASE, CREATE TABLE, ALTER TABLE, and adding constraints. It also covers creating tables with primary keys, foreign keys, and datetime/timestamp data types. Examples are given of creating tables and inserting records.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Create Database, - Create Table, - Alter Table, - Create Table With Constraint - Primary Key - Foreign Key - Date Time - Time Stamp

The document discusses SQL commands like CREATE DATABASE, CREATE TABLE, ALTER TABLE, and adding constraints. It also covers creating tables with primary keys, foreign keys, and datetime/timestamp data types. Examples are given of creating tables and inserting records.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

SQL

• Create Database,
• Create Table,
• Alter Table,
• Create Table with constraint
• Primary key
• Foreign key
• Date time
• Time stamp
Create database

The CREATE DATABASE statement is used to


create a new SQL database.

create database library;


Create Table
CREATE TABLE table_name  CREATE TABLE booktype(
( bktypeid int,
    column1 datatype, bktypedesc varchar(20)
    column2 datatype,
    column3 datatype, );
   ....
);
Create Table with constraints
NOT NULL - Ensures that a column cannot have a NULL value
UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table
FOREIGN KEY - Prevents actions that would destroy links between tables
CHECK - Ensures that the values in a column satisfies a specific condition
DEFAULT - Sets a default value for a column if no value is specified
CREATE INDEX - Used to create and retrieve data from the database very
quickly
Create Table with constraints
CREATE TABLE table_name ( CREATE TABLE discount(
    column1 datatype constraint,
    column2 datatype constraint, distypeid int primary key,
    column3 datatype constraint, distypedesc varchar(20)
    ....
); );
Alter Table
• The ALTER TABLE statement is used to add,
delete, or modify columns in an existing
table.

• The ALTER TABLE statement is also used to


add and drop various constraints on an
existing table.
Alter table booktype with constraints

ALTER TABLE Persons ALTER TABLE booktype

ADD PRIMARY KEY (ID);
ADD PRIMARY KEY (bktypeid);
Constraint – Not Null
• The NOT NULL constraint enforces a column
to NOT accept NULL values.

• This enforces a field to always contain a


value, which means that you cannot insert a
new record, or update a record without
adding a value to this field.
Create table publisher with constraints
( Not Null )
CREATE TABLE tablename (

    publisherid …….  NOT NULL,

    publishername …… NOT NULL,

    ……..

);
Create table dept and
alter with constraints ( Not Null )
ALTER TABLE dept

MODIFY deptdesc varchar(10) NOT NULL;
Create Table

Publisher

Dept
Foreign key in Table
• A FOREIGN KEY is a field (or collection of
fields) in one table, that refers to the
PRIMARY KEY in another table.

• The table with the foreign key is called the


child table, and the table with the primary
key is called the referenced or parent table.
Foreign key in Table
• The “deptid" column in the “dept" table is the PRIMARY KEY in
the “dept" table.

• The “deptid" column in the “student" table is a FOREIGN KEY


in the “student" table.

• The FOREIGN KEY constraint prevents invalid data from being


inserted into the foreign key column, because it has to be one
of the values contained in the parent table.
Create Table “student” with foreign key
CREATE TABLE table_name (
    column1 ….,
    column2…..,
    PRIMARY KEY (column name),
    FOREIGN KEY (column
name) REFERENCES tablename(column name)
); create table student(
sid varchar(20),
stname varchar(50),
deptid int,
emailadd varchar(70),
gsm bigint,
primary key (sid),
foreign key (deptid) references dept(deptid));
Create Table
Staff

Allotment

Book

Book_request
datetime & timestamp
• MySQL comes with the following data types for
storing a date or a date/time value in the database:

• DATE - format YYYY-MM-DD


• DATETIME - format: YYYY-MM-DD HH:MI:SS
• TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
• YEAR - format YYYY or YY
datetime & timestamp
• When you insert a TIMESTAMP value into a table,
MySQL converts it from your connection's time zone
to UTC for storing.
• When you query a TIMESTAMP value, MySQL
converts the UTC value back to your connection's
time zone.
• Note that this conversion does not take place for
other temporal data types such as DATETIME .
Create Table “issue”
create table issue(
issueid varchar(10),
bookid ……….,
stuid varchar(20),
staffed ……….,
issuedate timestamp default current_timestamp,
expdateofreturn date,
primary key (issueid),
foreign key (stuid) references student(sid)
foreign key (stfid) references staff(stfid));
Insert records into table
• Method 1:
INSERT INTO table_name (column1, column2,
column3, ...) VALUES (value1, value2, value3, ...);

INSERT INTO discount(distypeid, distypedesc)


VALUES (11,'director');
Insert records into table

• Method 2:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);

INSERT INTO discount VALUES (12,'dean-A');

You might also like