Half of the SQL marks are for building and changing tables rather than querying them. This page works through one table from create to drop, so you see the effect of each statement on the one before it.
1CREATE TABLE, with constraints
1
Create a table student with a primary key rollno, a compulsory name, a stream defaulting to Science, decimal marks, an age that must be between 3 and 20, and an admission date.
3 marks
▶Show the answer
MySQL command line client
mysql> create table student (
-> rollno int primary key,
-> name varchar(25) not null,
-> stream varchar(12) default 'Science',
-> marks decimal(5,2),
-> age int check (age >= 3 and age <= 20),
-> admdate date
-> );
Query OK, 0 rows affected (0.39 sec)
0 rows affected is correct and expected — a create makes a structure, it does not put rows in it.
describe shows the primary key as PRI with Null: NO, the not null on name, and the default sitting in its own column. Notice that the check constraint does not appear here — it is enforced, but describe has nowhere to show it.
📝CHECK is not on the IP syllabus
Informatics Practices students are not asked for the check constraint. Computer Science students are. The rest of this page applies to both.
Look at row 2. stream was not supplied and came out as Science — the default did its job. But admdate has no default, so it came out NULL. A column with no default and no value gets NULL, not a blank and not a zero.
3The constraints doing their job
A constraint is only visible when it stops something. All three of these were refused:
ERROR 1062 (23000): Duplicate entry '1' for key 'student.PRIMARY'
Age 25 breaks the check; a NULL name breaks not null; and roll number 1 already exists, which breaks the primary key. In each case no row was added — the statement was rejected whole.
4UPDATE — and the WHERE you must not forget
2
Change the marks of roll number 2 to 90.00.
1 mark
▶Show the answer
MySQL command line client
mysql> update student set marks = 90.00 where rollno = 2;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Rows matched: 1 confirms the where picked out exactly one row. Reading that line is the quickest way to catch a mistake.
3
Increase the marks of every Science student by 2.
2 marks
▶Show the answer
MySQL command line client
mysql> update student set marks = marks + 2 where stream = 'Science';
Query OK, 2 rows affected (0.03 sec)
Rows matched: 2 Changed: 2 Warnings: 0
set marks = marks + 2 reads the old value and writes the new one, row by row.
MySQL command line client
mysql> select rollno, name, stream, marks from student;
+--------+-----------------+----------+-------+
| rollno | name | stream | marks |
+--------+-----------------+----------+-------+
| 1 | Aditi Rai | Science | 90.50 |
| 2 | Bikash Chettri | Science | 92.00 |
| 3 | Chhering Bhutia | Commerce | 91.00 |
| 4 | Divya Sharma | Arts | 67.75 |
+--------+-----------------+----------+-------+
4 rows in set (0.00 sec)
⚠️An UPDATE with no WHERE changes every row
update student set marks = marks + 2; would have added two to all four students. There is no confirmation prompt and no undo. Write the where first, then go back and write the set.
Compare the stream row with the very first describe on this page. The type widened from varchar(12) to varchar(20) as asked — but the Default column has changed from Science to NULL. modify replaces the whole column definition, so anything you do not restate is lost. To keep it, the statement has to say so: alter table student modify column stream varchar(20) default 'Science';
This was found by running the statements and reading the output, not from a textbook — most of them do not mention it.
7DROP TABLE
MySQL command line client
mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
mysql> drop table student;
Query OK, 0 rows affected (0.14 sec)
mysql> show tables;
Empty set (0.00 sec)
Three rows existed a moment before, and drop took the rows, the columns and the name together.
✏️ Quick Check
A column is declared with DEFAULT 'Science'. A row is inserted without naming that column. What is stored?
✏️ Quick Check
After 'alter table student modify column stream varchar(20);', what happens to the column's existing DEFAULT 'Science'?
✏️ Quick Check
Which statement removes all the rows but keeps the table structure?