Basic operations of using the command line to create a table in mySql

One: Open the command line to start the mysql service

注意事项:应该使用管理员身份打开命令行键入命令"net start mysql"(鼠标右键使用管理员身份打开),否则会出现拒绝访问报错,如下:

insert image description here

After successful startup, as shown in the figure:
insert image description here

When starting as administrator, the directory is "C:\Windows\system32>"

Two: log in to the database

The login command is "mysql -u root -p" and then enter the correct password to enter. If it is installed for the first time, just press Enter to enter.

As shown in the figure:

insert image description here

Three: create a database

1. Type "create datebase library name" to create a database. Don't forget the semicolon at the end of the statement. If you forget the semicolon, just type the semicolon on the next line.

As shown in the figure:

insert image description here

2. After successful creation, type "show databases;" to view all databases, pay attention to databases instead of databases!!

As shown in the figure:
insert image description here

Sometimes in order to prevent garbled characters in the database, set the character set when creating the database, type the command "create datebase ii character set gbk;"

as the picture shows:

insert image description here

3. To use the specified database, type "use ii;"

The effect is as shown in the figure:
insert image description here

4. View the currently used database, type "select database();"

The effect is as shown in the figure:

insert image description here

5, add a table in the current database, type

copy code

create table 表名(
//列名 字符格式 约束
//注意末尾加逗号,最后一列末尾除外
uid int(32) primary,
uname varchar(32) ,
upassword varchar(32)

);

After copying the code
, the effect after building the table is as follows:

insert image description here

6. View the table structure

//desc table name

desc  user;

As shown in the figure:

insert image description here

7. Add table columns

//alter table table name add field character format constraints

 alter table user add unifo varchar(32) not null;

8. Modify table columns

alter table 表名 modify 列名 字符格式 约束

9. Modify the table name

alter table 表名  change 旧列名 新列名 新字符格式 新约束

10. Rename

rename table 旧表名 to 新表名

Guess you like

Origin blog.csdn.net/weixin_45932157/article/details/123583623