SQL server is the most popular database of microsoft company which was develop in 1989. SQL stands for Structure Query Language. It is RDBMs  so in this case record is store in tabular format. It is platform dependent because it runs only on windows operating system.

SQL PRIMARY KEY

Primary key always has unique data. A primary key length cannot be exceeded than 900 bytes. A primary key cannot have null value. This table can contain only one primary key. There can be no duplicate value for a primary key is knows as SQL PRIMARY KEY.

Syntax

CREATE TABLE table_name (id INT NOT NULL AUTO_INCREMENT,

name VARCHAR (50) NOT NULL,

age INT NOT NULL,

city VARCHAR (10) NOT NULL,

PRIMARY KEY (id) );

Example of SQL PRIMARY KEY

create table city (
cid INT not null auto_increment,
cityname varchar (100) not NUll,
primary key (cid) );

1. Input value of SQL PRIMARY KEY

insert into city (cityname)
values (‘bettiah’),
(‘manipur’),
(‘patna’),
(‘harinagar’),
(‘kanpur’);

2. Input value of SQL PRIMARY KEY

Output of SQL PRIMARY KEY

Output Value SQL PRIMARY KEY

SQL FOREIGN KEY

Foreign key is a field or a column that is use to establish a link between two tables. foreign key in one table use to point primary key in another table is knows as SQL FOREIGN KEY.

Syntax

CREATE TABLE table_name (id INT NOT NULL AUTO_INCREMENT,

name VARCHAR (50) NOT NULL,

age INT NOT NULL,

city VARCHAR (10) NOT NULL,

PRIMARY KEY (id) );

FOREIGN KEY (city) REFERENCES City (cid)

Example of SQL FOREIGN KEY

create table personal (
id Int not null,
name varchar (80) not null,
percentage int not null,
age int not null,
gender varchar (1) not null,
city int not null,
primary key (id),
foreign key (city) references city (cid));

Input Value of SQL FOREIGN KEY

Output of SQL FOREIGN KEY

Output Value of SQL FOREIGN KEY

SQL Unique Key

The key is a set of one or more than one columns of a table that uniquely identify a record in a database table is knows as SQL Unique Key.

Example of SQL Unique Key

CREATE TABLE r (
id INT not null unique,
Name varchar (50),
birth_date DATE,
phone VARCHAR(12),
gender VARCHAR(1)
);

1. part Input value of SQL Unique key

INSERT INTO r(id, name, birth_date, phone, gender)
values
(1, “raj”, “1990-10-10”, 784561236, “m”),
(2, “rohit”, “1990-11-10”, 784561236, “m”),
(3, “rohan”, “1995-05-15”, 784561236, “m”),
(4, “rohini”, “1994-09-20”, 784561236, “m”),
(5, “rahul”, “1999-10-12”, 784561236, “m”);

2. part Input value of SQL Unique key

Output of SQL Unique Key

Output value of SQL Unique key

SQL Alternate Key

Feature of Alternate key

  • Add Column in a table.
  • Changing data types of a column.
  • Change Column Name.
  • Adding Constraints to column.
  • Changing Column position.
  • Delete Column
  • Renaming Tables

Example of SQL Alternate Key

Input value of SQL Alternate Key

Output of SQL Alternate Key

Output value of SQL Alternate Ke

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about SQL please Wikipedia click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *