Hi Devs, Today we are going to see what exactly is SQL views and why you should use them. SQL views are the virtual tables created by you when you define a command to create a view. You can do all normal operation such as create,update,delete on views. Views can be very useful if used correctly.
Lets see How to Create a View
// To create a view. mysqli_query($conn, "CREATE VIEW userCustomList AS SELECT first_name as name, age as user_age FROM users");
Here we are creating a view which will hold data “name” and “user_age” only from the main table.
Update a SQL View Selection
Incase you want to update a view
// Updating View Selection mysqli_query($conn, "CREATE OR REPLACE VIEW userCustomList AS SELECT first_name as name, age as user_age, salary FROM users WHERE salary > 90000");
Here we are also getting “salary” and we are getting only those users where salary more than 90000.
Update a Record In View
// Simple query to update view mysqli_query($conn, "UPDATE userCustomList SET name = 'Something'");
Note : Update and delete will also take effect in original table as well, So be very careful. And take backup of your data.
Deleting A View
// This will delete the view. mysqli_query($conn, "DROP VIEW userCustomList");
Incase you want to delete data from view
Delete Data From View
// Deleting data from view mysqli_query($conn, "DELETE FROM userCustomList");
Note : Update and delete will also take effect in original table as well, So be very careful. And take backup of your data.