MySQL study notes (1)-DDL statement

1 Introduction

DDL (Data Definition Languages) data definition language : used to define database objects such as data segments, databases, tables, columns, and indexes. Commonly used keywords are create, drop, alter, etc.

2. Preparation

Before performing a series of operations on MySQL, we must first start the MySQL service. Here we show two ways to start / close the MySQL service under the Windows platform .

2.1 Start the MySQL service

2.1.1 Start via "Service"

"Start"-> "Control Panel"-> "Administrative Tools"-> "Services", find the MySQL right-click properties, open the MySQL service properties window (as shown below), click " Start " in the figure to start the service, If we want to close, we can click " Stop " after starting
Insert picture description here

2.1.2 Start via command line window

Press win+ to X Aopen Windows PowerShell (administrator), Enter in the command line window:

net start mysql57	

“mysql57”It is the service name that I filled in by default during installation. Please refer to the content of "Service Name" in Figure 1.1.1

The results are as follows:
Insert picture description here
If you want to shut down the service, enter:

net stop mysql57

The results are as follows:
Insert picture description here

2.2 Connect to MySQL server

After starting the MySQL service, enter the following command in the command line window (only in user mode) and enter the password according to the prompt to connect to the MySQL server:

>mysql -uroot -p

display

Welcome to the MySQL monitor. . .

It means the connection is successful

3. Specific operation

3.1 Create a database

mysql>create database test1;	//test1为数据库名称

3.2 Select database

mysql>use test1;

3.2.1 Create Table

CREATE TABLE tablename (
column_name_1 column_type_1 constraints,
column_name_2 column_type_2 constraints,

column_name_n column_type_n constraints)

The table name of MySQL exists on the disk in the form of a directory, so the characters of the table name can be any characters allowed by the directory name. column_name is the name of the column; column_type is the data type of the column; constraints are the constraints of this column

mysql>create table emp(ename varchar(10), hiredate date, sal decimal(10,2), deptno int(2));

3.2.2 View Table

mysql>desc emp;

Insert picture description here

3.2.3 Modify the table

  1. Modify the table type, the syntax is as follows:

ALTER TABLE tablename MODIFY [COLUMN] column_definition [FIRST | AFTER col_name]

For example, to modify the definition of the ename field of the table emp, change varchar (10) to varchar (20):

mysql>alter table emp modify ename varchar(20);

Insert picture description here

  1. Add table fields, the syntax is as follows:

ALTER TABLE tablename ADD [COLUMN] column_definition [FIRST | AFTER col_name]

For example, a new field age is added to the table emp, and the type is int (3):

mysql>alter table emp add column age int(3);

Insert picture description here

  1. Delete the table field, the syntax is as follows:

ALTER TABLE tablename DROP [COLUMN] col_name

For example, delete the field age:

mysql>alter table emp drop column age;

Insert picture description here

  1. The field is renamed, the syntax is as follows:

ALTER TABLE tablename CHANGE [COLUMN] old_col_name column_definition
[FIRST|AFTER col_name]

For example, rename age to age1, and change the field type to int (4):

mysql>alter table emp change age age1 int(4);

Insert picture description here

  1. Modify the field order

For example, add the new field birth date after ename:

mysql>alter table emp add birth date after ename;

Insert picture description here
For example, modify the field age and put it first:

mysql>alter table emp modify age int(3) first;

Insert picture description here

Note: The keywords CHANGE / FIRST | AFTER COLUMN belong to MySQL's extension to standard SQL and may not be applicable to other databases.

  1. Change the table name, the syntax is as follows:

ALTER TABLE tablename RENAME [TO] new_tablename

For example, to rename the table emp to emp1, the command is as follows:

mysql>alter table emp rename emp1;

Insert picture description here

3.2.4 Delete table

The table delete command is as follows:

DROP TABLE tablename

mysql>drop table emp;

Insert picture description here

3.3 Viewing the database

mysql>show databases;

Insert picture description here

3.4 Delete database

mysql>drop database test1;

Insert picture description here

Published an original article · Likes2 · Visits 22

Guess you like

Origin blog.csdn.net/weixin_43587255/article/details/105450564