服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - MySQL语句汇总整理

MySQL语句汇总整理

2022-02-13 19:29L-M-Y Mysql

这篇文章主要给大家分享的是MySQL语句汇总整理,围绕MySQL语句的相关资料对其进行整理,具有一定的参考价值,需要的小伙伴可以参考一下,希望对你有所帮助

 

1.选择数据库 USE

刚链接到MySQL时,没有数据库打开供你使用,而我们需要选择一个数据库,才能进行以下的操作。

方法:USE语句

USE Mdatabase;

如此,才能打开数据库,读取其中的数据

 

2.显示信息 SHOW

MySQL从内部表中提取信息

SHOW database;

返回可用数据库的一个列表

SHOW tables;

返回数据库内的表的列表

SHOW columns from table;

显示表列

 

3.创建表 CREATE TABLE

基本语法:

//by LMY
CREATE DATABASE 数据库名;   /*创建数据库*/
USE 数据库名;   /*连接数据库*/

CREATE TABLE 表名1
(
    列名1   数据类型    要求,
    列名2   数据类型    要求,
    列名3   数据类型    要求,
    .......
    PRIMARY KEY (主键) /*注意,此处没有逗号*/
);

以我的数据库作业作为栗子:

(环境,MySQL workbench

要求:

创建一个名为”my_own_movie_database“的数据库,之后,建如下几个表,之后把相应输入插入

MySQL语句汇总整理

首先,打开workbench,添加一个新的SQL文件

MySQL语句汇总整理

之后,在新的文件中写入sql语句

MySQL语句汇总整理

写入相应sql语句,写完,你会在左侧发现,有了一个新的数据库,点进去,还可以看到你建的表

完整代码如下:

drop database if exists `my_own_movie_database`;
create database `my_own_movie_database`;

USE `my_own_movie_database`;

CREATE TABLE `movieexec` (
  `name` char(30) DEFAULT NULL,
  `address` varchar(100) DEFAULT NULL,
  `cert` int(11) NOT NULL DEFAULT "0
  ",
  `netWorth` int(11) DEFAULT NULL,
  PRIMARY KEY (`cert`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#
# Data for table "movieexec"
#

INSERT INTO `movieexec` VALUES ("Calvin Coolidge","Fast Lane",123,20000000),("Merv Griffin","Riot Rd.",199,112000000),("Stephen Spielberg","123 ET road",222,100000000),("Ted Turner","Turner Av.",333,125000000),("George Lucas","Oak Rd.",555,200000000),("Jane Fonda","Turner Av.",567,200000000);

#
# Source for table "movies"
#

CREATE TABLE `studio` (
  `name` char(30) NOT NULL DEFAULT "",
  `address` varchar(100) DEFAULT NULL,
  `presC` int(11) DEFAULT NULL,
  PRIMARY KEY (`name`)
) ;

#
# Data for table "studio"
#

INSERT INTO `studio` VALUES ("MGM","MGM Boulevard",123),("Fox","Fox Boulevard",199),("Disney","Disney Boulevard",222),("USA Entertainm","USA Entertainm Boulevard",333),("Paramount","Paramount Boulevard",555);


CREATE TABLE `movies` (
  `title` char(100) NOT NULL DEFAULT "",
  `year` int(11) NOT NULL DEFAULT "0",
  `length` int(11) DEFAULT NULL,
  `movieType` char(10) DEFAULT NULL,
  `studioName` char(30) ,
  `producerC` int(11) DEFAULT NULL,
  PRIMARY KEY (`title`,`year`),
 foreign key(studioName) references studio(name) on delete cascade
) ;

#
# Data for table "movies"
#

INSERT INTO `movies` VALUES ("Empire Strikes Back",1980,111,"drama","Fox",555),("Gone With the Wind",1938,null,"drama","MGM",123),("Logan"s run",1977,120,"drama","MGM",888),("Pretty Woman",1990,119,"drama","Disney",999),("Star Trek",1979,132,"sciFic","Paramount",444),("Star Trek: Nemesis",2002,116,"sciFic","Paramount",321),("Star Wars",1977,124,"sciFic","Fox",555),("Terms of Endearment",1983,132,"drama","MGM",123),("The Man Who Wasn"t There",2001,116,"comedy","USA Entertainm",777),("The Usual Suspects",1995,106,"drama","MGM",999);

#
# Source for table "moviestar"
#


CREATE TABLE `moviestar` (
  `name` char(30) NOT NULL DEFAULT "",
  `address` varchar(255) DEFAULT NULL,
  `gender` char(1) DEFAULT NULL,
  `birthdate` date DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#
# Data for table "moviestar"
#
INSERT INTO `moviestar` VALUES ("Alec Baldwin","Baldwin Av.","M","1977-06-07"),("Debra Winger","A way","F","1978-05-06"),("Harrison Ford","Prefect Rd.","M","1955-05-05"),("Jack Nicholson""s","X path","M","1949-05-05"),("Jane Fonda","Turner Av.","F","1977-07-07");
#
# Source for table "starsin"
#
CREATE TABLE `starsin` (
  `movieTitle` char(100) NOT NULL DEFAULT "",
  `movieYear` int(11) NOT NULL DEFAULT "0",
  `starName` char(30) NOT NULL DEFAULT "",
  PRIMARY KEY (`movieTitle`,`movieYear`,`starName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#
# Data for table "starsin"
#

INSERT INTO `starsin` VALUES ("Empire Strikes Back",1980,"Harrison Ford"),("Star Wars",1977,"Carrie Fisher"),("Star Wars",1977,"Harrison Ford"),("Star Wars",1977,"Mark Hamill"),("Terms of Endearment",1983,"Debra Winger"),("Terms of Endearment",1983,"Jack Nicholson"),("The Usual Suspects",1995,"Kevin Spacey");

#
# Source for table "studio"
#

 

4.插入数据 INSERT

基本写法:

INSERT INTO 表名1 VALUES (数据1,数据2,数据3.....),(数据1,数据2......)....;


最后,运行,快捷键:Ctrl +Shift+Enter
(这个地方我愣是到这门课都快学完了,才知道怎么运行…哭死了,前半学期装不上软件,后半学期天天迷为什么建不了表…)
建完表之后,可以看到,表中的内容

MySQL语句汇总整理

到此这篇关于MySQL语句汇总整理 的文章就介绍到这了,更多相关MySQL语句整理 内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_52109814/article/details/121771413

延伸 · 阅读

精彩推荐