All you need to know to update sql table or database using SQL
How to add column in table?
Syntax:
ALTER TABLE table_name
ADD column_name dataType;
Let's see example:
Suppose you have a database named STD and there you have a table named student. Inside the table you have
columns like id, name, percentage,dob, age, gender, city, courses.
ALTER TABLE student
ADD grade varchar(255);
How to change data type of a column?
Syntax:
ALTER TABLE table_name
MODIFY column_name datatype;
Let's see example:
Suppose you have a database named STD and there you have a table named student. Inside the table you have
columns like id, name, percentage,dob, age, gender, city, courses.
ALTER TABLE student
MODIFY grade INT(10);
How to update value of a table?
Syntax:
update table_name set column_name=value where condition;
Example:
update books_store_1 set price=400 where books="Python"; What we did here, we set the price 400 of the python
book.
How to change or add constant of a column?
Syntax:
ALTER TABLE table_name
ADD constant_name (column_name);
Let's see example:
Suppose you have a database named STD and there you have a table named student. Inside the table you have
columns like id, name, percentage, dob, age, gender, city, courses.
ALTER TABLE student
ADD UNIQUE (name);
How to change column name?
Syntax:
ALTER TABLE table_name
CHANGE column_name new_column_name datatype;
Let's see example:
Suppose you have a database named STD and there you have a table named student. Inside the table you have
columns like id, name, percentage, dob, age, gender, city, courses.
ALTER TABLE student
CHANGE name Name_of_student varchar(255);
How to change column position?
Syntax:
ALTER TABLE table_name
MODIFY column_name
AFTER that_column_name_after_which_you_want_to_see;
Let's see example:
Suppose you have a database named STD and there you have a table named student. Inside the table you have
columns like id, name, percentage, dob, age, gender, city, courses.
ALTER TABLE student
ADD grade varchar(255)
AFTER percentage;
How to delete a column?
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name datatype;
Let's see example:
Suppose you have a database named STD and there you have a table named student. Inside the table you have
columns like id, name, percentage, dob, age, gender, city, courses.
ALTER TABLE student
DROP COLUMN Name_of_student;
How to rename table?
Syntax:
ALTER TABLE table_name
RENAME new_table_name;
Let's see example:
Suppose you have a database named STD and there you have a table named student. Inside the table you have
columns like id, name, percentage, dob, age, gender, city, courses.
ALTER TABLE student
RENAME Students_info;
How to add a column to an existing table?
Syntax:
ALTER TABLE table_name
ADD column_name datatype NOT NULL UNIQUE...etc constraint;
Example:
ALTER TABLE my_table
ADD Email varchar(255) NOT NULL;
How to delete a column from an existing table?
Syntax:
ALTER TABLE first_table
DROP COLUMN Email;
Example:
ALTER TABLE first_table
DROP COLUMN Email;
How to drop a table?
Syntax:
DROP TABLE Table_name;
Example:
DROP TABLE My_second_table;
How to drop or delete all the values present in a table?
Syntax:
DROP TABLE Table_name;
Example:
TRUNCATE TABLE My_second_table;
How to delete value of a column?
Syntax:
delete from table_name where condition;
Example:
delete from books_store_1 where price < 150;
What we did here, we delete those values which contain less than 150 in the price column.