0% found this document useful (0 votes)
66 views5 pages

NOT NULL Constraint

The document discusses various SQL commands and constraints including NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, data types, aggregate functions, sorting, and ALTER TABLE commands for modifying columns. It provides syntax examples for creating tables with constraints, inserting data, joining tables with foreign keys, selecting data using aggregate functions, and altering table structures.

Uploaded by

Punam Sindhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views5 pages

NOT NULL Constraint

The document discusses various SQL commands and constraints including NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, data types, aggregate functions, sorting, and ALTER TABLE commands for modifying columns. It provides syntax examples for creating tables with constraints, inserting data, joining tables with foreign keys, selecting data using aggregate functions, and altering table structures.

Uploaded by

Punam Sindhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

NOT NULL constraint

CREATE TABLE People(Id INTEGER, LastName VARCHAR(20) NOT NULL,


FirstName VARCHAR(20) NOT NULL, City VARCHAR(55));

INSERT INTO People VALUES(1, 'Hanks', 'Robert', 'New York');

UNIQUE constraint

CREATE TABLE Brands(Id INTEGER, BrandName VARCHAR(30)


UNIQUE);

INSERT INTO Brands VALUES(1, 'Coca Cola');

Primary key

CREATE TABLE Brands(Id INTEGER PRIMARY KEY, BrandName


VARCHAR(30) UNIQUE);

DESCRIBE Brands;

Foreign key

CREATE TABLE Authors(AuthorId INTEGER PRIMARY KEY, Name


VARCHAR(70));

CREATE TABLE Books(BookId INTEGER PRIMARY KEY, Title


VARCHAR(50),AuthorId INTEGER, FOREIGN KEY(AuthorId)
REFERENCES Authors(AuthorId));
SELECT * FROM Books2 WHERE Id=3;

DELETE FROM Books2 WHERE Id=1;

UPDATE Books2 SET Author='Lev Nikolayevich Tolstoy' WHERE Id=1;

DROP TABLE table_name

DROP DATABASE database_name

TRUNCATE TABLE table_name

ALTER TABLE table_name


MODIFY COLUMN column_name datatype

ALTER TABLE Persons


DROP COLUMN DateOfBirth

CREATE INDEX PIndex

ON Persons (LastName, FirstName)


MySQL comes with the following data types for storing a date or a date/time
value in the database:

 DATE - format YYYY-MM-DD


 DATETIME - format: YYYY-MM-DD HH:MI:SS
 TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
 YEAR - format YYYY or YY

SQL AVG() Syntax


SELECT AVG(column_name) FROM table_name

SQL COUNT(column_name) Syntax

The COUNT(column_name) function returns the number of values (NULL values


will not be counted) of the specified column:

SELECT COUNT(column_name) FROM table_name;

SQL COUNT(*) Syntax

The COUNT(*) function returns the number of records in a table:

SELECT COUNT(*) FROM table_name;

SQL FIRST() Syntax


SELECT FIRST(column_name) FROM table_name;

SQL LAST() Syntax


SELECT LAST(column_name) FROM table_name;

SQL MAX() Syntax


SELECT MAX(column_name) FROM table_name;

SQL MIN() Syntax


SELECT MIN(column_name) FROM table_name;

SQL SUM() Syntax


SELECT SUM(column_name) FROM table_name;

SQL GROUP BY Syntax


SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

SQL UCASE() Syntax


SELECT UCASE(column_name) FROM table_name;

SQL LCASE() Syntax


SELECT LCASE(column_name) FROM table_name;

SQL ROUND() Syntax


SELECT ROUND(column_name,decimals) FROM table_name;

SELECT * FROM Customers


ORDER BY Country;
SELECT * FROM Customers
ORDER BY Country DESC;

SELECT * FROM Customers


ORDER BY Country ASC, CustomerName DESC;

ALTER TABLE contacts


ADD last_name varchar(40) NOT NULL
AFTER contact_id;

ALTER TABLE contacts


MODIFY last_name varchar(50) NULL;

ALTER TABLE contacts


DROP COLUMN contact_type;

ALTER TABLE contacts


CHANGE COLUMN contact_type ctype
varchar(20) NOT NULL;

You might also like