File notes/lectures_notes.md changed (mode: 100644) (index 6f4fa83..aa3bffc) |
... |
... |
DESCRIBE <TableName>; -- Show the structure of a table |
994 |
994 |
SELECT * FROM <TableName> -- List all the rows in a table |
SELECT * FROM <TableName> -- List all the rows in a table |
995 |
995 |
DROP TABLE <TableName>; -- "Drop" (erase) a table |
DROP TABLE <TableName>; -- "Drop" (erase) a table |
996 |
996 |
DROP SCHEMA <SchemaName>; -- Drop a schema |
DROP SCHEMA <SchemaName>; -- Drop a schema |
|
997 |
|
SHOW WARNINGS; -- After a warning was issued, show the content of the warning. |
997 |
998 |
~~~ |
~~~ |
998 |
999 |
|
|
999 |
1000 |
<!-- |
<!-- |
|
... |
... |
CREATE TABLE HURRICANE( |
1019 |
1020 |
WindSpeed INT DEFAULT 76, |
WindSpeed INT DEFAULT 76, |
1020 |
1021 |
Above VARCHAR(25) |
Above VARCHAR(25) |
1021 |
1022 |
); |
); |
|
1023 |
|
-- WindSpeed INT CHECK (WindSpeed > 74 AND WindSpeed < 500), |
1022 |
1024 |
-- would be parsed but wouldn't have any effect! |
-- would be parsed but wouldn't have any effect! |
1023 |
1025 |
|
|
1024 |
1026 |
CREATE TABLE STATE( |
CREATE TABLE STATE( |
|
... |
... |
MariaDB [HW_CONSTRAINTS_PART1]> DESCRIBE STATE; |
1052 |
1053 |
~~~ |
~~~ |
1053 |
1054 |
|
|
1054 |
1055 |
~~~{.sqlmysql .numberLines} |
~~~{.sqlmysql .numberLines} |
|
1056 |
|
-- |
|
1057 |
|
-- Primary Keys |
|
1058 |
|
-- |
|
1059 |
|
|
1055 |
1060 |
-- Adding a primary key: |
-- Adding a primary key: |
1056 |
1061 |
ALTER TABLE STATE ADD PRIMARY KEY (Name); |
ALTER TABLE STATE ADD PRIMARY KEY (Name); |
1057 |
1062 |
|
|
1058 |
1063 |
-- Removing the primary key: |
-- Removing the primary key: |
1059 |
1064 |
ALTER TABLE STATE DROP PRIMARY KEY; |
ALTER TABLE STATE DROP PRIMARY KEY; |
1060 |
1065 |
|
|
|
1066 |
|
-- |
|
1067 |
|
-- UNIQUE Constraint |
|
1068 |
|
-- |
|
1069 |
|
|
1061 |
1070 |
-- Adding a UNIQUE constraint |
-- Adding a UNIQUE constraint |
1062 |
1071 |
ALTER TABLE STATE ADD UNIQUE (Postal_abbr); |
ALTER TABLE STATE ADD UNIQUE (Postal_abbr); |
1063 |
1072 |
|
|
1064 |
1073 |
-- Removing a UNIQUE constraint |
-- Removing a UNIQUE constraint |
1065 |
1074 |
ALTER TABLE STATE DROP INDEX Postal_abbr; |
ALTER TABLE STATE DROP INDEX Postal_abbr; |
1066 |
1075 |
|
|
|
1076 |
|
-- |
|
1077 |
|
-- NOT NULL Constraint |
|
1078 |
|
-- |
|
1079 |
|
|
1067 |
1080 |
-- Adding the NOT NULL constraint |
-- Adding the NOT NULL constraint |
1068 |
1081 |
ALTER TABLE STATE MODIFY Postal_abbr CHAR(2) NOT NULL; |
ALTER TABLE STATE MODIFY Postal_abbr CHAR(2) NOT NULL; |
1069 |
1082 |
|
|
1070 |
1083 |
-- Removing the NOT NULL constraint |
-- Removing the NOT NULL constraint |
1071 |
1084 |
ALTER TABLE STATE MODIFY Postal_abbr CHAR(2); |
ALTER TABLE STATE MODIFY Postal_abbr CHAR(2); |
1072 |
1085 |
|
|
|
1086 |
|
-- |
|
1087 |
|
-- Default value |
|
1088 |
|
-- |
|
1089 |
|
|
1073 |
1090 |
-- Changing the default value |
-- Changing the default value |
1074 |
1091 |
ALTER TABLE HURRICANE ALTER COLUMN WindSpeed SET DEFAULT 74; |
ALTER TABLE HURRICANE ALTER COLUMN WindSpeed SET DEFAULT 74; |
1075 |
1092 |
|
|
1076 |
1093 |
-- Removing the default value |
-- Removing the default value |
1077 |
1094 |
ALTER TABLE HURRICANE ALTER COLUMN WindSpeed DROP DEFAULT; |
ALTER TABLE HURRICANE ALTER COLUMN WindSpeed DROP DEFAULT; |
1078 |
1095 |
|
|
|
1096 |
|
-- |
|
1097 |
|
-- Foreign key |
|
1098 |
|
-- |
|
1099 |
|
|
1079 |
1100 |
--Adding a foreign key constraint |
--Adding a foreign key constraint |
1080 |
1101 |
ALTER TABLE HURRICANE ADD FOREIGN KEY (Above) REFERENCES STATE(Name); |
ALTER TABLE HURRICANE ADD FOREIGN KEY (Above) REFERENCES STATE(Name); |
|
1102 |
|
|
|
1103 |
|
-- Removing a foreign key constraint is out of the scope of this lecture. |
|
1104 |
|
-- If you are curious, you can have a look at https://www.w3schools.com/sql/sql_foreignkey.asp |
|
1105 |
|
-- Dropping a foreign key constraint requires your constraint to have a name, something we did not introduce. |
1081 |
1106 |
~~~ |
~~~ |
1082 |
1107 |
|
|
1083 |
1108 |
- `NOT NULL`{.sqlmysql} is to some extend part of the datatype. |
- `NOT NULL`{.sqlmysql} is to some extend part of the datatype. |