Professional Documents
Culture Documents
Database
Database
Database
1 - Database
2 - Table
3 - Column
4 - Row
5 - Primary Key
6 - Foreign Key
7 - Compound Key
8 - Index
9 - Redundancy
//cmd access
=====> mysql -u root -p
ruse: root
password: mysqlUbuntu123**
// list a databases
=====> show databases;
// connect to database
=====> use mysql;
//create a database
=====> create database my_db;
//drop a database
=====> drop database my_db;
// Table structure
=====> show columns from user;
// IF --- ERROR 1819 (HY000): Your password does not satisfy the current policy
requirements
//chech a user
SELECT host, user, authentication_string FROM user WHERE user = 'khalil';
//give grants
grant create,select,insert,update,delete on `cafdo_db%`.* to 'khalil'@'localhost';
//remove grants
REVOKE create,select,insert,update,delete ON `cafdo_db%`.* FROM
'hamdane'@'localhost';
//give grants
grant select on `cafdo_db%`.* to 'hamdane'@'localhost';
//create a database
=====> create database school_db;
//delete database
=====> drop database school_db;
//rename a table
=====> ALTER TABLE school_db. level RENAME TO school_db. levels;
//filter a table elements asc and desc
=====> select * from school_db. `levels` order by name asc;
=====> select * from school_db. `levels` order by name desc;
// logical and comparison operators ( =, <, >, <=, =>, !=, AND, OR NOT....)
select * from school_db. `levels` where id <= 2;
// partial section
=====> select name from school_db. `levels`;
// table structure
=====> show columns from school_db. `levels`;
=====> describe school_db. `levels`;
//delete a table
=====> drop table school_db. `levels`;
//insertion in a table
=====> INSERT INTO school_db. `levels` (name, description) VALUES ('6eme',
'Premiere classe niveau college');
// foreign key
=====>
CREATE TABLE school_db. `students` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`address` VARCHAR(155) NOT NULL,
`fees` VARCHAR(155) NOT NULL,
`level_id` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`,`level_id`),
KEY `tbl_level` (`level_id`),
CONSTRAINT `tbl_level` FOREIGN KEY (`level_id`)
REFERENCES `levels` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB DEFAULT CHARSET=utf8
// insertion in a table
=====> INSERT INTO school_db. `students` (name, address, fees, level_id) VALUES
('Amna Khali Hisseine', 'Rue de 40 Metres', 200000, 6);
// search
=====> select * school_db. `students` where name like '%s';
=====> select * from `students` where name like '%s%';
// select from and join 2 table
=====> SELECT students.*, levels.name as classe FROM students RIGHT JOIN levels
ON levels.id = students.level_id ;
=====> SELECT students.*, levels.name as classe FROM students RIGHT JOIN levels
ON levels.id = students.level_id where students.id != 0;