02. Create Database, Tables and Data Constraints in SQL
02. Create Database, Tables and Data Constraints in SQL
SHOW DATABASES;
USE database_name
No DROP and DELETE command are not same. DROP command erases everything whereas
DELETE command erases specific rows.
Eg:
Inserting Values
Eg:
Syntax:
Query:
Important: If you try to create a table that already exists, MySQL will throw an error. To avoid this, you
can use the CREATE TABLE IF NOT EXISTS syntax.
Syntax:
DELETE FROM table_name
WHERE some_condition;
Syntax
ALTER TABLE table_name
ADD column_name datatype;
Query
ALTER TABLE Student ADD marks INT;
Syntax:
To Create Temporary Table:
CREATE TABLE #EmpDetails (id INT, name VARCHAR(25))
Global Temporary Tables are visible to all connections and Dropped when the last connection
referencing the table is closed. Global Table Name must have an Unique Table Name.
DROP Column
MODIFY Column
NOT NULL: This constraint tells that we cannot store a null value in a column. That is, if a column
is specified as NOT NULL then we will not be able to store null in this particular column any more.
UNIQUE: This constraint when specified with a column, tells that all the values in the column must
be unique. That is, the values in any row of a column must not be repeated.
PRIMARY KEY: A primary key is a field which can uniquely identify each row in a table. And this
constraint is used to specify a field in a table as primary key.
FOREIGN KEY: A Foreign key is a field which can uniquely identify each row in a another table.
And this constraint is used to specify a field as Foreign key.
CHECK: This constraint helps to validate the values of a column to meet a particular condition.
That is, it helps to ensure that the value stored in a column meets a specific condition.
DEFAULT: This constraint specifies a default value for the column when no value is specified by
the user.
UNIQUE Constraint
Properties :
No duplicate values are allowed, i.e. Column assigned as primary key should have UNIQUE
values only.
NO NULL values are present in column with Primary key. Hence there is Mandatory value in
column having Primary key.
Only one primary key per table exist although Primary key may have multiple columns.
No new row can be inserted with the already existing primary key.
Classified as : a) Simple primary key that has a Single column 2) Composite primary key has
Multiple column.
Defined in Create table / Alter table statement.
Syntax :
Syntax:
Syntax :
Alter Table Person add Primary Key(Id);
A foreign key is created in the CREATE TABLE or ALTER TABLE statement. The foreign key in a table
should match the primary key in the referenced table for every row. This is called Referential Integrity.
Foreign key ensures referential integrity.
The table in which a foreign key is defined is called a Foreign table/Child table/Referencing table. and
the table that defines a primary key and is referenced by a foreign key is called a Primary table/Parent
table /Referenced Table
Properties:
CREATE TABLE people(no varchar2(10), fname varchar2(20), foreign key(no) references person);
OR
CREATE TABLE people(no varchar2(10), fname varchar2(20),foreign key(no) references person(id));
Syntax:
Now deleting records from the person table will delete all corresponding records from the child table.
Syntax:
CREATE TABLE
people(no varchar2(10), fname varchar2(20), foreign key(no)
references person on delete set null);
Difference Between ON DELETE CASCADE and ON DELETE SET NULL in
DBMS
Composite Key in SQL
Yes, a composite key can be used as a foreign key in another table within a relational database.
DEFAULT Constraints