Sunteți pe pagina 1din 40

 Represents a value for an attribute that is currently unknown or is not applicable

for this tuple.


 A null can be taken to mean the logical value “unknown.” It can mean that a value is
not applicable to a particular tuple, or it could merely mean that no value has yet
been supplied.
 null represents the absence of a value
 This integrity rule applies to the primary keys of base relations.
 In a base relation, no attribute of a primary key can be null.
 Example :
primary key of the Viewing relation, comprising the client number (clientNo) and the
property number (propertyNo). We should not be able to insert a tuple into the
Viewing relation with a null for the clientNo attribute, or a null for the propertyNo
attribute, or nulls for both attributes.
 If a foreign key exists in a relation, either the foreign key value must match a
candidate key value of some tuple in its home relation or the foreign key value must
be wholly null.
 For example, branchNo in the Staff relation is a foreign key targeting the
branchNo attribute in the home relation, Branch. It should not be possible to
create a staff record with branch number B025, for example, unless there is
already a record for branch number B025 in the Branch relation.
 However, we should be able to create a new staff record with a null branch
number to allow for the situation where a new member of staff has joined
the company but has not yet been assigned to a particular branch office.
 Additional rules specified by the users or database administrators of a database
that define or constrain some aspect of the enterprise
 Structured Query Language.
 SQL is an example of a transform-oriented language, or a language
designed to use relations to transform inputs into required outputs.
 Data Definition Language (DDL)
 Data Manipulation Language (DML)
 SQL is the first and, so far, only standard database language to gain wide
acceptance.
 Nearly every major current vendor provides database products based on
SQL or with an SQL interface
 create the database and relation structures;
 perform basic data management tasks, such as the insertion, modification, and
deletion of data from the relations;
 perform both simple and complex queries.
 Boolean data Boolean data consists of the distinct truth values TRUE and FALSE
 Character data consists of a sequence of characters from an implementation
defined character set, that is, it is defined by the vendor of the particular SQL
dialect.
 When a character string column is defined, a length can be specified to indicate
the maximum number of characters that the column can hold.
Example:
branchNo CHAR(4)  fixed length of 4 characters.
When we enter a string with fewer characters than this length, the string is padded
with blanks on the right to make up the required size.
address VARCHAR(30)
addess has a variable number of characters up to a maximum of 30,
 Precision is total number of digits while scale is number of decimal places.
 -12.345:  5 precision, 3 scale
 INTEGER (INT) is used for large positive or negative whole numbers. SMALLINT
is used for small positive or negative whole numbers and BIGINT for very large
whole numbers
Examples :
rooms SMALLINT
salary DECIMAL(7,2)
 The datetime data type is used to define points in time to a certain degree of
accuracy.

 DATE is used to store calendar dates using the YEAR, MONTH, and DAY fields.
 TIME is used to store time using the HOUR, MINUTE, and SECOND fields.
viewDate DATE
 CREATE DATABASE DatabaseName
Example :
CREATE DATABASE testDB;

Example using phpmyadmin.


 DROP DATABASE databasename;
DROP DATABASE testDB;
CREATE TABLE Persons (
PersonID INT,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
);
 NOT NULL - Ensures that a column cannot have a NULL value
 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies
each row in a table
 FOREIGN KEY - Uniquely identifies a row/record in another table
 CHECK - Ensures that all values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column when no value is specified
 INDEX - Used to create and retrieve data from the database very quickly
CREATE TABLE author
(aut_id varchar(8) NOT NULL,
aut_name varchar(50) NOT NULL,
country varchar(25) NOT NULL,
home_city varchar(25) NOT NULL );
CREATE TABLE author2
(aut_id varchar(8) NOT NULL,
aut_name varchar(50) NOT NULL,
country varchar(25) NOT NULL,
home_city varchar(25) NOT NULL,
PRIMARY KEY (aut_id) );
CREATE TABLE author4
(aut_id varchar(8) NOT NULL PRIMARY KEY,
aut_name varchar(50) NOT NULL,
country varchar(25) NOT NULL,
home_city varchar(25) NOT NULL);
CREATE TABLE INFO (
t1ID INT,
t2ID INT,
PRIMARY KEY (t1ID, t2ID)
)
CREATE TABLE book
(book_id varchar(15) NOT NULL,
book_name varchar(50) ,
isbn_no varchar(15) NOT NULL UNIQUE ,
aut_id varchar(8) ,
pub_id varchar(8) ,
dt_of_pub date ,
no_page decimal(5,0)
book_price decimal(8,2) ,
PRIMARY KEY (book_id)
);
CREATE TABLE publisher
(pub_id varchar(8) NOT NULL ,
pub_name varchar(50) NOT NULL DEFAULT ‘pub' ,
pub_city varchar(25) NOT NULL DEFAULT ‘Amman' ,
country varchar(25) NOT NULL DEFAULT ‘Jordan',
no_of_branch int(3) DEFAULT 2,
estd date,
PRIMARY KEY (pub_id));
CREATE TABLE IF NOT EXISTS author3
(id int NOT NULL AUTO_INCREMENT,
aut_id varchar(8),
aut_name varchar(50),
country varchar(25),
home_city varchar(25) NOT NULL,
PRIMARY KEY (id));
 sex CHAR NOT NULL CHECK (sex IN (‘M’, ‘F’))

Create Table pers(


name char(255) PRIMARY KEY,
sex char(1) default ‘m’ check(sex IN(‘m’,’f’))
)
CREATE TABLE products(
product_id INT PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
unit_price DECIMAL(10,2) CHECK(unit_price > 0)
);
 A foreign key is a column, or set of columns, that links each row in the
child table containing the foreign key to the row of the parent table
containing the matching candidate key value.
 Referential integrity means that, if the foreign key contains a value, that value must
refer to an existing, valid row in the parent table.
 For example, the branch number column branchNo in the PropertyForRent table
links the property to that row in the Branch table where the property is assigned.
 If the branch number is not null, it must contain a valid value from the column
branchNo of the Branch table.
CREATE TABLE categories(
categoryId INT AUTO_INCREMENT PRIMARY KEY,
categoryName VARCHAR(100) NOT NULL
);

CREATE TABLE products(


productId INT AUTO_INCREMENT PRIMARY KEY,
productName varchar(100) not null,
categoryId INT,
CONSTRAINT fk_category
FOREIGN KEY (categoryId)
REFERENCES categories(categoryId)
)
CREATE table dept1(
id int PRIMARY KEY,
name varchar(255) );
CREATE TABLE etype(
id int,
name varchar(55),
PRIMARY key(id) );
CREATE TABLE emp( empid int, empname varchar(15), did int, tid int,
FOREIGN KEY(did) REFERENCES dept(id) ,
FOREIGN KEY(tid) REFERENCES etype(id)
)
 add a new column to a table;
 drop a column from a table;
 add a new table constraint;
 drop a table constraint;
 set a default for a column;
 drop a default for a column.
ALTER TABLE publisher
ADD COLUMN col1 INT default 0

ALTER TABLE publisher


ADD COLUMN col1 INT default 0, ADD COLUMN col2 CHARACTER(15) NOT NULL
 ALTER TABLE publisher DROP COLUMN col1;
ALTER TABLE book
ADD CONSTRAINT isbn_u UNIQUE(isbn_no)

ALTER TABLE book DROP INDEX isbn_no


 DROP TABLE tableName.
 An index is a structure that provides accelerated access to the rows of a table
based on the values of one or more columns
 The presence of an index can significantly improve the performance of a query.
 However, as indexes may be updated by the system every time the underlying
tables are updated, additional overheads may be incurred.

 Example:
CREATE INDEX inxName on TABLE(COLUMN ASC or DESC)
CREATE INDEX inx ON book(isbn ASC)
CREATE INDEX Rentlnd ON PropertyForRent (city, rent);
DROP INDEX IndexName

S-ar putea să vă placă și