KEYS
Example
Section titled “Example”CREATE TABLE Orders ( OrderID int, CustomerID int, OrderNumber int, PRIMARY KEY (OrderID));Output
Section titled “Output”Query OK, 0 rows affected (0.20 sec)Explanation
Section titled “Explanation”The CREATE TABLE statement creates a table named ‘Orders’ with three columns: OrderID, CustomerID, and OrderNumber. The PRIMARY KEY keyword denotes that the OrderID is the primary key of the Orders table. A primary key is a unique identifier for records in the table. It cannot contain NULL values and must contain unique values.
Example
Section titled “Example”CREATE TABLE Employees ( ID int NOT NULL, Name varchar(255) NOT NULL, BirthDate date, Position varchar(255), Salary decimal(10,2), PRIMARY KEY (ID));
INSERT INTO EmployeesVALUES (1, 'John Doe', '1980-10-01', 'Manager', 45000.00), (2, 'Jane Doe', '1982-05-09', 'Developer', 35000.00);
SELECT * FROM Employees;Output
Section titled “Output”| ID | Name | BirthDate | Position | Salary ||----|---------|------------|-----------|---------|| 1 |John Doe | 1980-10-01 | Manager | 45000.00|| 2 |Jane Doe | 1982-05-09 | Developer | 35000.00|Explanation
Section titled “Explanation”In the SQL Server example given, an ‘Employees’ table is created with five columns: ‘ID’ (integer), ‘Name’ (string), ‘BirthDate’ (date), ‘Position’ (string), and ‘Salary’ (decimal). A primary key is set on the ‘ID’ column, which means that each entry in the ‘Employees’ table must have a unique ‘ID’. Two records are then inserted into the ‘Employees’ table, and finally, a query selects all records from the ‘Employees’ table and returns them as output.
Example
Section titled “Example”CREATE TABLE Employees ( ID INT PRIMARY KEY, NAME VARCHAR (20), AGE INT, SALARY DECIMAL (10, 2));
INSERT INTO Employees (ID, NAME, AGE, SALARY)VALUES (1, "Mike", 45, 50000), (2, "Hannah", 35, 75000), (3, "Sam", 55, 97000);Output
Section titled “Output”Query OK, 0 rows affectedQuery OK, 3 rows affectedExplanation
Section titled “Explanation”The above SQL code is creating an Employees table with various columns: ID, NAME, AGE, and SALARY. The structure of the table includes a Primary Key ID to ensure there are no duplicate, null or conflicting entries. Then, three rows are inserted into this table. The Output demonstrates that the commands executed successfully without affecting any records (as this is a creation operation).