SQL Tutorials - 1
1.1 Relational Database Management Systems
1.2 SQL
1.3 Data Definition Language(DDL)
1.4 Data Manipulation Language(DML)
1.5 Data Control Language(DCL)
2.1 Creating Database
2.2 Creating Table in SQL
Topics covered here are -
2.2.1 Schema
2.2.2 Data Types
2.2.3 Keys & Constraints
2.2.4 Create a table
2.2.5 View table structure
1.1 Relational Database Management Systems
1.2 SQL
1.3 Data Definition Language(DDL):
1.4 Data Manipulation Language:
1.5 Data Control Language(DCL):
2.1 Creating database
As the name implies, Data Definition Language is focused on defining the characteristics of the database and its tables and columns. This includes creating databases and tables, or altering and changing finer details about your database, such as table names and column data types. But it does not deal with the data within a database.
2.2 Creating Table
employee
database, we have the 'outer shell' of our building. Now we need to add some rooms to our building , or in SQL terms we need to add tables.Before Creating a table let us understand about Schema, datatype and constraints.
2.2.1 Schema
2.2.2 Data Types
1) Serial - This data type is used to create identifier columns for a PostgreSQL database. These identifiers are integers, auto-incrementing, and cannot contain a null value.
2) Char(N) - This data type specifies that information stored in a column can contain strings of up to N characters in length. If a string less than length N is stored, then the remaining string length is filled with space characters.
3) Varchar(N) - This data type specifies that information stored in a column can contain strings of up to N characters in length. If a string less than length N is stored, then the remaining string length isn't used.
4) Boolean - This is a data type that can only contain two values "true" or "false". In PostgreSQL, boolean values are often displayed in a shorthand format, t or f
5) Integer or INT - An integer is simply a "whole number." An example might be 1 or 50, -50, or 792197 depending on what storage type is used.
6) Decimal(precision, scale) - The decimal type takes two arguments, one being the total number of digits in the entire number on both sides of the decimal point (the precision), the second is the number of the digits in the fractional part of the number to the right of the decimal point (the scale).
7) Timestamp - The timestamp type contains both a simple date and time in YYYY-MM-DD HH:MM:SS format.
8) Date - The date type contains a date but no time.
2.2.3 Keys & Constraints
While data types are a mandatory part of a column definition, constraints are optional.
One of the key functions of a database is to maintain the integrity and quality of the data that it is storing. Keys( will see when designing multiple tales) and Constraints are rules that define what data values are allowed in certain columns. They are an important database concept and are part of a database's schema definition. Defining Keys and Constraints is part of the database design process and ensures that the data within a database is reliable and maintains its integrity. Constraints can apply to a specific column, an entire table, more than one table, or an entire schema.
UNIQUE: Prevents any duplicate values from being entered into that column.
NOT NULL: Essentially means that when adding data to the table a value MUST be specified for this column; it cannot be left empty.
DEFAULT: If no value is set in this field when a record is created then a value pre-defined is set in that field.
We talk more about UNIQUE
, NOT NULL
and DEFAULT
when we look at adding data into this table later.
2.2.4 Create a table
id serial UNIQUE NOT NULL,
username char(25),
City varchar(50),
enabled boolean DEFAULT TRUE
);
Let's break this down a little to understand exactly what's going on here
CREATE TABLE
: Firstly, CREATE TABLE users
is the primary command.users
: The name of the table that will be created.- (): The information in the parentheses is related to the columns in the table.
id, username, enabled
: These are the three columns of the table.serial
, char(25)
, boolean
: These are the data types of the columns. We will look at data types shortly.UNIQUE
, NOT NULL
: These are constraints. We'll talk about these later in this chapter.DEFAULT TRUE
: Specifies a default value for the column. We'll revisit this later.- Notice that each column definition is comma separated; this is the standard in any SQL database management system.
CREATE TABLE
: Firstly, CREATE TABLE users
is the primary command.users
: The name of the table that will be created.id, username, enabled
: These are the three columns of the table.serial
, char(25)
, boolean
: These are the data types of the columns. We will look at data types shortly.UNIQUE
, NOT NULL
: These are constraints. We'll talk about these later in this chapter.DEFAULT TRUE
: Specifies a default value for the column. We'll revisit this later.
0 Comments