学点SQL语句(一)

阅读 136

2022-07-12


文章目录

  • ​​库操作​​
  • ​​表操作​​

库操作

语法:
增:CREATE DATABASE NAME;

create database d2;

查:
查看指定库:
SHOW CREATE DATABASE NAME;
查看所有库:
SHOW DATABASES;
查看当前库:
SELECT DATABASE();

show create database d2;
show databases;
show database();

选择数据库:
USE NAME;

use d2;

删:
DROP DATABASE NAME;

drop database d2;

改:
也就只能修改库的字符编码
ALTER DATABASE NAME CHARSET CHAR_CODE;

alter database name charset gbk;
mysql> create database d2;
Query OK, 1 row affected (0.12 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| d1 |
| d2 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.10 sec)

mysql> show create database d2;
+----------+-------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------+
| d2 | CREATE DATABASE `d2` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| d1 |
| d2 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)

mysql> select database;
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 '' at line 1
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)

mysql> alter database d2 charset gbk;
Query OK, 1 row affected (0.09 sec)

mysql> show create database d2;
+----------+------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------+
| d2 | CREATE DATABASE `d2` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> drop database d2;
Query OK, 0 rows affected (0.18 sec)

mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| d1 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

mysql>

表操作

创建:
CREATE TABLE NAME(字段名1 类型[(宽度)约束条件],字段名2 类型[(宽度)约束条件]);

create table t3(id int);
create table t4(id int,name char):

查:
查看所有表:
SHOW TABLES;
查看某一张表结构
SHOW CREATE NAME;
查看某一张表的详细结构
DESC NAME;
查看表内的所有记录:
SELECT * FROM NAME;
查看表内某一条记录:
SELECT NAME FROM NAME;

show tables;
show create table t1;
desc t1;
select * from t1;
select id from t1;

向表内插入记录:
INSERT INTO NAME VALUSES();
特定字段插入记录:
INSERT INTO NAME(NAME,NAME,NAME) VALUSES();

insert into  t1 values(7,'wang');
insert into t1(id) values(5);

修改:
改表名:
ALTER TABLE NAME RENAME NEW_NAME
增加字段:
ALTER TABLE NAME ADD CHAR_NAME 数据类型,ADD CHAR_NAME 数据类型;
为增加的字段设定位置:
放首位:
ALTER TABLE NAME ADD CHAR_NAME 数据类型 FIRST;
放在xx后面:
ALTER TABLE NAME ADD CHAR_NAME 数据类型 AFTER 字段名;

alter table t1 rename t11;
alter table t11 add age int;
alter table t11 add sex char first;
alter table t11 add gender char after sex;

删除字段:
ALTER TABLE NAME DROP NAME;
修改字段:
ALTER TABLE NAME MODIFY NAME 数据类型;
ALTER TABLE NAME CHANGE NAME NEW_NAME 旧数据类型;
ALTER TABLE NAME CHANGE NAME NEW_NAME 新数据类型;

alter table t11 drop age;
alter table t11 modify name char;
alter table t11 change name Name char;
alter table t11 change Name name char(2);

复制表

复制表结构+记录
CREATE TABLE NEW_NAME SELECT * FROM NAME;
只复制表结构
CREATE TABLE NEW_NAME SELECT * FROM NAME WHER FALSE(1=2);

create table new_t1 select * from t11;
create table new_t11 select * from t11 where 1 >2

删除表
DROP TABLE NAME;

drop table new_t11;
drop table new_t111;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| d1 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

mysql> use d1;
Database changed
mysql> show tables;
+--------------+
| Tables_in_d1 |
+--------------+
| new_t1 |
| t1 |
| t4 |
+--------------+
3 rows in set (0.10 sec)

mysql> show create table t1;
+-------+---------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`id` int(11) DEFAULT NULL,
`name` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------+
1 row in set (0.18 sec)

mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> create table t1(id int);
ERROR 1050 (42S01): Table 't1' already exists
mysql> create table t3(id int);
Query OK, 0 rows affected (1.00 sec)

mysql> show tables;
+--------------+
| Tables_in_d1 |
+--------------+
| new_t1 |
| t1 |
| t3 |
| t4 |
+--------------+
4 rows in set (0.00 sec)

mysql> select * from t1;'
+------+------+
| id | name |
+------+------+
| 1 | NULL |
| 2 | NULL |
| 1 | 0 |
| 1 | 1 |
+------+------+
4 rows in set (0.10 sec)

'> \c
'> /c
'> '\c
mysql> select id from t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 1 |
| 1 |
+------+
4 rows in set (0.00 sec)

mysql> insert into table t1 valuse(1,'wang');
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 'table t1 valuse(1,'wang')' at line 1
mysql> insert into table t1 valuse(7,'wang');
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 'table t1 valuse(7,'wang')' at line 1
mysql> insert into table t1 values(7,'wang');
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 'table t1 values(7,'wang')' at line 1
mysql> insert into t1 values(1,'wang');
Query OK, 1 row affected, 1 warning (1.83 sec)

mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | NULL |
| 2 | NULL |
| 1 | 0 |
| 1 | 1 |
| 1 | 0 |
+------+------+
5 rows in set (0.00 sec)

mysql> insert into t1 values(7,2);
Query OK, 1 row affected (1.84 sec)

mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | NULL |
| 2 | NULL |
| 1 | 0 |
| 1 | 1 |
| 1 | 0 |
| 7 | 2 |
+------+------+
6 rows in set (0.00 sec)

mysql> inset into t1(id) values(5);
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 'inset into t1(id) values(5)' at line 1
mysql> insert into t1(id) values(5);
Query OK, 1 row affected (0.25 sec)

mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | NULL |
| 2 | NULL |
| 1 | 0 |
| 1 | 1 |
| 1 | 0 |
| 7 | 2 |
| 5 | NULL |
+------+------+
7 rows in set (0.00 sec)

mysql> alter table t1 rename t11;
Query OK, 0 rows affected (2.16 sec)

mysql> show tables;
+--------------+
| Tables_in_d1 |
+--------------+
| new_t1 |
| t11 |
| t3 |
| t4 |
+--------------+
4 rows in set (0.00 sec)

mysql> alter table t11 add agg(int);
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 '(int)' at line 1
mysql> alter table t11 add age int;
Query OK, 0 rows affected (3.21 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from t11;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | NULL | NULL |
| 2 | NULL | NULL |
| 1 | 0 | NULL |
| 1 | 1 | NULL |
| 1 | 0 | NULL |
| 7 | 2 | NULL |
| 5 | NULL | NULL |
+------+------+------+
7 rows in set (0.00 sec)

mysql> alter drop age;
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 'drop age' at line 1
mysql> alter t11 drop age;
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 't11 drop age' at line 1
mysql> alter table t11 drop age;
Query OK, 0 rows affected (2.47 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from t11;
+------+------+
| id | name |
+------+------+
| 1 | NULL |
| 2 | NULL |
| 1 | 0 |
| 1 | 1 |
| 1 | 0 |
| 7 | 2 |
| 5 | NULL |
+------+------+
7 rows in set (0.00 sec)

mysql> alter table t11 name char;
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 'name char' at line 1
mysql> alter table t11 modify name char;
Query OK, 7 rows affected (3.61 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> select * from t11;
+------+------+
| id | name |
+------+------+
| 1 | NULL |
| 2 | NULL |
| 1 | 0 |
| 1 | 1 |
| 1 | 0 |
| 7 | 2 |
| 5 | NULL |
+------+------+
7 rows in set (0.00 sec)

mysql> desc t11;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> alter table t11 change name Name char;
Query OK, 0 rows affected (0.26 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc
-> t11;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| Name | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> alter table t11 change Name name char(2);
Query OK, 7 rows affected (1.98 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> desc t11;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(2) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> create table new_t1 select * from t11;
ERROR 1050 (42S01): Table 'new_t1' already exists
mysql> create table new_t11 select * from t11;
Query OK, 7 rows affected (0.94 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> desc new_t11;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(2) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> create table new_t111 select * from t11 where 1=2;
Query OK, 0 rows affected (0.55 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc new_t111;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(2) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> select * from new_t11;
+------+------+
| id | name |
+------+------+
| 1 | NULL |
| 2 | NULL |
| 1 | 0 |
| 1 | 1 |
| 1 | 0 |
| 7 | 2 |
| 5 | NULL |
+------+------+
7 rows in set (0.00 sec)

mysql> select * from new_t111;
Empty set (0.00 sec)

mysql> show tables;
+--------------+
| Tables_in_d1 |
+--------------+
| new_t1 |
| new_t11 |
| new_t111 |
| t11 |
| t3 |
| t4 |
+--------------+
6 rows in set (0.00 sec)

mysql> drop new_t1;
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 'new_t1' at line 1
mysql> dw_t1;
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 'dw_t1' at line 1
mysql>
mysql> drop table new_t1;
Query OK, 0 rows affected (1.52 sec)

mysql> drop table new_t11;
Query OK, 0 rows affected (0.64 sec)

mysql> drop table new_t111;
Query OK, 0 rows affected (0.65 sec)

mysql> show tables;
+--------------+
| Tables_in_d1 |
+--------------+
| t11 |
| t3 |
| t4 |
+--------------+
3 rows in set (0.00 sec)

mysql>


精彩评论(0)

0 0 举报