Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Learning mysql sql sentences (1)

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In order to learn the database, first familiarize yourself with the sql language, and take mysql as an example to start learning!

(reference book "mysql database development, optimization, management and maintenance")

The following is the executed sql statement, mainly to create and modify the table:

First go to the windows command prompt:

Microsoft Windows XP [version 5.1.2600]

(C) copyright 1985-2001 Microsoft Corp.

C:\ Documents and Settings\ Administrator > mysql-uroot-proot

Welcome to the MySQL monitor. Commands end with; or\ g.

Your MySQL connection id is 4

Server version: 5.1.41-community MySQL Community Server (GPL)

Type 'help;' or'\ h' for help. Type'\ c'to clear the current input statement.

Mysql > show databases

+-+

| | Database |

+-+

| | information_schema |

| | mysql |

| | test |

+-+

3 rows in set (0.02 sec)

Mysql > use test

Database changed

Mysql > create table emp (ename varchar (20) not null primary key,hirdate date,sal

Decimal (10Pol 2), deptno int (2))

Query OK, 0 rows affected (0.08 sec)

Mysql > desc emp

+-+ +

| | Field | Type | Null | Key | Default | Extra | |

+-+ +

| | ename | varchar (20) | NO | PRI | NULL |

| | hirdate | date | YES | | NULL |

| | sal | decimal (10jue 2) | YES | | NULL | |

| | deptno | int (2) | YES | | NULL |

+-+ +

4 rows in set (0.02 sec)

Mysql > show create table emp

+-

-+

| | Table | Create Table |

| |

+-

-+

| | emp | CREATE TABLE `emp` (

`ename` varchar (20) NOT NULL

`hirdate` date DEFAULT NULL

`sal` decimal (10jue 2) DEFAULT NULL

`deptno` int (2) DEFAULT NULL

PRIMARY KEY (`ename`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-

-+

1 row in set (0.00 sec)

Mysql > alter table emp modify ename varchar (30)

Query OK, 0 rows affected (0.19 sec)

Records: 0 Duplicates: 0 Warnings: 0

Mysql > desc emp

+-+ +

| | Field | Type | Null | Key | Default | Extra | |

+-+ +

| | ename | varchar (30) | NO | PRI | |

| | hirdate | date | YES | | NULL |

| | sal | decimal (10jue 2) | YES | | NULL | |

| | deptno | int (2) | YES | | NULL |

+-+ +

4 rows in set (0.00 sec)

Mysql > alter table emp add column age int (3)

Query OK, 0 rows affected (0.19 sec)

Records: 0 Duplicates: 0 Warnings: 0

Mysql > desc emp

+-+ +

| | Field | Type | Null | Key | Default | Extra | |

+-+ +

| | ename | varchar (30) | NO | PRI | |

| | hirdate | date | YES | | NULL |

| | sal | decimal (10jue 2) | YES | | NULL | |

| | deptno | int (2) | YES | | NULL |

| | age | int (3) | YES | | NULL |

+-+ +

5 rows in set (0.00 sec)

Mysql > alter table emp change age agenum int (4)

Query OK, 0 rows affected (0.05 sec)

Records: 0 Duplicates: 0 Warnings: 0

Mysql > desc emp

+-+ +

| | Field | Type | Null | Key | Default | Extra | |

+-+ +

| | ename | varchar (30) | NO | PRI | |

| | hirdate | date | YES | | NULL |

| | sal | decimal (10jue 2) | YES | | NULL | |

| | deptno | int (2) | YES | | NULL |

| | agenum | int (4) | YES | | NULL |

+-+ +

5 rows in set (0.00 sec)

Mysql > alter table emp drop agenum

Query OK, 0 rows affected (0.17 sec)

Records: 0 Duplicates: 0 Warnings: 0

Mysql > desc emp

+-+ +

| | Field | Type | Null | Key | Default | Extra | |

+-+ +

| | ename | varchar (30) | NO | PRI | |

| | hirdate | date | YES | | NULL |

| | sal | decimal (10jue 2) | YES | | NULL | |

| | deptno | int (2) | YES | | NULL |

+-+ +

4 rows in set (0.00 sec)

Mysql > alter table emp add column birth date after ename

Query OK, 0 rows affected (0.16 sec)

Records: 0 Duplicates: 0 Warnings: 0

Mysql > desc emp

+-+ +

| | Field | Type | Null | Key | Default | Extra | |

+-+ +

| | ename | varchar (30) | NO | PRI | |

| | birth | date | YES | | NULL |

| | hirdate | date | YES | | NULL |

| | sal | decimal (10jue 2) | YES | | NULL | |

| | deptno | int (2) | YES | | NULL |

+-+ +

5 rows in set (0.00 sec)

Mysql > alter table emp modify sal first

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that

Corresponds to your MySQL server version for the right syntax to use near 'first

'at line 1

Mysql > alter table emp modify sal decimal (10L2) first

Query OK, 0 rows affected (0.16 sec)

Records: 0 Duplicates: 0 Warnings: 0

Mysql > desc emp

+-+ +

| | Field | Type | Null | Key | Default | Extra | |

+-+ +

| | sal | decimal (10jue 2) | YES | | NULL | |

| | ename | varchar (30) | NO | PRI | |

| | birth | date | YES | | NULL |

| | hirdate | date | YES | | NULL |

| | deptno | int (2) | YES | | NULL |

+-+ +

5 rows in set (0.00 sec)

Mysql > alter table emp change ename name varchar (20) first

Query OK, 0 rows affected (0.16 sec)

Records: 0 Duplicates: 0 Warnings: 0

Mysql > desc emp

+-+ +

| | Field | Type | Null | Key | Default | Extra | |

+-+ +

| | name | varchar (20) | NO | PRI | |

| | sal | decimal (10jue 2) | YES | | NULL | |

| | birth | date | YES | | NULL |

| | hirdate | date | YES | | NULL |

| | deptno | int (2) | YES | | NULL |

+-+ +

5 rows in set (0.00 sec)

Mysql > alter table emp change birth birthday date after hirdate

Query OK, 0 rows affected (0.23 sec)

Records: 0 Duplicates: 0 Warnings: 0

Mysql > desc emp

+-+ +

| | Field | Type | Null | Key | Default | Extra | |

+-+ +

| | name | varchar (20) | NO | PRI | |

| | sal | decimal (10jue 2) | YES | | NULL | |

| | hirdate | date | YES | | NULL |

| | birthday | date | YES | | NULL |

| | deptno | int (2) | YES | | NULL |

+-+ +

5 rows in set (0.00 sec)

Mysql > alter table emp rename emptable

Query OK, 0 rows affected (0.22 sec)

Mysql > show tables

+-+

| | Tables_in_test |

+-+

| | emptable |

+-+

1 row in set (0.00 sec)

Mysql > drop table emptable

Query OK, 0 rows affected (0.06 sec)

Mysql > show tables

Empty set (0.00 sec)

Mysql > exit

Bye

C:\ Documents and Settings\ Administrator >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report