Creating and Opening Database:
CREATE DATABASE ;
Example: mysql> create database School;
Now the database with the given name will be created. To open database give following command:
mysql> use School;
Creating Tables
Tables are defined with the CREATE TABLE command. When tables are created its columns are named,data
types and sizes supplied for each column. At least one column must be specified.
Syntax:
CREATE TABLE (,
,…..….,);
Example:
mysql> CREATE TABLE Students(RollNo INTEGER(3), Name VARCHAR(25));
Once the table is created we can insert the record init, edit or delete existing records,and also we can search
for desired record in a very comprehensive way using the SQL Select statement.
Creating tables with Constraints:
A Constraint is a condition or check applicable to a column or table which ensures the integrity or validity of
data. Constraints are also called Integrity constraints. The following constraints are commonly used in MySQL.
Constraintsname Description
PRIMARYKEY Used to create a primary key
UNIQUE to create a unique key
NOTNULL to define that column will not accept null values.
CHECK to define the custom rule or condition.
:: 69 ::
Not Null and Default constraints can be applied only at column level rest all constraints can be applied on
both column level and table levels.
A table may have multiple UNIQUE constraints but only one PRIMARY KEY is allowed.
Example:
CREATE TABLE Student
(StCode char(3) NOT NULL PRIMARY KEY,
Stname char(20) NOT NULL,
StAdd varchar(40),
AdmNo char(5) UNIQUE,
StSex char(1) DEFAULT ‘M’,
StAge integer CHECK (StAge>=10),
Stream char(1) CHECK Stream IN (‘S’, ‘C’, ‘A’) );
No comments:
Post a Comment