The document explains various constraints in MySQL, including NOT NULL, PRIMARY KEY, FOREIGN KEY, UNIQUE, DEFAULT, and CHECK constraints, which are used to enforce rules on the data stored in tables. It also provides syntax examples for creating tables with these constraints and demonstrates how to insert, update, delete, and query data using SQL commands. Additionally, it covers the use of ORDER BY, GROUP BY, and DISTINCT clauses for data retrieval.
The document explains various constraints in MySQL, including NOT NULL, PRIMARY KEY, FOREIGN KEY, UNIQUE, DEFAULT, and CHECK constraints, which are used to enforce rules on the data stored in tables. It also provides syntax examples for creating tables with these constraints and demonstrates how to insert, update, delete, and query data using SQL commands. Additionally, it covers the use of ORDER BY, GROUP BY, and DISTINCT clauses for data retrieval.
MYSQL? Constraints in MySQL is used to define rules for what values can be stored in rows and columns in a table. It is used to limit the data that can be inserted into a table. The action is canceled if there is a conflict between the constraint and the data action. This also helps in maintaining the reliability and accuracy of the data. NOT NULL CONSTRAINT IN MYSQL The NOT NULL constraint is used for a column in a table. It ensures that there should not be any NULL value in that column. It is generally assigned for a column while creating a table. The syntax for this is shown below. column_name datatype NOT NULL; THE PRIMARY KEY CONSTRAINT IN MYSQL A primary key is a column or a • Single column as primary key set of columns that uniquely defines each row or tuple in a • CREATE TABLE table_name( table. • column_name datatype • There can be only one primary PRIMARY KEY key in a table. • ); • The primary key can't contain NULL values. • A set of columns as primary key • If we try to insert NULL value • CREATE TABLE table_name( to a primary key column, it will • column_name datatype, cause an error. • A primary key must contain • PRIMARY KEY (column_list) unique values. For a set of • ); columns, the combination of columns must be unique. THE FOREIGN KEY CONSTRAINT IN MYSQL A foreign key is a column or set of columns in a table that references to a column or set of columns of another table. It helps in maintaining links among the tables. Typically, the referenced column or set of columns of another table is the primary key of that table. CREATE TABLE result( roll_no INT, reg_no INT, marks INT FOREIGN KEY (reg_no) REFERENCES students(reg_no) ); UNIQUE CONSTRAINT IN MYSQL The UNIQUE constraint is used in MySQL to ensure uniqueness in the values of a column or set of columns in a table. When a duplicate value is inserted in a column with a unique constraint, MySQL rejects that insertion and issues an error. Add the UNIQUE constraint to a single column CREATE TABLE table_name( column_name data_type UNIQUE ); DEFAULT CONSTRAINT IN MYSQL The DEFAULT constraint is used in MySQL to specify a default value for a column. When a value is not inserted into a column having a DEFAULT constraint, the specified DEFAULT value is inserted by MySQL. Syntax: column_name data_type DEFAULT default_value; Ex. CREATE TABLE result (reg_no INT, name VARCHAR(255), marks INT DEFAULT 0); MYSQL CHECK CONSTRAINT 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.If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row. The following SQL creates a CHECK constraint on the "Age" column when the "Persons" table is created. The CHECK constraint ensures that the age of a person must be 18, or older: CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, CHECK (Age>=18) ); PRACTI CE THE DATABASE Create db Section1; Use Section1; create table stud(name varchar(20), ssn varchar(14) not null, email varchar(20), level int, primary key(ssn)); INSERT DATA INTO TABLE insert into stud values (‘omar','1646766789',‘[email protected]',3); insert into stud(name,ssn,email,level) values ('mohamed','123456789', '[email protected]',3); insert into stud(name,ssn,level) values ('ali','5556456789',3); insert into stud(name,ssn,level) values ('asd','5556456789',3); ERROR 1062 (23000): Duplicate entry '5556456789' for key 1 UPDATE UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];
update stud set level = 4 where name ='ali' ; without where will update all record to level 4 DELETE FROM TABLE DELETE FROM table_name WHERE [condition];
delete from stud where name='ali';
delete from stud; delete all record ORDER BY SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC]; select * from stud order by name asc; select * from stud order by name desc; Insert into stud values(‘ali’,’433325465’,’[email protected]’,4) select * from stud order by name , ssn asc; select * from stud order by name , ssn desc; GROUP BY SELECT column1, column2 FROM table_name WHERE [ conditions ] GROUP BY column1, column2
select name,sum(level) from stud group by name;
select name,sum(level) as summation from stud group by name; DISTINCT No duplicated data select distinct name from stud; Thanks