Informatics Practices
What is the purpose of ALTER TABLE command ? Can you add new columns with constraints such as NOT NULL ? Give example to justify your answer.
SQL Queries
2 Likes
Answer
The ALTER TABLE
command is used to change definitions of existing tables. It is used to add columns, integrity constraints and redefine a column (datatype, size, default value) in a table.
Yes, we can add new columns with constraints such as NOT NULL, which ensures that a column must always contain a value (i.e., cannot be empty or null).
For example, to add a new column tel_number
with the NOT NULL constraint in the Empl
table, the statement is:
ALTER TABLE Empl
ADD COLUMN(tel_number integer NOT NULL);
Answered By
1 Like
Related Questions
Mr. Mittal is using a table with following columns :
Name, Class, Stream_Id, Stream_name
He needs to display names of students who have not been assigned any stream or have been assigned stream_name that ends with "computers".
He wrote the following command, which did not give the desired result.
SELECT Name, Class FROM Students WHERE Stream_name = NULL OR Stream_name = "%computers" ;
Help Mr. Mittal to run the query by removing the error and write correct query.
The Doc_name Column of a table Hospital is given below :
Doc_name Avinash Hariharan Vinayak Deepak Sanjeev Based on the information, find the output of the following queries :
(i) SELECT docname FROM HOSPITAL WHERE Docname like "%v";
(ii) SELECT docname FROM HOSPITAL WHERE docname like "%e%";
Sarthak, a student of class XII, created a table "Class". Grade is one of the columns of this table. To find the details of students whose Grades have not been entered, he wrote the following MySql query, which did not give the desired result:
SELECT * FROM Class WHERE Grade = "Null";
Help Sarthak to run the query by removing the errors from the query and write the correct query.
What is the purpose of DROP TABLE command in MySql ? How is it different from DELETE command ?