├── img2.png ├── image.png └── README.md /img2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Purvapatel4725/MySQL-Cheatsheet/HEAD/img2.png -------------------------------------------------------------------------------- /image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Purvapatel4725/MySQL-Cheatsheet/HEAD/image.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQL on Linux Terminal Using MySQL 2 | 3 | ### Note: 4 | Once you enter the MySQL CLI, you will need to use “;” for it to interpret the command. Otherwise, it will just move to the next line, allowing you to enter more commands. The SQL language is not case-sensitive. However, the order of the commands matters (e.g., `SELECT` goes first, then `FROM`, followed by other parameters). Dates in SQL are represented as integers and must be wrapped between “ ” (e.g., “YYYY-MM-DD”). Also, Fields and columns are used interchangeably but mean the same thing for this content. 5 | 6 | ### Regex Summary: 7 | - `^` means the beginning. 8 | - `$` means the end. 9 | - `|` is a logical OR. 10 | - `[abcd]` specifies any character from a, b, c, d. 11 | - `[a-c]` specifies the range from “a to c”. 12 | 13 | To get a deeper understanding of regex expressions used in SQL, here's a handy reference table: 14 | 15 | ![Regex](image.png) 16 | 17 | ## Installation Steps 18 | 19 | 1. **Install MySQL:** 20 | ```bash 21 | sudo apt install mysql-server -y 22 | ``` 23 | This command installs MySQL on your machine. 24 | 25 | 2. **Verify Installation:** 26 | ```bash 27 | sudo systemctl status mysql 28 | ``` 29 | If it says ‘active (running)’, the installation was successful. 30 | 31 | 3. **Start MySQL:** 32 | ```bash 33 | sudo mysql 34 | ``` 35 | This command starts MySQL in the terminal. 36 | 37 | ## Basic Commands 38 | 39 | 4. **Show Databases:** 40 | ```sql 41 | show databases; 42 | ``` 43 | Displays all available databases. 44 | 45 | 5. **Create a Database:** 46 | ```sql 47 | create database {name}; 48 | ``` 49 | Creates a new database with the specified name. 50 | 51 | 6. **Use a Database:** 52 | ```sql 53 | use {name of the database}; 54 | ``` 55 | Switches to a specific database for further operations. 56 | 57 | ## Table Operations 58 | 59 | 7. **Create a Table:** 60 | ```sql 61 | create table {name of the table} ( 62 | id int, 63 | name varchar(255), 64 | gender boolean 65 | ); 66 | ``` 67 | Creates a table with columns `id`, `name`, and `gender`. 68 | 69 | 8. **Show Tables:** 70 | ```sql 71 | show tables; 72 | ``` 73 | Displays all tables in the current database. 74 | 75 | 9. **Describe a Table:** 76 | ```sql 77 | describe {name of the table}; 78 | ``` 79 | Displays the structure of the specified table. 80 | 81 | ## Data Operations 82 | 83 | 10. **Insert Values:** 84 | ```sql 85 | insert into {table name} values (1, “Purva”, True); 86 | ``` 87 | Inserts values into the specified table. 88 | 89 | 11. **Select All:** 90 | ```sql 91 | select * from {name of the table}; 92 | ``` 93 | Displays all data from the specified table. 94 | 95 | 12. **Filter Data:** 96 | ```sql 97 | select * from {name of the table} where name = “Purva”; 98 | ``` 99 | Filters data where the `name` field is “Purva”. 100 | 101 | 13. **Delete Data:** 102 | ```sql 103 | delete from {name of the table} where name = “Purva”; 104 | ``` 105 | Deletes rows where the `name` field is “Purva”. 106 | 107 | 14. **Update Data:** 108 | ```sql 109 | update {name of the table} set name = “Patel” where id = 1; 110 | ``` 111 | Updates the `name` field to “Patel” where `id` is 1. 112 | 113 | ## Advanced Commands 114 | 115 | 15. **Order Data:** 116 | ```sql 117 | select * from {name of the table} order by id asc; 118 | ``` 119 | Orders data by `id` in ascending order. 120 | 121 | 16. **Add a Column:** 122 | ```sql 123 | alter table {name of the table} add LastName varchar(255); 124 | ``` 125 | Adds a new column `LastName` to the table. 126 | 127 | 17. **Arithmetic Operations:** 128 | ```sql 129 | select age + 1 from {name of the table}; 130 | ``` 131 | Performs arithmetic operations on `age` column. 132 | 133 | 18. **Set Alias:** 134 | ```sql 135 | select age + 1 as NewAge from {name of the table}; 136 | ``` 137 | Sets an alias `NewAge` for the result of the arithmetic operation. 138 | 139 | 19. **Distinct Values:** 140 | ```sql 141 | select distinct FirstName from {name of the table}; 142 | ``` 143 | Displays unique values from the `FirstName` column. 144 | 145 | 20. **IN Operator:** 146 | ```sql 147 | select * from {name of the table} where name in (“Purva”, “Patel”); 148 | ``` 149 | Filters data where the `name` field is either “Purva” or “Patel”. 150 | 151 | 21. **BETWEEN Operator:** 152 | ```sql 153 | select * from {name of the table} where age between 20 and 30; 154 | ``` 155 | Displays data where `age` is between 20 and 30. 156 | 157 | 22. **LIKE Operator:** 158 | ```sql 159 | select * from {name of the table} where name like “P%”; 160 | ``` 161 | Filters data where `name` starts with “P”. 162 | 163 | 23. **REGEXP Operator:** 164 | ```sql 165 | select * from {name of the table} where name regexp “^P”; 166 | ``` 167 | Filters data where `name` starts with “P” using regular expressions. 168 | 169 | 24. **NULL Values:** 170 | ```sql 171 | select * from {name of the table} where name is null; 172 | ``` 173 | Filters data where the `name` field is `null`. 174 | 175 | 25. **LIMIT Results:** 176 | ```sql 177 | select * from {name of the table} limit 5; 178 | ``` 179 | Limits the number of results to 5. 180 | 181 | 26. **OFFSET Results:** 182 | ```sql 183 | select * from {name of the table} limit 5 offset 2; 184 | ``` 185 | Skips the first 2 results and then limits the results to 5. 186 | 187 | ## Joining Tables 188 | 189 | 27. **Basic Join:** 190 | ```sql 191 | select * from table1 join table2 on table1.id = table2.id; 192 | ``` 193 | Joins two tables on the `id` column. 194 | 195 | 28. **Join Across Databases:** 196 | ```sql 197 | use database1; 198 | select * from database2.table2 join table1 on table1.id = table2.id; 199 | ``` 200 | Joins tables from two different databases. 201 | 202 | 29. **Self Join:** 203 | ```sql 204 | select * from table1 t1 join table1 t2 on t1.id = t2.id; 205 | ``` 206 | Joins a table with itself using aliases. 207 | 208 | 30. **Compound Join:** 209 | ```sql 210 | select * from table1 join table2 on table1.id = table2.id and table1.name = table2.name; 211 | ``` 212 | Joins tables on multiple columns. 213 | 214 | 31. **USING Clause:** 215 | ```sql 216 | select * from table1 join table2 using (id); 217 | ``` 218 | Joins tables using a common column. 219 | 220 | 32. **Natural Join:** 221 | ```sql 222 | select * from table1 natural join table2; 223 | ``` 224 | Automatically joins tables on columns with the same name. 225 | 226 | 33. **Cross Join:** 227 | ```sql 228 | select * from table1 cross join table2; 229 | ``` 230 | Produces a Cartesian product of both tables. 231 | 232 | 34. **UNION:** 233 | ```sql 234 | select * from table1 union select * from table2; 235 | ``` 236 | Combines the results of two queries. 237 | 238 | 35. **Create Table from Another Table:** 239 | ```sql 240 | create table new_table as select * from existing_table; 241 | ``` 242 | Creates a new table by copying an existing table. 243 | 244 | --- 245 | 246 | By following these commands, you can efficiently manage databases and tables using MySQL on a Linux terminal. 247 | 248 | --- 249 | ### Example of how to create an efficent ddatabase: 250 | **Here are the reuirements from which we have to built our model:** 251 | 252 | - Every person has a name and an address. A person is uniquely identified by their name 253 | - There are two groups of persons, staff and students. A person can be a student and staff at the same time 254 | - Every staff receives a salary, while every student pays a fee. 255 | - Among the staff, there is research and teaching staff. A staff can belong to both groups. 256 | - Among the teaching staff, there are lecturers and TAs. A TA works for several courses. (course does not need to be an entity, just add a courseID attribute to an entity TA) 257 | - Every student is either a graduate student or an undergraduate student. 258 | - A graduate student has a thesis title, on which he/she is working. 259 | - An undergrad is in the final year if he/she is in his/her 4th year. 260 | - Every final year student is working on a project. 261 | - Every final year student is supervised by a member of the teaching staff. 262 | 263 | **We will then build a appropriate ER Diagram based on the above requirements:** 264 | 265 | ![ERdiagram](img2.png) 266 | 267 | **In the given ER diagram:** 268 | 269 | - Entities (Tables) are represented by blue rectangular boxes. 270 | - Attributes (Columns) are shown as green circles, connected to their respective entities. 271 | - Relationships between entities are depicted using purple diamond shapes. 272 | - A single line between an entity and a relationship indicates partial participation, meaning not all instances of the entity must participate in that relationship. 273 | - A double line signifies total participation, meaning every instance of that entity must be involved in the relationship. 274 | 275 | **Let's make a Relational Model from the above diagram:** 276 | 277 | ```text 278 | Person(name, address) 279 | PK: name 280 | 281 | Employee(name, salary) 282 | PK: name 283 | FK: Employee(name) references Person(name) 284 | Research(name) 285 | PK: name 286 | FK: Research(name) references Employee(name) 287 | 288 | Teaching(name) 289 | PK: name 290 | FK: Teaching(name) references Employee(name) 291 | 292 | Lecturer(name) 293 | PK: name 294 | FK: Lecturer(name) references Teaching(name) 295 | 296 | Tutor(name, courseno) 297 | PK: name 298 | FK: Tutor(name) references Teaching(name) 299 | 300 | Student(name, fee) 301 | PK: name 302 | FK: Student(name) references Person(name) 303 | 304 | PostGrad(name, thesis) 305 | PK: name 306 | FK: PostGrad(name) references Student(name) 307 | 308 | UnderGrad(name, year) 309 | PK: name 310 | FK: UnderGrad(name) references Student(name) 311 | 312 | FinalYear(name, project) 313 | PK: name 314 | FK: FinalYear(name) references UnderGrad(name) 315 | Constraint: year = 4 316 | 317 | Supervisor(student_name, supervisor_name) 318 | PK: (student_name, supervisor_name) 319 | FK: Supervisor(student_name) references FinalYear(name) 320 | FK: Supervisor(supervisor_name) references Employee(name) 321 | 322 | ``` 323 | **Write the database code from the above Relational Model:** 324 | 325 | ```sql 326 | -- Create the database 327 | CREATE DATABASE UniversityDB; 328 | USE UniversityDB; 329 | 330 | -- Person Table 331 | CREATE TABLE Person ( 332 | name VARCHAR(100) PRIMARY KEY, 333 | address VARCHAR(255) NOT NULL 334 | ); 335 | 336 | -- Employee Table (Overlapping with Student) 337 | CREATE TABLE Employee ( 338 | name VARCHAR(100) PRIMARY KEY, 339 | salary DECIMAL(10,2) NOT NULL, 340 | FOREIGN KEY (name) REFERENCES Person(name) ON DELETE CASCADE ON UPDATE CASCADE 341 | ); 342 | 343 | -- Student Table (Overlapping with Employee) 344 | CREATE TABLE Student ( 345 | name VARCHAR(100) PRIMARY KEY, 346 | fee DECIMAL(10,2) NOT NULL, 347 | FOREIGN KEY (name) REFERENCES Person(name) ON DELETE CASCADE ON UPDATE CASCADE 348 | ); 349 | 350 | -- Teaching and Research (Disjoint Specialization of Employee) 351 | CREATE TABLE Teaching ( 352 | name VARCHAR(100) PRIMARY KEY, 353 | FOREIGN KEY (name) REFERENCES Employee(name) ON DELETE CASCADE ON UPDATE CASCADE 354 | ); 355 | 356 | CREATE TABLE Research ( 357 | name VARCHAR(100) PRIMARY KEY, 358 | FOREIGN KEY (name) REFERENCES Employee(name) ON DELETE CASCADE ON UPDATE CASCADE 359 | ); 360 | 361 | -- Lecturer and Tutor (Disjoint Specialization of Teaching) 362 | CREATE TABLE Lecturer ( 363 | name VARCHAR(100) PRIMARY KEY, 364 | FOREIGN KEY (name) REFERENCES Teaching(name) ON DELETE CASCADE ON UPDATE CASCADE 365 | ); 366 | 367 | CREATE TABLE Tutor ( 368 | name VARCHAR(100) PRIMARY KEY, 369 | courseno VARCHAR(50) NOT NULL, 370 | FOREIGN KEY (name) REFERENCES Teaching(name) ON DELETE CASCADE ON UPDATE CASCADE 371 | ); 372 | 373 | -- Undergraduate and Postgraduate (Disjoint Specialization of Student) 374 | CREATE TABLE UnderGrad ( 375 | name VARCHAR(100) PRIMARY KEY, 376 | year INT NOT NULL CHECK (year BETWEEN 1 AND 4), -- Year must be between 1 and 4 377 | FOREIGN KEY (name) REFERENCES Student(name) ON DELETE CASCADE ON UPDATE CASCADE 378 | ); 379 | 380 | CREATE TABLE PostGrad ( 381 | name VARCHAR(100) PRIMARY KEY, 382 | thesis VARCHAR(255) NOT NULL, 383 | FOREIGN KEY (name) REFERENCES Student(name) ON DELETE CASCADE ON UPDATE CASCADE 384 | ); 385 | 386 | -- Final Year (Now only stores students from UnderGrad with year = 4) 387 | CREATE TABLE FinalYear ( 388 | name VARCHAR(100) PRIMARY KEY, 389 | project VARCHAR(255) NOT NULL, 390 | FOREIGN KEY (name) REFERENCES UnderGrad(name) ON DELETE CASCADE ON UPDATE CASCADE 391 | ); 392 | 393 | -- Supervisor Relationship (m:n between Employee and FinalYear) 394 | CREATE TABLE Supervisor ( 395 | student_name VARCHAR(100), 396 | supervisor_name VARCHAR(100), 397 | PRIMARY KEY (student_name, supervisor_name), 398 | FOREIGN KEY (student_name) REFERENCES FinalYear(name) ON DELETE CASCADE ON UPDATE CASCADE, 399 | FOREIGN KEY (supervisor_name) REFERENCES Employee(name) ON DELETE CASCADE ON UPDATE CASCADE 400 | ); 401 | 402 | -- Stored Procedure to Auto-Insert into FinalYear if year = 4 403 | DELIMITER // 404 | CREATE TRIGGER InsertFinalYear 405 | AFTER INSERT ON UnderGrad 406 | FOR EACH ROW 407 | BEGIN 408 | IF NEW.year = 4 THEN 409 | INSERT INTO FinalYear (name, project) 410 | VALUES (NEW.name, 'Default Project'); 411 | END IF; 412 | END; 413 | // 414 | DELIMITER ; 415 | 416 | ``` 417 | 418 | Author: **Purva Patel** 419 | --------------------------------------------------------------------------------