Basic Mysql Queries
Basic Mysql Queries
Elements of a Database:
Classifications:
Commands:
● mysql -p ←to open mysql
● exit← type twice to exit the app
● show databases; ← to show ur db
● use dbname; ← to activate ur database file
● describe tbname; ← shows table structure
● create table tbname (fldname type(#), fldname type(#)); ← creates
table w/ fieldnames/columns
● create database [IF NOT EXISTS] dbname; ← prevents you from creating a
new database that already exists
● insert into tbname (fldname, fldname) values (’val’,’val’); ← to
insert a value under a column
Select
● select * from tbname; ← displays all records in ur table, * is the short hand for
selecting all the columns
editing
→ modifying record
Primary Key - info only exclusive to 1 user (ex: email, username, etc)
● alter table tbname add primary key (fdname); ← to add a primary key to a
field/column
torino567comporg23
passtorino567comporg23
if ($num<1) { echo "No record found."; } else { echo "<table border='1'>"; echo "<tr>"; echo
"<th>Student ID</th> <th>Family Name</th><th>Given Name</th> <th>Middle Name
</th><th>Gender</th> <th>Age</th> </tr>";
while ($row=mysqli_fetch_array($query))
{
$a=$row['stud_id'];
$b=$row['fname'];
$c=$row['gname'];
$d=$row['mname'];
$e=$row['gender'];
$f=$row['age'];
echo "<tr>";
echo "<td>$a</td>";
echo "<td>$b</td>";
echo "<td>$c</td>";
echo "<td>$d</td>";
echo "<td>$e</td>";
echo "<td>$f</td>";
echo "</tr>";
}
echo "</table>";
echo "<p>There are $num records.</p>";
} ?>
Data Types:
int - integer
float - # w/ decimal
text - can hold 250 letters & u can set the max of letters it can hold but it will save for the max #
of char it has (ex: fname text(30) - “Barde” ← will save 30)
varchar - like text but will save the number of characters in the word (ex: fname text(30) -
“Barde” ← will save 5)
u can press the up arrow & it will put in ur previous command. you can also retype ; if you forgot
to in ur previous command.
sql keywords are not case sensitive, so select will be the same as SELECT