Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Database

The document discusses SQL statements for creating, altering, and dropping databases and tables. It also covers SQL data types and constraints such as primary keys, foreign keys, checks, defaults, and auto-increment.

Uploaded by

suliwen7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Database

The document discusses SQL statements for creating, altering, and dropping databases and tables. It also covers SQL data types and constraints such as primary keys, foreign keys, checks, defaults, and auto-increment.

Uploaded by

suliwen7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Create DB

The create database statement is used to create a new SQL database.


Syntax:
CREATE DATABASE databasename;

Drop DB
The drop database statement is used to drop an existing SQL database.
Syntax:
DROP DATABASE databasename;

Create Table
The create table statement is used to create a new table in a database.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,

);
The column parameters specify the names of the columns of the table. The datatype parameter
specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).

Data Types Reference


Int
Currency
Time
Text
Date
Bit
Varchar

Drop table
The drop table statement is used to drop an existing table in a database.
Syntax:
DROP TABLE table_name;

Alter Table
The alter table statement is used to add, delete, or modify columns in an existing table. It’s also
used to add and drop various constraints on an existing table.

To add a column in a table, use the following syntax:


ALTER TABLE table_name
ADD column_name datatype;
To delete a column in a table, use the following syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
To rename a column in a table, use the following syntax:
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
ALTER / MODIFY COLUMN column_name datatype;
VINCOLI (CONSTRAINCTS)
SQL constraints are used to specify rules for data in a table. Constraints can be specified when the
table is created with the create table statement, or after the table is created with the alter table.
Syntax:
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,

);

Not Null
By default, a column can hold null values. 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.
Example:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);

Unique
The unique constraint ensures that all values in a column are different. You can have many
unique constraints per table, but only one Primary Key constraint per table.

Example on MySQL:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);

Primary key
The primary key constraint uniquely identifies each record in a table.
Primary keys must contain unique values, and cannot contain null values. A table can have only one
primary key; and in the table, this primary key can consist of single or multiple columns (fields).

Example on MySQL:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
Foreign key
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. 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.
Example on MySQL:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

Check
The check constraint is used to limit the value range that can be placed in a column.
If you define a check constraint on a column it will allow only certain values for this column.

Example on MySQL:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);

Default
The default constraint is used to set a default value for a column.
The default value will be added to all new records, if no other value is specified.

Example on MySQL
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);
Auto Increment
Auto-increment allows a unique number to be generated automatically when a new record is
inserted into a table.
Often this is the primary key field that we would like to be created automatically every time a new
record is inserted.

Syntax for MySQL


CREATE TABLE Persons (
Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);
MySQL uses the auto_increment keyword to perform an auto-increment feature. By default, the
starting value is 1, and it will increment by 1 for each new record. To let the auto_increment
sequence start with another value, use the following SQL statement:
ALTER TABLE Persons AUTO_INCREMENT=100;

Select
Select distinct
Where
In
Not in
Between
#
Like

You might also like