diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..17e71b8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,447 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ +out/ +target/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ +**/Properties/launchSettings.json +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +# publish/ + +# Publish Web Output +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Typescript v1 declaration files +typings/ +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +/build +/Build +/deploy +/package +bin/ +obj/ +sql/ +*/obj/debug +**/Debug +#ignore thumbnails created by windows +Thumbs.db +#Ignore files build by Visual Studio +*.obj +*.pdb +*.user +*.aps +*.pch +*.docstates +*.vspscc +*_i.c +*_p.c +*.ncb +*.suo +*.tlb +*.dbmdl +*.schemaview +*.tlh +*.cache +*.ilk +*.log +[Bb]in +[Dd]ebug*/ +*.lib +*.sbr +obj/ +[Rr]elease*/ +_ReSharper*/ +[Tt]est[Rr]esult* +*.docstates +*.swp +*.*~ +*.gpState +*.ReSharper* +*.preflight +*.nocommit +#Ignore Recovery Files made by Excel +~$*.xlsx +*.rdl.data + +*.jfm + +#====================================================================================== +# The below section could possibly be removed as these should be ignored by the above. +#====================================================================================== + +samples/in-memory/ticket-reservations/DemoWorkload/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +*.nupkg +samples/in-memory/ticket-reservations/packages/CircularGauge.1.0.0/CreatePackageFile.bat +*.suo +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Debug/PopulateAlwaysEncryptedData.exe +*.pdb +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Debug/PopulateAlwaysEncryptedData.vshost.exe +*.Cache +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Debug/PopulateAlwaysEncryptedData.exe.config +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs +samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +*.exe +samples/features/in-memory/iot-smart-grid/ConsoleClient/bin/Release/DataGenerator.dll +samples/features/in-memory/iot-smart-grid/ConsoleClient/bin/Release/Reports/PowerDashboard.pbix +samples/databases/wide-world-importers/wwi-integration-etl/Daily ETL/bin/Development/Daily ETL.ispac +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Release/PopulateAlwaysEncryptedData.csproj.FileListAbsolute.txt +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Release/PopulateAlwaysEncryptedData.vshost.exe.config +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Release/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +*.dll +samples/features/in-memory/ticket-reservations/DemoWorkload/obj/Release/DemoWorkload.FrmConfig.resources +samples/features/in-memory/ticket-reservations/DemoWorkload/bin/Release/DemoWorkload.vshost.exe.config +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Debug/PopulateAlwaysEncryptedData.csproj.FileListAbsolute.txt +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Debug/PopulateAlwaysEncryptedData.vshost.exe.config +*.zip +samples/features/in-memory/ticket-reservations/packages/CircularGauge.1.0.0/CreatePackageFile.bat +samples/features/in-memory/ticket-reservations/TicketReservations/TicketReservations.dbmdl +samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Release/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs +samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Debug/MultithreadedInMemoryTableInsert.csproj.FileListAbsolute.txt +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Debug/PopulateAlwaysEncryptedData.Properties.Resources.resources +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Release/PopulateAlwaysEncryptedData.PopulateAlwaysEncryptedDataMain.resources +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/bin/Debug/PopulateAlwaysEncryptedData.vshost.exe.manifest +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Debug/PopulateAlwaysEncryptedData.PopulateAlwaysEncryptedDataMain.resources +samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Release/MultithreadedInMemoryTableInsert.Properties.Resources.resources +samples/databases/wide-world-importers/sample-scripts/always-encrypted/PopulateAlwaysEncryptedData/obj/Release/PopulateAlwaysEncryptedData.Properties.Resources.resources +samples/features/in-memory/ticket-reservations/packages/CircularGauge.1.0.0/ReadMe.txt +*.dacpac +samples/features/in-memory/ticket-reservations/TicketReservations/obj/Release/Model.xml +samples/features/in-memory/ticket-reservations/DemoWorkload/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +*.dat + +*.user +samples/features/in-memory/ticket-reservations/DemoWorkload/obj/Release/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +samples/databases/wide-world-importers/workload-drivers/vehicle-location-insert/MultithreadedInMemoryTableInsert/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs +samples/features/in-memory/ticket-reservations/DemoWorkload/bin/Debug/DemoWorkload.vshost.exe.config +samples/features/in-memory/ticket-reservations/DemoWorkload/obj/Release/DemoWorkload.Properties.Resources.resources +samples/databases/wide-world-importers/wwi-integration-etl/Daily ETL/obj/Development/Project.params +samples/features/in-memory/ticket-reservations/DemoWorkload/obj/Debug/DemoWorkload.csproj.FileListAbsolute.txt +samples/features/in-memory/iot-smart-grid/Db/obj/Release/Model.xml +samples/applications/iot-smart-grid/DataGenerator/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/bin/Debug/MultithreadedOrderInsert.exe.config +samples/features/in-memory/iot-smart-grid/ConsoleClient/bin/Release/ConsoleClient.exe.config +*.jfm +samples/features/in-memory/ticket-reservations/TicketReservations/bin/Release/TicketReservations.publish.sql +samples/applications/iot-smart-grid/ConsoleClient/bin/Release/ConsoleClient.exe.config +samples/applications/iot-smart-grid/Db/Db.dbmdl +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/obj/Release/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs +samples/databases/wide-world-importers/wwi-dw-ssdt/wwi-dw-ssdt/obj/Debug/Model.xml +samples/databases/wide-world-importers/wwi-dw-ssdt/wwi-dw-ssdt/WideWorldImportersDW.dbmdl +samples/features/in-memory/iot-smart-grid/WinFormsClient/bin/Release/Reports/PowerDashboard.pbix +samples/databases/wide-world-importers/wwi-ssdt/wwi-ssdt/WideWorldImporters.dbmdl +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs +samples/databases/wide-world-importers/wwi-ssasmd/wwi-ssasmd/bin/WWI-SSASMD.asdatabase +samples/applications/iot-smart-grid/Db/obj/Release/Db.sqlproj.FileListAbsolute.txt +*.manifest +samples/applications/iot-smart-grid/WinFormsClient/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs +samples/databases/wide-world-importers/wwi-ssasmd/wwi-ssasmd/obj/Development/IncrementalShapshot.xml +samples/applications/iot-smart-grid/ConsoleClient/bin/Release/Reports/PowerDashboard.pbix +samples/applications/iot-smart-grid/Db/obj/Release/Model.xml +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/obj/Release/MultithreadedOrderInsert.csproj.FileListAbsolute.txt +samples/applications/iot-smart-grid/ConsoleClient/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs +samples/applications/iot-smart-grid/WinFormsClient/bin/Release/Client.exe.config +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/bin/Release/MultithreadedOrderInsert.exe.config +samples/databases/wide-world-importers/workload-drivers/order-insert/MultithreadedOrderInsert/obj/Debug/MultithreadedInMemoryTableInsert.MultithreadedOrderInsertMain.resources +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationErrors.rdl.data +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationErrorDetails.rdl.data +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyEvaluationDetails.rdl.data +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDetails.rdl.data +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboard.rdl.data +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/bin/Debug +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboard - Backup.rdl +/samples/features/epm-framework/5.0/2Reporting/PolicyReports/PolicyDashboardFiltered.rdl.data +samples/features/sql-management-objects/src/out/CodeCoverage/CodeCoverage.config + +# Certificates +*.pem +*.p12 + +# Composer +/vendor/ \ No newline at end of file diff --git a/1.sql b/1.sql new file mode 100644 index 0000000..6514f01 --- /dev/null +++ b/1.sql @@ -0,0 +1,5 @@ +SELECT pc.model, pc.speed, pc.hd FROM PC +INNER JOIN product +ON pc.model=product.model +WHERE pc.hd = 10 AND maker='A' OR pc.hd = 20 AND maker='A' +ORDER BY pc.speed; \ No newline at end of file diff --git a/10.sql b/10.sql new file mode 100644 index 0000000..13cf43d --- /dev/null +++ b/10.sql @@ -0,0 +1,8 @@ +SELECT type, product.model, MAX(price) AS max_price +FROM (SELECT model, price + FROM pc + UNION SELECT model, price + FROM printer + UNION SELECT model, price + FROM laptop) AS product_any JOIN product ON product_any.model = product.model +GROUP BY product.model \ No newline at end of file diff --git a/2.sql b/2.sql new file mode 100644 index 0000000..3287bb8 --- /dev/null +++ b/2.sql @@ -0,0 +1,2 @@ +SELECT * FROM outcome_o +WHERE date LIKE '____-__-14%'; \ No newline at end of file diff --git a/3.sql b/3.sql new file mode 100644 index 0000000..32bc8fa --- /dev/null +++ b/3.sql @@ -0,0 +1,4 @@ +SELECT maker +FROM product JOIN pc ON pc.model = product.model +GROUP BY maker + HAVING MIN(speed)>=600; \ No newline at end of file diff --git a/4.sql b/4.sql new file mode 100644 index 0000000..3b96ea6 --- /dev/null +++ b/4.sql @@ -0,0 +1,4 @@ +SELECT maker +FROM product LEFT JOIN pc ON pc.model = product.model +WHERE pc.model is null and type='PC' +GROUP BY maker; diff --git a/5.sql b/5.sql new file mode 100644 index 0000000..df84603 --- /dev/null +++ b/5.sql @@ -0,0 +1,9 @@ +SELECT maker +FROM product +WHERE EXISTS ( + SELECT maker + FROM product AS all_products LEFT JOIN pc ON pc.model = all_products.model + WHERE type = 'PC' AND product.maker = all_products.maker + GROUP BY maker + HAVING COUNT(all_products.model) = COUNT(pc.model)) +GROUP BY maker; diff --git a/6.sql b/6.sql new file mode 100644 index 0000000..da938ca --- /dev/null +++ b/6.sql @@ -0,0 +1,4 @@ +SELECT CONCAT('name: ', name) AS name, + CONCAT('class: ', class) AS class, + CONCAT('launched: ', launched) AS launched +FROM ships \ No newline at end of file diff --git a/7.sql b/7.sql new file mode 100644 index 0000000..0c43654 --- /dev/null +++ b/7.sql @@ -0,0 +1,5 @@ +SELECT maker +FROM product LEFT JOIN pc ON pc.model = product.model +WHERE type='PC' +GROUP BY maker + HAVING COUNT(pc.model) BETWEEN 1 AND COUNT(product.model) - 1; \ No newline at end of file diff --git a/8.sql b/8.sql new file mode 100644 index 0000000..651eb7b --- /dev/null +++ b/8.sql @@ -0,0 +1,6 @@ +SELECT ship, + (SELECT classes.country FROM ships JOIN classes ON + outcomes.ship = ships.name AND ships.class = classes.class) AS country +FROM outcomes +WHERE result='sunk' AND + (SELECT ships.name FROM ships WHERE outcomes.ship = ships.name) IS NOT NULL; \ No newline at end of file diff --git a/9.sql b/9.sql new file mode 100644 index 0000000..a7cb9aa --- /dev/null +++ b/9.sql @@ -0,0 +1,9 @@ +SELECT maker, + IF(product_pc=0, 'no', CONCAT('yes(', available_pc, ')')) AS creates_pc +FROM(SELECT maker, CASE WHEN product.type='PC' THEN 1 ELSE 0 END AS + product_pc, COUNT(available_models.model) as available_pc + FROM product + LEFT JOIN (SELECT model FROM pc) AS available_models ON + available_models.model = product.model + GROUP BY maker) AS products_pc +ORDER BY maker; diff --git a/MySQL_DB_Select.sql b/MySQL_DB_Select.sql new file mode 100644 index 0000000..d3499ca --- /dev/null +++ b/MySQL_DB_Select.sql @@ -0,0 +1,546 @@ +create database if not exists Lab_2_SQL; +use Lab_2_SQL; + +CREATE TABLE Laptop ( + code int NOT NULL , + model varchar (50) NOT NULL , + speed smallint NOT NULL , + ram smallint NOT NULL , + hd real NOT NULL , + price decimal(8,2) NULL , + screen tinyint NOT NULL +); + +CREATE TABLE PC ( + code int NOT NULL , + model varchar (50) NOT NULL , + speed smallint NOT NULL , + ram smallint NOT NULL , + hd real NOT NULL , + cd varchar (10) NOT NULL , + price decimal(8,2) NULL +); + +CREATE TABLE Product ( + maker varchar (10) NOT NULL , + model varchar (50) NOT NULL , + type varchar (50) NOT NULL +); + +CREATE TABLE Printer ( + code int NOT NULL , + model varchar (50) NOT NULL , + color char (1) NOT NULL , + type varchar (10) NOT NULL , + price decimal(8,2) NULL +); + +ALTER TABLE Laptop ADD + CONSTRAINT PK_Laptop PRIMARY KEY NONCLUSTERED + ( + code + ); + +ALTER TABLE PC ADD + CONSTRAINT PK_pc PRIMARY KEY NONCLUSTERED + ( + code + ); + +ALTER TABLE Product ADD + CONSTRAINT PK_product PRIMARY KEY NONCLUSTERED + ( + model + ); + + +ALTER TABLE Printer ADD + CONSTRAINT PK_printer PRIMARY KEY NONCLUSTERED + ( + code + ); + +ALTER TABLE Laptop ADD + CONSTRAINT FK_Laptop_product FOREIGN KEY + ( + model + ) REFERENCES Product ( + model + ); + +ALTER TABLE PC ADD + CONSTRAINT FK_pc_product FOREIGN KEY + ( + model + ) REFERENCES Product ( + model + ); + +ALTER TABLE Printer ADD + CONSTRAINT FK_printer_product FOREIGN KEY + ( + model + ) REFERENCES Product ( + model + ); + +#---Product------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Product values('B','1121','PC'); +insert into Product values('A','1232','PC'); +insert into Product values('A','1233','PC'); +insert into Product values('E','1260','PC'); +insert into Product values('A','1276','Printer'); +insert into Product values('D','1288','Printer'); +insert into Product values('A','1298','Laptop'); +insert into Product values('C','1321','Laptop'); +insert into Product values('A','1401','Printer'); +insert into Product values('A','1408','Printer'); +insert into Product values('D','1433','Printer'); +insert into Product values('E','1434','Printer'); +insert into Product values('B','1750','Laptop'); +insert into Product values('A','1752','Laptop'); +insert into Product values('E','2111','PC'); +insert into Product values('E','2112','PC'); + +#---PC------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into PC values(1,'1232',500,64,5,'12x',600); +insert into PC values(2,'1121',750,128,14,'40x',850); +insert into PC values(3,'1233',500,64,5,'12x',600); +insert into PC values(4,'1121',600,128,14,'40x',850); +insert into PC values(5,'1121',600,128,8,'40x',850); +insert into PC values(6,'1233',750,128,20,'50x',950); +insert into PC values(7,'1232',500,32,10,'12x',400); +insert into PC values(8,'1232',450,64,8,'24x',350); +insert into PC values(9,'1232',450,32,10,'24x',350); +insert into PC values(10,'1260',500,32,10,'12x',350); +insert into PC values(11,'1233',900,128,40,'40x',980); + +#---Laptop------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Laptop values(1,'1298',350,32,4,700,11); +insert into Laptop values(2,'1321',500,64,8,970,12); +insert into Laptop values(3,'1750',750,128,12,1200,14); +insert into Laptop values(4,'1298',600,64,10,1050,15); +insert into Laptop values(5,'1752',750,128,10,1150,14); +insert into Laptop values(6,'1298',450,64,10,950,12); + +#---Printer------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Printer values(1,'1276','n','Laser',400); +insert into Printer values(2,'1433','y','Jet',270); +insert into Printer values(3,'1434','y','Jet',290); +insert into Printer values(4,'1401','n','Matrix',150); +insert into Printer values(5,'1408','n','Matrix',270); +insert into Printer values(6,'1288','n','Laser',400); + + +CREATE TABLE Income ( + code int NOT NULL , + point tinyint NOT NULL , + date datetime NOT NULL , + inc decimal(8,2) NOT NULL +); + +CREATE TABLE Outcome ( + code int NOT NULL , + point tinyint NOT NULL , + date datetime NOT NULL , + `out` decimal(8,2) NOT NULL +); + +CREATE TABLE Income_o ( + point tinyint NOT NULL , + date datetime NOT NULL , + inc decimal(8,2) NOT NULL +); + +CREATE TABLE Outcome_o ( + point tinyint NOT NULL , + date datetime NOT NULL , + `out` decimal(8,2) NOT NULL +); + +ALTER TABLE Income ADD + CONSTRAINT PK_Income PRIMARY KEY NONCLUSTERED + ( + code + ); + +ALTER TABLE Outcome ADD + CONSTRAINT PK_Outcome PRIMARY KEY NONCLUSTERED + ( + code + ); + +ALTER TABLE Income_o ADD + CONSTRAINT PK_Income_o PRIMARY KEY NONCLUSTERED + ( + point, + date + ); + +ALTER TABLE Outcome_o ADD + CONSTRAINT PK_Outcome_o PRIMARY KEY NONCLUSTERED + ( + point, + date + ); + +#---Income------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Income values(1,1,'2001-03-22 00:00:00.000',15000.00); +insert into Income values(2,1,'2001-03-23 00:00:00.000',15000.00); +insert into Income values(3,1,'2001-03-24 00:00:00.000',3600.00); +insert into Income values(4,2,'2001-03-22 00:00:00.000',10000.00); +insert into Income values(5,2,'2001-03-24 00:00:00.000',1500.00); +insert into Income values(6,1,'2001-04-13 00:00:00.000',5000.00); +insert into Income values(7,1,'2001-05-11 00:00:00.000',4500.00); +insert into Income values(8,1,'2001-03-22 00:00:00.000',15000.00); +insert into Income values(9,2,'2001-03-24 00:00:00.000',1500.00); +insert into Income values(10,1,'2001-04-13 00:00:00.000',5000.00); +insert into Income values(11,1,'2001-03-24 00:00:00.000',3400.00); +insert into Income values(12,3,'2001-09-13 00:00:00.000',1350.00); +insert into Income values(13,3,'2001-09-13 00:00:00.000',1750.00); + +#-----Outcome +insert into Outcome values(1,1,'2001-03-14 00:00:00.000',15348.00); +insert into Outcome values(2,1,'2001-03-24 00:00:00.000',3663.00); +insert into Outcome values(3,1,'2001-03-26 00:00:00.000',1221.00); +insert into Outcome values(4,1,'2001-03-28 00:00:00.000',2075.00); +insert into Outcome values(5,1,'2001-03-29 00:00:00.000',2004.00); +insert into Outcome values(6,1,'2001-04-11 00:00:00.000',3195.04); +insert into Outcome values(7,1,'2001-04-13 00:00:00.000',4490.00); +insert into Outcome values(8,1,'2001-04-27 00:00:00.000',3110.00); +insert into Outcome values(9,1,'2001-05-11 00:00:00.000',2530.00); +insert into Outcome values(10,2,'2001-03-22 00:00:00.000',1440.00); +insert into Outcome values(11,2,'2001-03-29 00:00:00.000',7848.00); +insert into Outcome values(12,2,'2001-04-02 00:00:00.000',2040.00); +insert into Outcome values(13,1,'2001-03-24 00:00:00.000',3500.00); +insert into Outcome values(14,2,'2001-03-22 00:00:00.000',1440.00); +insert into Outcome values(15,1,'2001-03-29 00:00:00.000',2006.00); +insert into Outcome values(16,3,'2001-09-13 00:00:00.000',1200.00); +insert into Outcome values(17,3,'2001-09-13 00:00:00.000',1500.00); +insert into Outcome values(18,3,'2001-09-14 00:00:00.000',1150.00); + +#---Income_o +insert into Income_o values(1,'2001-03-22 00:00:00.000',15000.00); +insert into Income_o values(1,'2001-03-23 00:00:00.000',15000.00); +insert into Income_o values(1,'2001-03-24 00:00:00.000',3400.00); +insert into Income_o values(1,'2001-04-13 00:00:00.000',5000.00); +insert into Income_o values(1,'2001-05-11 00:00:00.000',4500.00); +insert into Income_o values(2,'2001-03-22 00:00:00.000',10000.00); +insert into Income_o values(2,'2001-03-24 00:00:00.000',1500.00); +insert into Income_o values(3,'2001-09-13 00:00:00.000',11500.00); +insert into Income_o values(3,'2001-10-02 00:00:00.000',18000.00); + +#---Outcome_o +insert into Outcome_o values(1,'2001-03-14 00:00:00.000',15348.00); +insert into Outcome_o values(1,'2001-03-24 00:00:00.000',3663.00); +insert into Outcome_o values(1,'2001-03-26 00:00:00.000',1221.00); +insert into Outcome_o values(1,'2001-03-28 00:00:00.000',2075.00); +insert into Outcome_o values(1,'2001-03-29 00:00:00.000',2004.00); +insert into Outcome_o values(1,'2001-04-11 00:00:00.000',3195.04); +insert into Outcome_o values(1,'2001-04-13 00:00:00.000',4490.00); +insert into Outcome_o values(1,'2001-04-27 00:00:00.000',3110.00); +insert into Outcome_o values(1,'2001-05-11 00:00:00.000',2530.00); +insert into Outcome_o values(2,'2001-03-22 00:00:00.000',1440.00); +insert into Outcome_o values(2,'2001-03-29 00:00:00.000',7848.00); +insert into Outcome_o values(2,'2001-04-02 00:00:00.000',2040.00); +insert into Outcome_o values(3,'2001-09-13 00:00:00.000',1500.00); +insert into Outcome_o values(3,'2001-09-14 00:00:00.000',2300.00); +insert into Outcome_o values(3,'2002-09-16 00:00:00.000',2150.00); + + +CREATE TABLE Battles ( + name varchar (20) NOT NULL , + date datetime NOT NULL +); + +CREATE TABLE Classes ( + class varchar (50) NOT NULL , + type varchar (2) NOT NULL , + country varchar (20) NOT NULL , + numGuns tinyint NULL , + bore real NULL , + displacement int NULL +); + +CREATE TABLE Ships ( + name varchar (50) NOT NULL , + class varchar (50) NOT NULL , + launched smallint NULL +); + +CREATE TABLE Outcomes ( + ship varchar (50) NOT NULL , + battle varchar (20) NOT NULL , + result varchar (10) NOT NULL +); + +ALTER TABLE Battles ADD + CONSTRAINT PK_Battles PRIMARY KEY CLUSTERED + ( + name + ); + +ALTER TABLE Classes ADD + CONSTRAINT PK_Classes PRIMARY KEY CLUSTERED + ( + class + ); + +ALTER TABLE Ships ADD + CONSTRAINT PK_Ships PRIMARY KEY CLUSTERED + ( + name + ); + +ALTER TABLE Outcomes ADD + CONSTRAINT PK_Outcomes PRIMARY KEY CLUSTERED + ( + ship, + battle + ); + +ALTER TABLE Ships ADD + CONSTRAINT FK_Ships_Classes FOREIGN KEY + ( + class + ) REFERENCES Classes ( + class + ); + +ALTER TABLE Outcomes ADD + CONSTRAINT FK_Outcomes_Battles FOREIGN KEY + ( + battle + ) REFERENCES Battles ( + name + ); + +#---Classes------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Classes values('Bismarck','bb','Germany',8,15,42000); +insert into Classes values('Iowa','bb','USA',9,16,46000); +insert into Classes values('Kon','bc','Japan',8,14,32000); +insert into Classes values('North Carolina','bb','USA',12,16,37000); +insert into Classes values('Renown','bc','Gt.Britain',6,15,32000); +insert into Classes values('Revenge','bb','Gt.Britain',8,15,29000); +insert into Classes values('Tennessee','bb','USA',12,14,32000); +insert into Classes values('Yamato','bb','Japan',9,18,65000); + +#---Battles------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Battles values('Guadalcanal','1942-11-15 00:00:00.000'); +insert into Battles values('North Atlantic','1941-05-25 00:00:00.000'); +insert into Battles values('North Cape','1943-12-26 00:00:00.000'); +insert into Battles values('Surigao Strait','1944-10-25 00:00:00.000'); +insert into Battles values ('#Cuba62a' , '1962-10-20'); +insert into Battles values ('#Cuba62b' , '1962-10-25'); + +#---Ships------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Ships values('California','Tennessee',1921); +insert into Ships values('Haruna','Kon',1916); +insert into Ships values('Hiei','Kon',1914); +insert into Ships values('Iowa','Iowa',1943); +insert into Ships values('Kirishima','Kon',1915); +insert into Ships values('Kon','Kon',1913); +insert into Ships values('Missouri','Iowa',1944); +insert into Ships values('Musashi','Yamato',1942); +insert into Ships values('New Jersey','Iowa',1943); +insert into Ships values('North Carolina','North Carolina',1941); +insert into Ships values('Ramillies','Revenge',1917); +insert into Ships values('Renown','Renown',1916); +insert into Ships values('Repulse','Renown',1916); +insert into Ships values('Resolution','Renown',1916); +insert into Ships values('Revenge','Revenge',1916); +insert into Ships values('Royal Oak','Revenge',1916); +insert into Ships values('Royal Sovereign','Revenge',1916); +insert into Ships values('Tennessee','Tennessee',1920); +insert into Ships values('Washington','North Carolina',1941); +insert into Ships values('Wisconsin','Iowa',1944); +insert into Ships values('Yamato','Yamato',1941); +insert into Ships values('South Dakota','North Carolina',1941); + +#---Outcomes------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Outcomes values('Bismarck','North Atlantic','sunk'); +insert into Outcomes values('California','Surigao Strait','OK'); +insert into Outcomes values('Duke of York','North Cape','OK'); +insert into Outcomes values('Fuso','Surigao Strait','sunk'); +insert into Outcomes values('Hood','North Atlantic','sunk'); +insert into Outcomes values('King George V','North Atlantic','OK'); +insert into Outcomes values('Kirishima','Guadalcanal','sunk'); +insert into Outcomes values('Prince of Wales','North Atlantic','damaged'); +insert into Outcomes values('Rodney','North Atlantic','OK'); +insert into Outcomes values('Schamhorst','North Cape','sunk'); +insert into Outcomes values('South Dakota','Guadalcanal','damaged'); +insert into Outcomes values('Tennessee','Surigao Strait','OK'); +insert into Outcomes values('Washington','Guadalcanal','OK'); +insert into Outcomes values('West Virginia','Surigao Strait','OK'); +insert into Outcomes values('Yamashiro','Surigao Strait','sunk'); +insert into Outcomes values('California','Guadalcanal','damaged'); + + +CREATE TABLE Company ( + ID_comp int NOT NULL , + name char (10) NOT NULL +); + +CREATE TABLE Pass_in_trip ( + trip_no int NOT NULL , + date datetime NOT NULL , + ID_psg int NOT NULL , + place char (10) NOT NULL +); + +CREATE TABLE Passenger ( + ID_psg int NOT NULL , + name char (20) NOT NULL +); + +CREATE TABLE Trip ( + trip_no int NOT NULL , + ID_comp int NOT NULL , + plane char (10) NOT NULL , + town_from char (25) NOT NULL , + town_to char (25) NOT NULL , + time_out datetime NOT NULL , + time_in datetime NOT NULL +); + +ALTER TABLE Company ADD + CONSTRAINT PK2 PRIMARY KEY CLUSTERED + ( + ID_comp + ); + +ALTER TABLE Pass_in_trip ADD + CONSTRAINT PK_pt PRIMARY KEY CLUSTERED + ( + trip_no, + date, + ID_psg + ); + +ALTER TABLE Passenger ADD + CONSTRAINT PK_psg PRIMARY KEY CLUSTERED + ( + ID_psg + ); + +ALTER TABLE Trip ADD + CONSTRAINT PK_t PRIMARY KEY CLUSTERED + ( + trip_no + ); + +ALTER TABLE Pass_in_trip ADD + CONSTRAINT FK_Pass_in_trip_Passenger FOREIGN KEY + (ID_psg) REFERENCES Passenger (ID_psg); +ALTER TABLE Pass_in_trip ADD + CONSTRAINT FK_Pass_in_trip_Trip FOREIGN KEY + (trip_no) REFERENCES Trip (trip_no); + +ALTER TABLE Trip ADD + CONSTRAINT FK_Trip_Company FOREIGN KEY + ( + ID_comp + ) REFERENCES Company ( + ID_comp + ); + +#---Company------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Company values(1,'Don_avia '); +insert into Company values(2,'Aeroflot '); +insert into Company values(3,'Dale_avia '); +insert into Company values(4,'air_France'); +insert into Company values(5,'British_AW'); + +#---Passenger------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Passenger values(1,'Bruce Willis '); +insert into Passenger values(2,'George Clooney '); +insert into Passenger values(3,'Kevin Costner '); +insert into Passenger values(4,'Donald Sutherland '); +insert into Passenger values(5,'Jennifer Lopez '); +insert into Passenger values(6,'Ray Liotta '); +insert into Passenger values(7,'Samuel L. Jackson '); +insert into Passenger values(8,'Nikole Kidman '); +insert into Passenger values(9,'Alan Rickman '); +insert into Passenger values(10,'Kurt Russell '); +insert into Passenger values(11,'Harrison Ford '); +insert into Passenger values(12,'Russell Crowe '); +insert into Passenger values(13,'Steve Martin '); +insert into Passenger values(14,'Michael Caine '); +insert into Passenger values(15,'Angelina Jolie '); +insert into Passenger values(16,'Mel Gibson '); +insert into Passenger values(17,'Michael Douglas '); +insert into Passenger values(18,'John Travolta '); +insert into Passenger values(19,'Sylvester Stallone '); +insert into Passenger values(20,'Tommy Lee Jones '); +insert into Passenger values(21,'Catherine Zeta-Jones'); +insert into Passenger values(22,'Antonio Banderas '); +insert into Passenger values(23,'Kim Basinger '); +insert into Passenger values(24,'Sam Neill '); +insert into Passenger values(25,'Gary Oldman '); +insert into Passenger values(26,'Clint Eastwood '); +insert into Passenger values(27,'Brad Pitt '); +insert into Passenger values(28,'Johnny Depp '); +insert into Passenger values(29,'Pierce Brosnan '); +insert into Passenger values(30,'Sean Connery '); +insert into Passenger values(31,'Bruce Willis '); +insert into Passenger values(37,'Mullah Omar '); + +#---Trip------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Trip values(1100,4,'Boeing ','Rostov ','Paris ','1900-01-01 14:30:00.000','1900-01-01 17:50:00.000'); +insert into Trip values(1101,4,'Boeing ','Paris ','Rostov ','1900-01-01 08:12:00.000','1900-01-01 11:45:00.000'); +insert into Trip values(1123,3,'TU-154 ','Rostov ','Vladivostok ','1900-01-01 16:20:00.000','1900-01-01 03:40:00.000'); +insert into Trip values(1124,3,'TU-154 ','Vladivostok ','Rostov ','1900-01-01 09:00:00.000','1900-01-01 19:50:00.000'); +insert into Trip values(1145,2,'IL-86 ','Moscow ','Rostov ','1900-01-01 09:35:00.000','1900-01-01 11:23:00.000'); +insert into Trip values(1146,2,'IL-86 ','Rostov ','Moscow ','1900-01-01 17:55:00.000','1900-01-01 20:01:00.000'); +insert into Trip values(1181,1,'TU-134 ','Rostov ','Moscow ','1900-01-01 06:12:00.000','1900-01-01 08:01:00.000'); +insert into Trip values(1182,1,'TU-134 ','Moscow ','Rostov ','1900-01-01 12:35:00.000','1900-01-01 14:30:00.000'); +insert into Trip values(1187,1,'TU-134 ','Rostov ','Moscow ','1900-01-01 15:42:00.000','1900-01-01 17:39:00.000'); +insert into Trip values(1188,1,'TU-134 ','Moscow ','Rostov ','1900-01-01 22:50:00.000','1900-01-01 00:48:00.000'); +insert into Trip values(1195,1,'TU-154 ','Rostov ','Moscow ','1900-01-01 23:30:00.000','1900-01-01 01:11:00.000'); +insert into Trip values(1196,1,'TU-154 ','Moscow ','Rostov ','1900-01-01 04:00:00.000','1900-01-01 05:45:00.000'); +insert into Trip values(7771,5,'Boeing ','London ','Singapore ','1900-01-01 01:00:00.000','1900-01-01 11:00:00.000'); +insert into Trip values(7772,5,'Boeing ','Singapore ','London ','1900-01-01 12:00:00.000','1900-01-01 02:00:00.000'); +insert into Trip values(7773,5,'Boeing ','London ','Singapore ','1900-01-01 03:00:00.000','1900-01-01 13:00:00.000'); +insert into Trip values(7774,5,'Boeing ','Singapore ','London ','1900-01-01 14:00:00.000','1900-01-01 06:00:00.000'); +insert into Trip values(7775,5,'Boeing ','London ','Singapore ','1900-01-01 09:00:00.000','1900-01-01 20:00:00.000'); +insert into Trip values(7776,5,'Boeing ','Singapore ','London ','1900-01-01 18:00:00.000','1900-01-01 08:00:00.000'); +insert into Trip values(7777,5,'Boeing ','London ','Singapore ','1900-01-01 18:00:00.000','1900-01-01 06:00:00.000'); +insert into Trip values(7778,5,'Boeing ','Singapore ','London ','1900-01-01 22:00:00.000','1900-01-01 12:00:00.000'); +insert into Trip values(8881,5,'Boeing ','London ','Paris ','1900-01-01 03:00:00.000','1900-01-01 04:00:00.000'); +insert into Trip values(8882,5,'Boeing ','Paris ','London ','1900-01-01 22:00:00.000','1900-01-01 23:00:00.000'); + +#---Pass_in_trip------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +insert into Pass_in_trip values(1100,'2003-04-29 00:00:00.000',1,'1a '); +insert into Pass_in_trip values(1123,'2003-04-05 00:00:00.000',3,'2a '); +insert into Pass_in_trip values(1123,'2003-04-08 00:00:00.000',1,'4c '); +insert into Pass_in_trip values(1123,'2003-04-08 00:00:00.000',6,'4b '); +insert into Pass_in_trip values(1124,'2003-04-02 00:00:00.000',2,'2d '); +insert into Pass_in_trip values(1145,'2003-04-05 00:00:00.000',3,'2c '); +insert into Pass_in_trip values(1181,'2003-04-01 00:00:00.000',1,'1a '); +insert into Pass_in_trip values(1181,'2003-04-01 00:00:00.000',6,'1b '); +insert into Pass_in_trip values(1181,'2003-04-01 00:00:00.000',8,'3c '); +insert into Pass_in_trip values(1181,'2003-04-13 00:00:00.000',5,'1b '); +insert into Pass_in_trip values(1182,'2003-04-13 00:00:00.000',5,'4b '); +insert into Pass_in_trip values(1187,'2003-04-14 00:00:00.000',8,'3a '); +insert into Pass_in_trip values(1188,'2003-04-01 00:00:00.000',8,'3a '); +insert into Pass_in_trip values(1182,'2003-04-13 00:00:00.000',9,'6d '); +insert into Pass_in_trip values(1145,'2003-04-25 00:00:00.000',5,'1d '); +insert into Pass_in_trip values(1187,'2003-04-14 00:00:00.000',10,'3d '); +insert into Pass_in_trip values(8882,'2005-11-06 00:00:00.000',37,'1a '); +insert into Pass_in_trip values(7771,'2005-11-07 00:00:00.000',37,'1c '); +insert into Pass_in_trip values(7772,'2005-11-07 00:00:00.000',37,'1a '); +insert into Pass_in_trip values(8881,'2005-11-08 00:00:00.000',37,'1d '); +insert into Pass_in_trip values(7778,'2005-11-05 00:00:00.000',10,'2a '); +insert into Pass_in_trip values(7772,'2005-11-29 00:00:00.000',10,'3a '); +insert into Pass_in_trip values(7771,'2005-11-04 00:00:00.000',11,'4a '); +insert into Pass_in_trip values(7771,'2005-11-07 00:00:00.000',11,'1b '); +insert into Pass_in_trip values(7771,'2005-11-09 00:00:00.000',11,'5a '); +insert into Pass_in_trip values(7772,'2005-11-07 00:00:00.000',12,'1d '); +insert into Pass_in_trip values(7773,'2005-11-07 00:00:00.000',13,'2d '); +insert into Pass_in_trip values(7772,'2005-11-29 00:00:00.000',13,'1b '); +insert into Pass_in_trip values(8882,'2005-11-13 00:00:00.000',14,'3d '); +insert into Pass_in_trip values(7771,'2005-11-14 00:00:00.000',14,'4d '); +insert into Pass_in_trip values(7771,'2005-11-16 00:00:00.000',14,'5d '); +insert into Pass_in_trip values(7772,'2005-11-29 00:00:00.000',14,'1c '); + + diff --git a/README.md b/README.md index 681d633..6c870e9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ -# Databases - +# Lab-2 + +## Task - V11 diff --git a/random_table_split.sql b/random_table_split.sql new file mode 100644 index 0000000..2677012 --- /dev/null +++ b/random_table_split.sql @@ -0,0 +1,62 @@ +# create two more tables from existent one, with one to one relationship +# where columns are randomly picked from parent table. +DROP PROCEDURE IF EXISTS table3_split; +DELIMITER // +CREATE PROCEDURE table3_split() +BEGIN + DECLARE done BOOL DEFAULT FALSE; + DECLARE num INT DEFAULT (0); + DECLARE table3_names VARCHAR(10); + DECLARE name_cursor CURSOR + FOR SELECT COLUMN_NAME + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_NAME = 'laptop'; #any table name, in this database table3 not exist + DECLARE CONTINUE HANDLER + FOR NOT FOUND SET done = TRUE; + open name_cursor; + names_loop: + LOOP + FETCH name_cursor INTO table3_names; + IF done THEN + LEAVE names_loop; + END IF; + SET @new_table4 = + 'CREATE TABLE IF NOT EXISTS table4 + ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id));'; + SELECT @new_table4; + PREPARE new_table_query FROM @new_table4; + EXECUTE new_table_query; + SET @new_table5 = + 'CREATE TABLE IF NOT EXISTS table5 ( id INT NOT NULL AUTO_INCREMENT, + `table4` INT NOT NULL, + PRIMARY KEY(id, table4), + INDEX `fk_table4_idx` (`table4` ASC) VISIBLE, + CONSTRAINT `fk_table4` + FOREIGN KEY (`table4`) + REFERENCES `Lab_2_SQL`.`table4` (`id`));'; + SELECT @new_table5; + PREPARE new_table_query FROM @new_table5; + EXECUTE new_table_query; + SET num = rand(); + IF num < 0.50 THEN + SET @insert_table4 = + CONCAT('ALTER TABLE table4 ADD COLUMN ', table3_names, ' VARCHAR(50);'); + SELECT @insert_table4; + PREPARE new_table_query FROM @insert_table4; + EXECUTE new_table_query; + END IF; + IF num > 0.50 THEN + SET @insert_table5 = + CONCAT('ALTER TABLE table5 ADD COLUMN ', table3_names, ' VARCHAR(50);'); + SELECT @insert_table5; + PREPARE new_table_query FROM @insert_table5; + EXECUTE new_table_query; + END IF; + END LOOP; + CLOSE name_cursor; +END // +DELIMITER ; + +DROP TABLE IF EXISTS table5; +DROP TABLE IF EXISTS table4; +call table3_split()