SQL code not running , getting a "permission denied for schema public" error message - sql

CREATE TABLE student
(
student_id INT PRIMARY KEY,
nam VARCHAR (20),
major VARCHAR (20)
);
I'm just running this code and I keep getting a permission denied for schema public message. What does this mean and how can I fix it.?

I see you are asking this in reference to the freeCodeCamp's SQL course by the looks of it.
You need to create your own database and you should be good to go, see the steps below:
Open mySQL command line and create a new database CREATE DATABASE TestDB
Open PopSQL > File > Preferences > Connections > Add New Connection > MySQL
Connection name: any name
Hostname/Port: localhost | 3306 (3306 is default port number, maybe you used/have different port during MySQL installation)
Database: TestDB
Username: root | Password: (this is what you set when you installed MySQL)
Connection Type: turn this ON, you want to "Connect directly from my computer"
Test connection, if successful, save.
Also, please note this is working on MySQL Server 5.7.20, it may be different on other versions.

It means you are using POPsql sample data. Click on manage connections and add a connection to your own database.

Please make sure you are using the right database, in the popSQL the "PopSQL Sample Data" database is selected by default.
you can change it as mentioned below

You need to add your own new connection instead of running it on POPsql sample data private connection

just delete the other sample server from the list of connections and add your own.

I see you are asking this in reference to the freeCodeCamp's SQL course as the same with me, what you really need to do is make sure you follow all of his instructions and after that make sure you change the connection to MySQL after add the new connections.
enter image description here

Related

Unable to access the temp tables in azure sql database

Using following code I have created a temp table in azure sql database.
CREATE TABLE ##UpsertTempTable (
eno varchar(25),
ename varchar(25)
);
and I am want to check the data using the below query
select * from ##UpsertTempTable
Ideally it should run without any issue as in all of the azure documentation it works without any issues but unfortunately it is not working and giving below error.
I tried looking solution in all places in the internet but could not find any relevant documentation for this issue.
Error : Failed to execute query. Error: Invalid object name '##UpsertTempTable'.
I tried in Query Editor(Preview) in Portal, and create temporary table code doesn't work. I both used ##UpsertTempTable and #UpsertTempTable.
For example, when we run the code, no error happens .
When you run select * from ##UpsertTempTable, Query editor will gives the error:
I also try with SSMS V17.9 and SSMS V18.1, everything is ok.
What I think is the query editor doesn't support create temporary table well.
I asked Azure Support and wait their replay, please wait my update.
Update:
Azure Support replied me:
"This is by design, the temp tables exists as long as the connection is open.
The current way portal query editor is designed, the connection is killed resulting in temp table being deleted.
"
Hope this helps.

how to use sql server packages in go

I use the denisenkom/go-mssqldb package for connecting to SQL server in go but I don't know how to write the connecting code,
what are the arguments of this method : sql.Open() for example when we say :
db, err := sql.Open("mysql", "user:password#/database")
what do "mysql" and "user:password#/database" refer to?
and in which part of the program we should tell the name of the database which we want to use in the program? I mean in which part of the code we introduce the name of the database we want to use?
"mssql" is the protocol/driver/database type you are connecting to. "user:password#/database" is the connection string. See this example for the components of this String.
Your application typically knows the database it wants to connect. Or at last when the user logs in.

Check for the server name in connection string if it exists or not

I have done a program where in which I'll pass the server name to the connection string at run time. I need to check if the server name is valid.
Help me out with this?
Try using the SqlDataSourceEnumerator.Instance.GetDataSources() method to get a list of available SQL servers. (see MSDN: http://msdn.microsoft.com/en-us/library/system.data.sql.sqldatasourceenumerator.getdatasources.aspx)
You alternatively might want to connect to the server in a Try..Catch block and check if the connection status is 'open'.

Invalid object name 'PetDatabase.Sales'

Im trying to run the following to import a large volume of sales data in a text file into a database. When i run the following i get the error: "Invalid object name 'PetDatabase.Sales'
BULK INSERT PetDatabase.Sales
FROM 'C:\Temp\P1.txt'
WITH
(
FORMATFILE = 'C:\Temp\PetSales.Fmt'
);
Can anyone see whats causing my problem? I do have the tables within a folder; however, when i tried PetsDatabase.Tables.Sales it made no difference.
Ignore this answer. It was written when the question was tagged with mysql. Leaving the answer here to keep the comments.
--
Try using LOAD DATA INFILE instead.
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Make sure PetDatabase.Sales exists in your text file.
Swap for whichever row and field terminator delimiters you're using. Here I'm using delimiters from a comma separated file
BULK INSERT PetDatabase
FROM 'c:\temp\p1.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM PetDatabase
GO
--Drop the table to clean up database.
SELECT *
FROM PetDatabase
GO
Also, make sure the following doesn't apply to you:
If a SQL Server user is logged in using Windows Authentication, the user can read only the files accessible to the user account, independent of the security profile of the SQL Server process.
When executing the BULK INSERT statement by using sqlcmd or osql, from one computer, inserting data into SQL Server on a second computer, and specifying a data_file on third computer by using a UNC path, you may receive a 4861 error.
To resolve this error, use SQL Server Authentication and specify a SQL Server login that uses the security profile of the SQL Server process account, or configure Windows to enable security account delegation.
Is PetDatabase is schema name or database name?
If it is database name, then include schema name also like this if your schema name is dbo.
PetDatabase.dbo.Sales

How to create Sql Synonym or "Alias" for Database Name?

I'm using ms sql 2008 and trying to create a database name that references another database. For example 'Dev', 'Test', 'Demo' would be database names that i could reference from my multiple config files, but each name would point to another database such as 'db20080101' or 'db20080114'.
[Edit]Some of the configs are for applications that i control the code and some aren't (ex. MS Reporting service datasource file configs)[/Edit]
It seems that sqlserver only supports synonyms for View,Table,Sproc, or Function. And Alias' are for table and column names.
Is there a way to do this that i missed in the docs?
Any one have any suggestions on a workaround?
use 3 part notation and alias up to the table, example
select * from tempdb.dbo.sysobjects a
join master.dbo.sysobjects b on a.id = b.id
There is a way to simulate this using a linked server. This assumes you have two SQL servers with the same set of databases one for development/test and one live.
Open SQL Server Management Studio on your development/test server
Right click Server Objects > Linked Servers
Select New Linked Server...
Select the General page
Specify alias name in Linked server field - this would normally be the name of your live server
Select SQL Native Client as the provider
Enter sql_server for Product Name
In Data Source specify the name of the development server
Add Security and Server Options to taste
Click OK
The above is for SQL Server 2005 but should be similar for 2008
Once you've done that you can write SQL like this:
SELECT * FROM liveservername.databasename.dbo.tablename
Now when your scripts are run on the development server with the linked server back to itself they will work correctly pulling data from the development server and when the exact same scripts are run on the live server they will work normally.
I've done something similar to this using another config file.
The new config file maps your generic name to all of the information needed to connect to that database (db name, user name, password, etc.) and then your connection function takes your generic name as an argument.
db.config:
DEV_DB_NAME = db20080101
DEV_DB_USER = dev_user
DEV_DB_PASS = dev_pass
TEST_DB_NAME = db20070101
TEST_DB_USER = test_user
TEST_DB_PASS = test_pass
connection code:
db_connection get_connection(string prefix) {
db_connection db_conn = new db_connection;
string db_name = get_config_value(config_path, prefix + "_DB_NAME");
string db_user = get_config_value(config_path, prefix + "_DB_USER");
string db_pass = get_config_value(config_path, prefix + "_DB_PASS");
db_conn.connect(db_name, db_user, db_pass);
return db_conn;
}
Then you just call get_connection() with your db alias as the argument.
I know this probably will not help in all situations, but you still have the option of using views. You can insert, delete, update, select into a view as long as it has a proper identity key (Primary Key). If you point it to another database, you should drop and recreate to get the different schema (in case you're working between production and test while making changes to the schema in test and/or production.
Synonyms are useful for when you're going to another database and have a 3 or 4 part name, but when you want to make it so you can have a set name, a linked server will also work which will let you use a fixed name if the table names are the same in both databases and you're just pointing between prod and test.