Php Unit Iiimb
Php Unit Iiimb
MySQL Database
Commands
which returns:
+----------+
| Database |
+----------+
| mysql |
+----------+
1 rows in set (0.00 sec)
This returns:
Database changed
For example:
This creates the user michele who can access anything locally. To change to
the michele user, at the mysql command prompt, type:
exit
Then start MySQL from the command line with the new username and
password.The syntax for specifying the username and password when
starting MySQL is:
It is important to back up your databases so that you can recover your data
and be up and running again in case problems occur, such as system crashes,
hardware failures, or users deleting data by mistake.
Physical backups consist of raw copies of the directories and files that store
database contents. This type of backup is suitable for large, important
databases that need to be recovered quickly when problems occur.
Most of the time, you’ll just want to back up everything in the database. To do
this,use the --all-databases command-line switch. The resulting database backup
file contains the commands necessary to create the databases and users, making a
complete database restore a snap.
Ex:
To create an empty copy of your database—just the structure—for testing, use the
--no-data switch:
You can also do the opposite and just back up the data with the --no-create-info
switch:
If you did a selective backup of only one database, it’s a bit more complex. To
This tells mysqldump to generate separate files for each table in the store
database.
They’ll all be placed in the directory /home/jon. Each file’s name will be the
name of the table that is being exported. Each file contains the records in the
respective table separated by the comma character (,) that was specified on the
command line.
Here is an easy way to write MySQL query results into a .txt or .CSV files
through the select statement.
101 aaa
102 bbb
Advanced SQL
Indexes
The data in an index is sorted and organized to make finding a specific value as quick as possible.
Because the values are sorted.
UNIQUE INDEX in MySQL is used to ensure that the values in one or multiple
columns are unique across all rows (or records) in a table. It prevents any duplicate
entry into the table. It can be useful in situations where uniqueness has to be
ensured but it doesn't necessarily need to be a primary key.
In other words, when we create a UNIQUE INDEX on one or more columns in a table,
MySQL guarantees that the values in those columns will be unique for every row in the table.
The UNIQUE INDEX can be created at the time of table creation or it can be added later using
the ALTER TABLE statement. It can be declared on one or multiple columns, and it can also
include null values in the indexed columns.
As the uniqueness is enforced on one or more column(s) in a table, it is ensured that these
columns contain unique values only, but it can also be a NULL value though.
The use of UNIQUE INDEX in MySQL also improves efficiency by making data retrieval
faster, helps it retrieve the required data quickly concerning the search criteria that we mention
in the WHERE clause.
The focus of both PRIMARY KEY as well as UNIQUE INDEX in MySQL is to make sure
that a column must have a unique value in each of its rows.
But there are a few major differences between the two that should be highlighted. A
PRIMARY KEY is a special unique index that can be assigned to only one column/field of a
table. UNIQUE INDEX, on the other hand, isn't limited to having only one column in a table.
It can be applied to multiple columns. PRIMARY KEY doesn't allow the insertion of any null
value in a column. However, we can have null values in the case of a UNIQUE INDEX.
Previously, the allowance of null values was considered a bug, but that's not the case here.
As already mentioned, there can only be one primary key but multiple unique indexes within a
table.
A primary key prohibits the insertion of NULL values but a unique index doesn't.
Most importantly, a primary key is meant to enforce the referential integrity between the tables
and this makes sure that each of the columns in a table can be uniquely identified. Contrarily, a
unique index is used to ensure the uniqueness within a table.
https://www.scaler.com/topics/unique-index-in-mysql/
Note: Updating a table with indexes takes more time than updating a table
without (because the indexes also need an update). So, only create indexes on
columns that will be frequently searched against.
Multicolumn indexes
It’s also possible to create MySQL indexes that use more than one column. A
multicolumn unique index ensures that the combination of column values is
unique.
The best columns to index are those that are likely to be used in the WHERE
clause, especially if you know that certain combinations of keys will be used.
Those are good columns to add to a multicolumn index. Order the columns in a
multicolumn index so that columns used frequently come first. MySQL uses a
multicolumn index to speed up a query even if only the first value of the index is
used.
Primary indexes are also unique. Only one primary index is allowed per table.
However, you can have as many unique indexes as your heart desires.
We’re going to do a query with a specific WHERE clause, and then use
EXPLAIN to get details about how it was processed by MySQL:
SELECT * FROM authors WHERE author = 'Arnold Robbins';
This returns the following:
+-----------+----------+----------------+
| author_id | title_id | author |
+-----------+----------+----------------+
| 3 | 2 | Arnold Robbins |
+-----------+----------+----------------+
EXPLAIN, in turn, gives you this output (which has wrapped a little):
+----+-------------+---------+------+---------------+------+---------+------+---
After creating a unique index on authors called authind using the syntax from
Example , rerun the EXPLAIN query:
-+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+------
Because the results are grouped by title, it’s possible to count the number of
authors for each title.
Concatenation.
Just like the process of putting strings together with the PHP dot operator (.),
which is a period, MySQL can paste together strings fromdata fields with the
CONCAT function.
For example, if you want to return a single field that combines the title with
the number of pages, you could use CONCAT.