0% found this document useful (0 votes)
3 views

MYSQL BASIC COMMANDS

The document provides a comprehensive guide on MySQL commands for database management, including creating, deleting, and showing databases, as well as creating and manipulating tables. It covers syntax and examples for various operations such as creating tables, inserting records, updating records, and deleting tables. Additionally, it explains the use of specific commands like RENAME TABLE and sp_rename in SQL Server, along with considerations for dependencies and permissions.

Uploaded by

harshithajanga12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

MYSQL BASIC COMMANDS

The document provides a comprehensive guide on MySQL commands for database management, including creating, deleting, and showing databases, as well as creating and manipulating tables. It covers syntax and examples for various operations such as creating tables, inserting records, updating records, and deleting tables. Additionally, it explains the use of specific commands like RENAME TABLE and sp_rename in SQL Server, along with considerations for dependencies and permissions.

Uploaded by

harshithajanga12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

MySQL

1. Create Database
Syntax:

CREATE DATABASE database_name;

Description: Creates a new database. Example:

CREATE DATABASE mydatabase;

Output: Database created successfully.


2. Delete Database
Syntax:

DROP DATABASE database_name;

Description: Deletes an existing database. Example:

DROP DATABASE mydatabase;

Output: Database deleted successfully.


3. Show Databases
Syntax:

SHOW DATABASES;

Description: Lists all databases. Example:

SHOW DATABASES;

Output: List of databases.


4. Use Database
Syntax:

USE database_name;

Description: Selects a database to use. Example:

USE mydatabase;

Output: Database changed.


To display all tables in a MySQL database, you can use the following command:
Syntax:
SHOW TABLES;

Description: This command lists all the tables in the currently selected database.
Example:

USE mydatabase;
SHOW TABLES;

Output:

+----------------+
| Tables_in_mydatabase |
+----------------+
| users |
| orders |
| products |
+----------------+

This will show a list of all tables in the mydatabase database


Create Table in MySQL
Description: The CREATE TABLE statement is used to create a new table in a database. It defines the
table's structure, including columns, data types, and constraints.
Syntax:

CREATE TABLE table_name (


column1 datatype constraint COMMENT 'comment',
column2 datatype constraint COMMENT 'comment',
...
);

Attributes:
 column1, column2, ...: Names of the columns.
 datatype: Data type for the column (e.g., INT, VARCHAR, DATE).
 constraint: Constraints for the column (e.g., PRIMARY KEY, NOT NULL, UNIQUE).
 COMMENT: Optional comment for the column.
Example: Creating a students table with various data types and constraints:

CREATE TABLE students (


student_id INT AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique student identifier',
first_name VARCHAR(50) NOT NULL COMMENT 'First name of the student',
last_name VARCHAR(50) NOT NULL COMMENT 'Last name of the student',
birth_date DATE COMMENT 'Birth date of the student',
email VARCHAR(100) UNIQUE COMMENT 'Email address of the student',
phone_number VARCHAR(15) COMMENT 'Phone number of the student',
enrollment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'Date of enrollment',
gpa DECIMAL(3, 2) CHECK (gpa >= 0.00 AND gpa <= 4.00) COMMENT 'Grade Point Average',
is_active BOOLEAN DEFAULT TRUE COMMENT 'Active status of the student'
);
Output:

Query OK, 0 rows affected (0.02 sec)

Explanation of the example:


 student_id: An integer that auto-increments and serves as the primary key.
 first_name and last_name: Strings that cannot be null.
 birth_date: A date field.
 email: A string that must be unique.
 phone_number: A string for the phone number.
 enrollment_date: A timestamp that defaults to the current timestamp.
 gpa: A decimal value with a constraint to ensure it is between 0.00 and 4.00.
 is_active: A boolean that defaults to true.
This example covers a variety of data types and constraints, providing a comprehensive structure for
a students table.
Rename Table in MySQL
Description: The RENAME TABLE statement is used to change the name of an existing table in the
database.
Syntax:

RENAME TABLE old_table_name TO new_table_name;

Example:

RENAME TABLE students TO learners;

Output:

Query OK, 0 rows affected (0.01 sec)

Possible Conditions:
1. Single Table Rename:

2. RENAME TABLE old_table_name TO new_table_name;

Renames one table to a new name.


3. Multiple Table Rename:

4. RENAME TABLE old_table_name1 TO new_table_name1, old_table_name2 TO


new_table_name2;

Renames multiple tables in a single statement.


5. Table with Constraints:
 Ensure that any constraints (like foreign keys) are updated to reflect the new table name
if necessary.
6. Table in Use:
 Ensure the table is not being accessed by other operations during the rename to avoid
conflicts.
7. Permissions:
 The user must have the necessary privileges to rename the table.
Example with Multiple Tables:

RENAME TABLE students TO learners, courses TO subjects;

Output:

Query OK, 0 rows affected (0.02 sec)

This command will rename the students table to learners and the courses table to subjects in a
single operation.
Rename Table Using sp_rename in SQL Server
Description: The sp_rename stored procedure is used to rename user-created objects in the current
database, such as tables, columns, indexes, and other objects.
Syntax:

EXEC sp_rename 'old_name', 'new_name', 'object_type';

 old_name: The current name of the object.


 new_name: The new name for the object.
 object_type: The type of object being renamed (optional). Common values include 'OBJECT',
'COLUMN', 'INDEX'.
Example: Renaming a Table

EXEC sp_rename 'dbo.students', 'learners';

Output:

Caution: Changing any part of an object name could break scripts and stored procedures.

This message indicates that renaming an object might affect scripts and stored procedures that
reference the old name.
Example: Renaming a Column

EXEC sp_rename 'dbo.students.first_name', 'first', 'COLUMN';

Output:

Caution: Changing any part of an object name could break scripts and stored procedures.
Possible Conditions:
1. Renaming a Table:

2. EXEC sp_rename 'old_table_name', 'new_table_name';

Renames a table.

3. Renaming a Column:

4. EXEC sp_rename 'table_name.old_column_name', 'new_column_name', 'COLUMN';

Renames a column within a table.



5. Renaming an Index:

6. EXEC sp_rename 'table_name.old_index_name', 'new_index_name', 'INDEX';

 Renames an index within a table.


7. Renaming Other Objects:
 The object_type parameter can be used to specify other types of objects, such as
'OBJECT' for general objects.
Important Considerations:
 Dependencies: Renaming objects can break dependencies in scripts, stored procedures, and
views that reference the old name.
 Permissions: Ensure you have the necessary permissions to rename the object.
 Schema: If the object is schema-bound, ensure the schema is correctly referenced.
Using sp_rename is a powerful way to manage object names in SQL Server, but it should be used with
caution to avoid breaking dependencies.
Delete Table in MySQL
Description: The DROP TABLE statement is used to delete an entire table from the database, including
all of its data and structure.
Syntax:

DROP TABLE table_name;

Example:

DROP TABLE students;

Output:

Query OK, 0 rows affected (0.01 sec)

This command will completely remove the students table from the database.
Delete Table Records in MySQL
Description: The DELETE statement is used to delete specific records from a table based on a
condition.
Syntax:

DELETE FROM table_name WHERE condition;

Example:

DELETE FROM students WHERE student_id = 1;

Output:

Query OK, 1 row affected (0.00 sec)

This command will delete the record from the students table where the student_id is 1.
Example with Sample Values
Let's create a students table, insert some sample values, and then demonstrate deleting records and
the table itself.
Create Table:

CREATE TABLE students (


student_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
birth_date DATE,
email VARCHAR(100)
);

Insert Sample Values:

INSERT INTO students (first_name, last_name, birth_date, email) VALUES


('John', 'Doe', '2000-01-01', '[email protected]'),
('Jane', 'Smith', '1999-05-15', '[email protected]'),
('Alice', 'Johnson', '2001-07-22', '[email protected]');

Output Table:

+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+

Delete a Specific Record:

DELETE FROM students WHERE student_id = 2;


Output:

Query OK, 1 row affected (0.00 sec)

Updated Table:

+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+

Delete the Entire Table:

DROP TABLE students;

Output:

Query OK, 0 rows affected (0.01 sec)

This will remove the students table and all its data from the database.
Creating a New Table from Existing Data Types in MySQL
Description: The CREATE TABLE ... AS SELECT statement is used to create a new table based on the
structure and data of an existing table. This can be done in several ways, depending on whether you
want to copy the structure only, the structure and data, or specific columns.
Syntax:

CREATE TABLE new_table AS SELECT * FROM existing_table;

Example:

CREATE TABLE new_students AS SELECT * FROM students;

Output:

Query OK, 3 rows affected (0.01 sec)


Records: 3 Duplicates: 0 Warnings: 0

This command creates a new table new_students with the same structure and data as
the students table.
Possible Ways to Create a New Table from Existing Data Types
1. Copying Structure and Data:

2. CREATE TABLE new_table AS SELECT * FROM existing_table;

 Copies both the structure and data from the existing table.
3. Copying Structure Only:

4. CREATE TABLE new_table AS SELECT * FROM existing_table WHERE 1=0;

 Copies only the structure without any data.


 Example:

 CREATE TABLE new_students AS SELECT * FROM students WHERE 1=0;

5. Copying Specific Columns:

6. CREATE TABLE new_table AS SELECT column1, column2 FROM existing_table;

 Copies only the specified columns.


 Example:

 CREATE TABLE new_students AS SELECT first_name, last_name FROM students;

7. Copying with Conditions:

8. CREATE TABLE new_table AS SELECT * FROM existing_table WHERE condition;

 Copies data that meets specific conditions.


 Example:

 CREATE TABLE new_students AS SELECT * FROM students WHERE birth_date > '2000-
01-01';

Example with Sample Values


Original students Table:

CREATE TABLE students (


student_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
birth_date DATE,
email VARCHAR(100)
);

INSERT INTO students (first_name, last_name, birth_date, email) VALUES


('John', 'Doe', '2000-01-01', '[email protected]'),
('Jane', 'Smith', '1999-05-15', '[email protected]'),
('Alice', 'Johnson', '2001-07-22', '[email protected]');

Creating a New Table with Structure and Data:

CREATE TABLE new_students AS SELECT * FROM students;


Output:

Query OK, 3 rows affected (0.01 sec)


Records: 3 Duplicates: 0 Warnings: 0

New new_students Table:

+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+

This example demonstrates how to create a new table from an existing table's structure and data.
INSERT INTO in MySQL
Description: The INSERT INTO statement is used to add new records to a table. It can insert data into
all columns or specific columns of a table.
Syntax and Examples
1. Inserting into All Columns: Syntax:

2. INSERT INTO table_name VALUES (value1, value2, ...);

Example:

INSERT INTO students VALUES (1, 'John', 'Doe', '2000-01-01',


'[email protected]');

3. Inserting into Specific Columns: Syntax:

4. INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

Example:

INSERT INTO students (first_name, last_name, birth_date, email) VALUES ('Jane',


'Smith', '1999-05-15', '[email protected]');

5. Inserting Multiple Rows: Syntax:

6. INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...),
(value1, value2, ...), ...;
Example:

INSERT INTO students (first_name, last_name, birth_date, email) VALUES


('Alice', 'Johnson', '2001-07-22', '[email protected]'),
('Bob', 'Brown', '2002-03-10', '[email protected]');

7. Inserting Data from Another Table: Syntax:

8. INSERT INTO table_name (column1, column2, ...) SELECT column1, column2, ... FROM
another_table WHERE condition;

Example:

INSERT INTO students (first_name, last_name, birth_date, email)


SELECT first_name, last_name, birth_date, email FROM new_students WHERE birth_date
> '2000-01-01';

Example with students Table


Create Table:

CREATE TABLE students (


student_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
birth_date DATE,
email VARCHAR(100)
);

Insert Sample Values:

INSERT INTO students (first_name, last_name, birth_date, email) VALUES


('John', 'Doe', '2000-01-01', '[email protected]'),
('Jane', 'Smith', '1999-05-15', '[email protected]');

Output Table:

+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
+------------+------------+-----------+------------+----------------------+

Insert Multiple Rows:

INSERT INTO students (first_name, last_name, birth_date, email) VALUES


('Alice', 'Johnson', '2001-07-22', '[email protected]'),
('Bob', 'Brown', '2002-03-10', '[email protected]');
Output Table:

+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
| 4 | Bob | Brown | 2002-03-10 | [email protected] |
+------------+------------+-----------+------------+----------------------+

These examples demonstrate various ways to use the INSERT INTO statement to add data to
the students table.
Update Records in MySQL
Description: The UPDATE statement is used to modify existing records in a table. You can update one
or multiple columns and rows based on a specified condition.
Syntax and Examples
1. Updating Specific Columns: Syntax:

2. UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

Example:

UPDATE students SET email = '[email protected]' WHERE student_id = 1;

3. Updating Multiple Columns: Syntax:

4. UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

Example:

UPDATE students SET first_name = 'Jonathan', email = '[email protected]'


WHERE student_id = 1;

5. Updating Multiple Rows: Syntax:

6. UPDATE table_name SET column1 = value1 WHERE condition;

Example:

UPDATE students SET email = '[email protected]' WHERE birth_date < '2000-01-01';

7. Updating All Rows: Syntax:

8. UPDATE table_name SET column1 = value1, column2 = value2, ...;


Example:

UPDATE students SET is_active = FALSE;

Example with students Table


Original students Table:

CREATE TABLE students (


student_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
birth_date DATE,
email VARCHAR(100),
is_active BOOLEAN DEFAULT TRUE
);

INSERT INTO students (first_name, last_name, birth_date, email) VALUES


('John', 'Doe', '2000-01-01', '[email protected]'),
('Jane', 'Smith', '1999-05-15', '[email protected]'),
('Alice', 'Johnson', '2001-07-22', '[email protected]');

Original Table Data:

+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+

Updating a Specific Column:

UPDATE students SET email = '[email protected]' WHERE student_id = 1;

Output:

Query OK, 1 row affected (0.00 sec)

Updated Table:

+------------+------------+-----------+------------+-------------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+-------------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected] |
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+-------------------------+
Updating Multiple Columns:

UPDATE students SET first_name = 'Jonathan', email = '[email protected]' WHERE


student_id = 1;

Output:

Query OK, 1 row affected (0.00 sec)

Updated Table:

+------------+------------+-----------+------------+-------------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+-------------------------+
| 1 | Jonathan | Doe | 2000-01-01 | [email protected]|
| 2 | Jane | Smith | 1999-05-15 | [email protected] |
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+-------------------------+

Updating Multiple Rows:

UPDATE students SET email = '[email protected]' WHERE birth_date < '2000-01-01';

Output:

Query OK, 1 row affected (0.00 sec)

Updated Table:

+------------+------------+-----------+------------+-------------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+-------------------------+
| 1 | Jonathan | Doe | 2000-01-01 | [email protected]|
| 2 | Jane | Smith | 1999-05-15 | [email protected] |
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+-------------------------+

Updating All Rows:

UPDATE students SET is_active = FALSE;

Output:

Query OK, 3 rows affected (0.00 sec)


Updated Table:

+------------+------------+-----------+------------+-------------------------+-----------
+
| student_id | first_name | last_name | birth_date | email | is_active
|
+------------+------------+-----------+------------+-------------------------+-----------
+
| 1 | Jonathan | Doe | 2000-01-01 | [email protected]| 0
|
| 2 | Jane | Smith | 1999-05-15 | [email protected] | 0
|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]| 0
|
+------------+------------+-----------+------------+-------------------------+-----------
+

These examples demonstrate various ways to use the UPDATE statement to modify data in
the students table.
Alter Table in MySQL
Description: The ALTER TABLE statement is used to modify the structure of an existing table. This can
include adding, deleting, or modifying columns, as well as adding or dropping constraints.
Syntax and Examples
1. Adding a Column: Syntax:

2. ALTER TABLE table_name ADD column_name datatype;

Example:

ALTER TABLE students ADD age INT;

3. Dropping a Column: Syntax:

4. ALTER TABLE table_name DROP COLUMN column_name;

Example:

ALTER TABLE students DROP COLUMN age;

5. Modifying a Column: Syntax:

6. ALTER TABLE table_name MODIFY COLUMN column_name new_datatype;

Example:

ALTER TABLE students MODIFY COLUMN email VARCHAR(150);


7. Renaming a Column: Syntax:

8. ALTER TABLE table_name CHANGE old_column_name new_column_name datatype;

Example:

ALTER TABLE students CHANGE COLUMN birth_date dob DATE;

9. Renaming the Table: Syntax:

10. ALTER TABLE old_table_name RENAME TO new_table_name;

Example:

ALTER TABLE students RENAME TO learners;

11. Adding a Constraint: Syntax:

12. ALTER TABLE table_name ADD CONSTRAINT constraint_name constraint_definition;

Example:

ALTER TABLE students ADD CONSTRAINT unique_email UNIQUE (email);

13. Dropping a Constraint: Syntax:

14. ALTER TABLE table_name DROP CONSTRAINT constraint_name;

Example:

ALTER TABLE students DROP CONSTRAINT unique_email;

Example with students Table


Original students Table:

CREATE TABLE students (


student_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
birth_date DATE,
email VARCHAR(100)
);

INSERT INTO students (first_name, last_name, birth_date, email) VALUES


('John', 'Doe', '2000-01-01', '[email protected]'),
('Jane', 'Smith', '1999-05-15', '[email protected]'),
('Alice', 'Johnson', '2001-07-22', '[email protected]');
Original Table Data:

+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+

Adding a Column:

ALTER TABLE students ADD age INT;

Output:

Query OK, 0 rows affected (0.01 sec)

Updated Table Structure:

+------------+------------+-----------+------------+----------------------+------+
| student_id | first_name | last_name | birth_date | email | age |
+------------+------------+-----------+------------+----------------------+------+
| 1 | John | Doe | 2000-01-01 | [email protected] | NULL |
| 2 | Jane | Smith | 1999-05-15 | [email protected]| NULL |
| 3 | Alice | Johnson | 2001-07-22 | [email protected]| NULL |
+------------+------------+-----------+------------+----------------------+------+

Dropping a Column:

ALTER TABLE students DROP COLUMN age;

Output:

Query OK, 0 rows affected (0.01 sec)

Updated Table Structure:

+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | birth_date | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+

Modifying a Column:

ALTER TABLE students MODIFY COLUMN email VARCHAR(150);


Output:

Query OK, 0 rows affected (0.01 sec)

Renaming a Column:

ALTER TABLE students CHANGE COLUMN birth_date dob DATE;

Output:

Query OK, 0 rows affected (0.01 sec)

Updated Table Structure:

+------------+------------+-----------+------------+----------------------+
| student_id | first_name | last_name | dob | email |
+------------+------------+-----------+------------+----------------------+
| 1 | John | Doe | 2000-01-01 | [email protected] |
| 2 | Jane | Smith | 1999-05-15 | [email protected]|
| 3 | Alice | Johnson | 2001-07-22 | [email protected]|
+------------+------------+-----------+------------+----------------------+

Renaming the Table:

ALTER TABLE students RENAME TO learners;

Output:

Query OK, 0 rows affected (0.01 sec)

Adding a Constraint:

ALTER TABLE learners ADD CONSTRAINT unique_email UNIQUE (email);

Output:

Query OK, 0 rows affected (0.01 sec)

Dropping a Constraint:

ALTER TABLE learners DROP CONSTRAINT unique_email;

Output:

Query OK, 0 rows affected (0.01 sec)


These examples demonstrate various ways to use the ALTER TABLE statement to modify the structure
of the students table.
In MySQL, the DESCRIBE or SHOW COLUMNS commands are typically used to get information about a
table's structure. However, in SQL Server, you can use the sp_help stored procedure to achieve a
similar result.
Describe Table in MySQL
Description: The DESCRIBE statement provides information about the columns in a table, including
column names, data types, and constraints.
Syntax:

DESCRIBE table_name;

Example:

DESCRIBE students;

Output:

+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| student_id | int | NO | PRI | NULL | auto_increment |
| first_name | varchar(50) | YES | | NULL | |
| last_name | varchar(50) | YES | | NULL | |
| birth_date | date | YES | | NULL | |
| email | varchar(100)| YES | UNI | NULL | |
+-------------+-------------+------+-----+---------+----------------+

Describe Table in SQL Server Using sp_help


Description: The sp_help stored procedure provides detailed information about a table, including
column names, data types, constraints, and more.
Syntax:

EXEC sp_help 'table_name';

Example:

EXEC sp_help 'students';

Output:

Name Owner Type Created_datetime


------------------ ------ ---------- -------------------
students dbo user table 2023-02-13 00:00:00

Column_name Type Computed Length Prec Scale Nullable TrimTrailingBlanks


FixedLenNullInSource Collation
------------------ ------------- -------- ------ ---- ----- -------- ------------------ -
------------------- ----------------------
student_id int no 4 10 0 no (n/a)
(n/a) SQL_Latin1_General_CP1_CI_AS
first_name varchar no 50 (n/a)(n/a) yes no
yes SQL_Latin1_General_CP1_CI_AS
last_name varchar no 50 (n/a)(n/a) yes no
yes SQL_Latin1_General_CP1_CI_AS
birth_date date no 3 (n/a)(n/a) yes (n/a)
(n/a) NULL
email varchar no 100 (n/a)(n/a) yes no
yes SQL_Latin1_General_CP1_CI_AS

Identity Seed Increment Not For Replication


--------- ---- --------- -------------------
No identity column defined.

RowGuidCol
----------
No rowguidcol column defined.

Data_located_on_filegroup
-------------------------
PRIMARY

This output provides comprehensive details about the students table, including column attributes,
constraints, and other metadata.

You might also like